$().ready(function() {
	var lb_obj = {
		src: 'img',
		ie: {
			h: 1.1
		}/*,
		close: '#LightboxClose'*/
	}
	$("a[rel='lightbox']").lightbox(lb_obj);
});

(function($) {
	$.fn.lightbox = function(obj) {
		
		// Define variables
		var el = $(this);
		var link = el.attr("href");
		el.attr("href","javascript:void(0);");
		
		el.click(function() {
			
			var t = $(this);
			
			var config = {
				opacity: [0.5, .50, 50],
				zindex: 1001,
				bg: 'black',
				ie: {
					h: 1.1
				},
				close: 'onclick'
			}
			var lbel = {
					holder: "#LightboxHolder",
					backg: "#Lightbox"
				}
			var content = '';
			
			// Check for overwrites to properties
			if (obj) {
				if (obj.opacity) config.opacity = obj.opacity;
				if (obj.zindex) config.zindex = obj.zindex;
				if (obj.bg) config.bg = obj.bg;
				if (obj.ie.h) config.ie.h = obj.ie.h;
				if (obj.close) config.close = obj.close;
			}
			
			$(lbel.backg).remove();
			$(lbel.holder).remove();
			$("body").append("<div title=\"Click anywhere to close\" id=\"Lightbox\"></div>");
			
			// Assign CSS properties to it
			var css_obj = {
				height: ($.browser.msie ? (document.body.scrollHeight * config.ie.h)  : '100%'),
				width: document.body.scrollWidth,
				position: 'absolute',
				top: 0,
				left: 0,
				background: config.bg,
				'z-index': config.zindex,
				overflow: 'hidden',
				display: 'none',
				'-moz-opacity': config.opacity[0],
				opacity: config.opacity[1],
				filter: 'alpha(opacity='+config.opacity[2]+')'
			}
			var lb = $("#Lightbox");
			lb.css(css_obj);
			
			// Close the lightbox if it's clicked
			switch (config.close) {
				case 'onclick':
					lb.click(function() {
						LightboxClose();
					});
					break;
				default:
					$(config.close).click(function() {
						LightboxClose();
					});
					break;
			}
			
			function LightboxClose() {
				lb.fadeOut('fast', function() { $(this).remove(); });
				$(lbel.holder).fadeOut('fast', function() { $(this).remove(); });
			}
			
			function CenterLightbox() {
				$(lbel.holder).css("left",($("body").width() / 2) - ($(lbel.holder).width() / 2));
				$(lbel.backg).css("width", $("body").width());
			}
			$(window).resize(function() {
				CenterLightbox();
			});
			lb.fadeIn('normal',
				function() {
					var content = '';
					
					if (obj.src == 'img') {
						src = t.find('img');
						img = src.attr('src');
						title = src.attr('alt');
						content = "<table id=\"LightboxTitle\" cellspacing=0 cellpadding=0 border=0><tr align=right><td>"+title+"&nbsp;<a id=\"LightboxClose\" href=\"javascript:void(0);\">[ x ]</a></td></tr><tr><td>" + "<img src=\"images/common/loader.gif\" id=\"LoaderImage\" /><img style=\"display: none\" src=\""+img.replace('thumb=1','')+"\" alt=\"\" /></td><tr></table>";
					} else {
						content = "THIS IS A TEST";
					}
					
					var html = "<div id=\"LightboxHolder\">";
					html += "<div id=\"LightboxContent\">";
					html += content;
					html += "</div>";
					html += "</div>";
					$("body").append(html);
					$("#LightboxContent img:last").load(function() {
						$("#LoaderImage").remove();
						$(this).show('slow', CenterLightbox);
					});
					
					var css_obj = {
						position: 'absolute',
						top: 0,
						'margin-left': 'auto',
						'margin-right': 'auto',
						'margin-top': '50px',
						'z-index': (config.zindex + 1),
						float: 'left',
						background: "#FFF",
						padding: "8px",
						color: "#333"
					}
					var lbb = $(lbel.holder);
					lbb.css(css_obj);
					
					var lbc = $("#LightboxClose");
					lbc.click(function() {
						LightboxClose();
					});
					CenterLightbox();
				}
			);
		});
	}
})(jQuery);