/*
Plugin Name : Simple Popup
Required 	: Jquery ui ver 1.4 +

Documentation : 

onclick="openModal('#targetDiv','AjaxRequest URL ( Let this blank if not use Ajax ) ')"> ... </a>

*/


$(function(){
	$('body').prepend('<div id="modal" style="width:100%; height:100%; position:fixed; top:0; left:0; background:#000000; opacity:0.6; z-index:8888; display:none;"></div>');
	
	document.onkeydown = function(e){ 
          if (e == null) { // ie 
            keycode = event.keyCode; 
          } else { // mozilla 
            keycode = e.which; 
          } 
          if(keycode == 27){ 
		  	closeModal(obj);
          } 
    };
	
});

function openModal(obj,urlAjaxRequest){	
	$(obj).addClass('popup');
	window.scroll(0,0);
	$('#modal').show();
	$(obj).attr('style','z-index:9999');
	$(obj).show();
	
	$('#modal').click(function(){
		if(urlAjaxRequest != ''){
			closeModal(obj,true);
		}else{
			closeModal(obj,false);
		}
	});
	
	$('#closeModal').remove();
	$('#cB').remove();
	$(obj).append('<div style="clear:both" id="cB"></div><a href="#" id="closeModal" onclick="closeModal(\''+ obj +'\',false)"> </a>');
	
	if(urlAjaxRequest != ''){
		$(obj).html('Please Wait...');
		$.ajax({
			url: urlAjaxRequest,
			cache: false,
			success: function(html){
				$(obj).html(html); 
				$(obj).append('<div style="clear:both" id="cB"></div><a href="#" id="closeModal" onclick="closeModal(\''+ obj +'\',true)"> </a>');
			}
		});
	}
}
function closeModal(obj,aj){	
	$('#modal').hide();
	$('#closeModal').remove();
	$(obj).hide();
	if(aj == true){
		$(obj).html('');
	}
}


(function( $ ){
	
  	$.fn.imagePopup = function() { 
		var i = 1;
		$(this).find('img').each(function(){
			$(this).attr('');
			var widt = $(this).width();
			var finalW = widt / 2;
			$(this).click(function(){
				$('#modal').show();
				$('#'.i).attr('style','margin-left:-' + finalW + 'px; width:' + widt + 'px; display:block; z-index:9999');
				$('body').append('<a href="#" id="closeModal" onclick="closeModal(\''+ obj +'\')" style="left:50%; margin-top:110px; margin-left:'+ finalW-10 +'px">close</a>');
			});
			i++;
		});
	}
})(JQuery);


//Line-A Design
