/**
 * Euro Simple Lightbox
 * Euro RSCG Edge
 */


/**
 * Configurable Properties for Lightbox functionality.
 * <pre>
 * LightboxProperties.BackgroundOpacityLevel   : Decimal value between 0.0 and 1.0. Default is 0.70
 * LightboxProperties.FadeInSpeedBackground    : Milliseconds to Fade In the Lightbox Background. Default is 100
 * LightboxProperties.FadeInSpeedLightbox      : Milliseconds to Fade In the Lightbox. Default is 333
 * LightboxProperties.FadeOutSpeedBackground   : Milliseconds to Fade Out the Lightbox Background. Default is 100
 * LightboxProperties.FadeOutSpeedLightbox     : Milliseconds to Fade Out the Lightbox. Default is 100
 * LightboxProperties.DoCloseOnBackgroundClick : Determines if the Lightbox will close on a background click. Default is true.
 * LightboxProperties.DoRecenterOnWindowResize : Determines if the Lightbox will recenter on window resize event. Default is true.
 * LightboxProperties.DoRecenterOnWindowScroll : Determines if the Lightbox will recenter on window scroll event. Default is false.
 * LightboxProperties.DoScrollScreen           : Determines if the Page will scroll as it would have before Lightbox is opened. Default is false.
 * </pre>
 */
var LightboxProperties = {
	/** Background Opacity */
	BackgroundOpacityLevel : 0.70,
	/** Milliseconds to Fade In the Lightbox Background */
	FadeInSpeedBackground : 100,
	/** Milliseconds to Fade In the Lightbox */
	FadeInSpeedLightbox : 333,
	/** Milliseconds to Fade Out the Lightbox Background */
	FadeOutSpeedBackground : 100,
	/** Milliseconds to Fade Out the Lightbox */
	FadeOutSpeedLightbox : 100,
	/** Determines whether or not the Lightbox will close if the background is clicked. (true or false) */
	DoCloseOnBackgroundClick : true,
	/** Lightbox will recenter on window resize. (true or false) */
	DoRecenterOnWindowResize : true,
	/** Lightbox will recenter on window scroll. (true or false) */
	DoRecenterOnWindowScroll : false,
	/** If false, the body overflow CSS property is set to hidden, causing the page to not scroll. (true or false) */
	DoScrollScreen : false
};


/* Declare .lightbox class w/ properties to hide it as soon as possible. Needed at this time for browsers other than FF. */
$("head").append('<style type="text/css">.lightbox{display:none;visibility:hidden;}</style>');


$(document).ready(function(){
	
	var BodyOverflowOriginalValue = $("body").css("overflow");
	
	/*
	 * CSS for the lightbox
	 */
	function SetLightboxCSS(){
		$(".lightbox")
			.css({
				display : "none",
				visibility : "hidden",
				position : "absolute",
				top : "25%",
				left : "25%",
				zIndex : "1001",
				filter : "alpha(opacity=100)",
				"-moz-opacity" : 1.0,
				opacity : 1.0
			});
	}
	SetLightboxCSS();
	
	/*
	 * Create Lightbox Background
	 */
	$("body").prepend('<div id="div_lightbox_bg"></div>');
	/*
	 * CSS for Lightbox Background
	 */
	function SetLightboxBackgroundCSS(){
		$("#div_lightbox_bg")
			.css({
				display : "none",
				visibility : "hidden",
				zIndex : "1000",
				position : "absolute",
				top : "0px",
				left : "0px",
				width : "100%",
				height : "100%",
				overflow : "hidden",
				padding : "0px",
				margin : "0px",
				backgroundColor : "#000",
				filter : "alpha(opacity="+(LightboxProperties.BackgroundOpacityLevel*100)+")",
				"-moz-opacity" : LightboxProperties.BackgroundOpacityLevel,
				opacity : LightboxProperties.BackgroundOpacityLevel
			});
	}
	SetLightboxBackgroundCSS();
	
	/*
	 * Event Handler for Lightbox Trigger - OPENS Lightbox
	 */
	$(".lightbox_trigger").click(function(){
		if(!LightboxProperties.DoScrollScreen)
			$("body").css("overflow","hidden");
		
		var lightbox_data = $(this).data("lightbox_dom_data");
		
		var lightbox = $("#"+lightbox_data["lb_id"]+":hidden");
		if(lightbox){
			SetLightboxBackgroundCSS();
			
			$("#div_lightbox_bg:hidden")
				.stop()
				.css({
					visibility:"visible",
					width:$(document).width() + "px",
					height:$(document).height() + "px"
				})
				.fadeIn(LightboxProperties.FadeInSpeedBackground, function(){
					SetLightboxCSS();
					
					var left = Math.round(($(window).width() / 2) - (lightbox.width() / 2)) + $(window).scrollLeft();
					if(left < 0) left = 0;
					var top = Math.round(($(window).height() / 2) - (lightbox.height() / 2)) + $(window).scrollTop();
					if(top < 0) top = 0;
					
					lightbox
						.stop()
						.css({left:left+"px", top:top+"px", visibility:"visible"})
						.fadeIn(LightboxProperties.FadeInSpeedLightbox);
				});
		}
	});
	
	/*
	 * Event Handler to Close Lightbox
	 */
	$("#div_lightbox_bg, .lightbox_close").click(function(){
		if(!LightboxProperties.DoCloseOnBackgroundClick && $(this).is("#div_lightbox_bg")){
			// Do nothing
		}
		else {
			if(!LightboxProperties.DoScrollScreen)
				$("body").css("overflow",BodyOverflowOriginalValue);
			
			var lightbox = $(".lightbox:visible");
			if(lightbox){
				lightbox
					.stop()
					.fadeOut(LightboxProperties.FadeOutSpeedLightbox, function(){
						SetLightboxCSS();
						$("#div_lightbox_bg")
							.stop()
							.fadeOut(LightboxProperties.FadeOutSpeedBackground, function(){
								SetLightboxBackgroundCSS();
						});
					});
			}
		}
	});
	
	/*
	 * Events/Function to Recenter Lightbox in Window
	 */
	if(LightboxProperties.DoRecenterOnWindowResize)
		$(window).resize(CenterLightboxOnPage);
	if(LightboxProperties.DoRecenterOnWindowScroll)
		$(window).scroll(CenterLightboxOnPage);
	function CenterLightboxOnPage(){
		var lightbox = $(".lightbox:visible");
		if(lightbox) {
			var bgWidth = $(document).width() + "px";
			var bgHeight = $(document).height() + "px";
			$("#div_lightbox_bg:visible")
				.stop()
				.css({width:bgWidth, height:bgHeight});
			
			var x = Math.round(($(window).width() / 2) - (lightbox.width() / 2)) + $(window).scrollLeft();
			if(x < 0) x = 0;
			var y = Math.round(($(window).height() / 2) - (lightbox.height() / 2)) + $(window).scrollTop();
			if(y < 0) y = 0;
			
			lightbox.css({left:x+"px", top:y+"px"});
		}
	}

});


/**
 * Associates a lightbox trigger element with a lightbox containing element
 * 
 * @param {Object} triggerElementId - Element ID of the element that opens the Lightbox
 * @param {Object} lightboxElementId - Element ID of the Lightbox content container
 */
function BindLightboxToTrigger(triggerElementId, lightboxElementId){
	$(document).ready(function(){
		$("#"+triggerElementId)
			.data("lightbox_dom_data",
				{
					lb_id : lightboxElementId
				}
			);
	});
}

