(function($) {
	$.fn.lightBox = function(settings) 
	{
		settings = $.extend({
		   width: 800,
		   height: 600,
		   maskStyle: '',
		   contentStyle: '',
		   type : 'url',
		   location:"includes/lightBox/"
		}, settings);
		
		$(this).each(function(){
			  $(this).bind('click',function(e){
					  openLight($(this));
					  e.preventDefault();
			  });
		});
		
		function openLight($this)
		{
			var lWidth = settings.width;
			var lHeight = settings.height;
			
			var loader = $("<div class='lightbox' id='loader' style='position:fixed; display:none; background-color:#FFF; z-index:999; top:50%; left:50%; width:32px; height:32px; margin-top:-16px; margin-left:-16px; padding:10px;" + settings.contentStyle + "'><img src='" + settings.location + "loader.gif' width='32' height='32'/></div>");
			var light = $("<div class='lightbox' id='light' style='position:fixed; overflow:auto; display:none; background-color:#FFF; top:50%; left:50%; width:" + String(lWidth + 10) + "px; height:" + String(lHeight + 10) + "px; margin-top:" + String((lHeight + 10)/-2) + "px; margin-left:" + String((lWidth + 10)/-2) + "px; padding:10px; z-index:1000;" + settings.contentStyle + "'/>");
			var mask = $("<div class='lightbox' id='mask' style='position:fixed; display:none; background-color:#000; top:0; left:0; width:100%; height:100%; z-index:998; opacity:0.8; -moz-opacity:0.8; filter:alpha(opacity=80);" + settings.maskStyle + "'/>");
			
			mask.click(function()
			{
				$('#light').hide('slow',function(){$(".lightbox").remove()});
				$('#mask').fadeOut('fast');
			})
			
			$("body").append(mask);
			$("body").append(loader);
			$("body").append(light);
			
			if (settings.type == "url")
			{	
				$('#loader').show('slow');
				$.ajax({
					url:$this.attr("href"),
					success:function(result)
					{
						$('#light').html(result);
						$('#loader').fadeOut('slow');
						$('#light').show('slow');
					}
				});
			}
			else if (settings.type == "img")
			{
				$("#light").append($("<img src='" + $this.attr("href") + "' width='" + lWidth + "' height='" + lHeight + "'/>"));
			}
			
			$('#mask').fadeIn('fast');
		}
	};
	
	$.lightBox = function(settings) 
	{
		settings = $.extend({
		   url : "about:blank",
		   width: 800,
		   height: 600,
		   maskStyle: '',
		   contentStyle: '',
		   method : 'POST',
		   data : {lVoid : Math.round(Math.random()*100)},
		   type : 'url',
		   location:"includes/lightBox/"
		}, settings);
		
		var lWidth = settings.width;
		var lHeight = settings.height;
		
		var loader = $("<div class='lightbox' id='loader' style='position:fixed; display:none; background-color:#FFF; z-index:999; top:50%; left:50%; width:32px; height:32px; margin-top:-16px; margin-left:-16px; padding:10px;" + settings.contentStyle + "'><img src='" + settings.location +"loader.gif' width='32' height='32'/></div>");
		var light = $("<div class='lightbox' id='light' style='position:fixed; overflow:auto; display:none; background-color:#FFF; top:50%; left:50%; width:" + String(lWidth + 10) + "px; height:" + String(lHeight + 10) + "px; margin-top:" + String((lHeight + 10)/-2) + "px; margin-left:" + String((lWidth + 10)/-2) + "px; padding:10px; z-index:1000;" + settings.contentStyle + "'/>");
		var mask = $("<div class='lightbox' id='mask' style='position:fixed; display:none; background-color:#000; top:0; left:0; width:100%; height:100%; z-index:998; opacity:0.8; -moz-opacity:0.8; filter:alpha(opacity=80);" + settings.maskStyle + "'/>");
		
		mask.click(function()
		{
			$('#light').hide('slow');
			$('#mask').fadeOut('fast',function(){$(".lightbox").remove()});
		})
		
		$("body").append(mask);
		$("body").append(loader);
		$("body").append(light);
		
		if (settings.type == "url")
		{	
			$('#loader').show('slow');
			$.ajax({
				url:settings.url,
				success:function(result)
				{
					$('#light').html(result);
					$('#loader').fadeOut('slow');
					$('#light').show('slow');
				}
			});
		}
		else if (settings.type == "img")
		{
			$("#light").append($("<img src='" + settings.url + "' width='" + lWidth + "' height='" + lHeight + "'/>"));
		}
		
		$('#mask').fadeIn('fast');
	};
	
	$.resize = function(settings) 
	{
		settings = $.extend({
		   width: 800,
		   height: 600
		}, settings);
		
		lWidth = settings.width;
		lHeight = settings.height;
		$("#light").animate({"width": lWidth+"px", "height": lHeight+"px", "margin-left": lWidth/-2+"px", "height": lHeight+"px", "margin-top": lHeight/-2+"px"});
	};
	
})(jQuery);
