///*------------------------------------------------------------------------------------
//Lightbox functionality
//This lightbox allows for multiple lightbox content within the same page.  
//The lightbox is created on first open, and the content moved from it's location
//in the DOM to inside the light box.  When the lightbox is reopend, the content
//is shuffled between being hidden at the end of the body, or displayed in the 
//lightbox

//- place your content in a div with a class of modal-content and a unique id
//- open the lightbox in two ways:
//	1. Give an element a class of 'modalelement' and a rel='unique id of content'
//	2. Call openLightbox('unique id of element');
//- close the lightbox in two ways:
//	1. Click on the element that opened it.
//	2. Call closeLightbox();
//	
//Required CSS
//adjust #lightbox z-index accordingly
//--------------------------------------------------------------------------------------
//#lightbox                   { width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:20; }
//#lightbox .lightboxBg       { width:100%; height:100%; background-color:#FFFFFF; position:absolute; 
//							  top:0px; left:0px; opacity:.70; filter: alpha(opacity=70); -moz-opacity: 0.70;  }
//#lightbox .contentOuter     { position:absolute; z-index:1; }
//.modalpopup		        { background-color:#ffffff; display:none; }
//.modalelement, .modalclose	{ cursor:pointer; }
//------------------------------------------------------------------------------------*/
function _openLightbox(obj) {
	openLightbox(obj.attr("rel"));
}

function openLightbox(elementId) {	
	//create lightbox if it doesn't exist yet;
    if (!$jQ("#lightbox")[0]) {
	    var lightbox = document.createElement("div");
	    $jQ(lightbox).attr('id', "lightbox");

	    var lightboxBg = document.createElement("div");
	    $jQ(lightboxBg).addClass("lightboxBg");
	    $jQ(lightbox).append(lightboxBg);
	    //$jQ(lightboxBg).click(function() { closeLightbox(); });

	    var contentOuter = document.createElement("div");
	    $jQ(contentOuter).addClass("contentOuter");
	    $jQ(lightbox).append(contentOuter);
  
    	$jQ("body").append(lightbox);
		
		$jQ(window).resize(
			function() {
				positionLightbox();
			});		
		$jQ(window).scroll(
			function() {
				positionLightbox();
			});
	}
		
	$jQ("body").append($jQ(".contentOuter .modalpopup").hide());	
	$jQ(".contentOuter").append($jQ("#" + elementId).show());

	$jQ("#lightbox").show();
	positionLightbox();
	$jQ("#lightbox").hide();

	$jQ("#lightbox").show();
}

/* Calculates the position of the lightboxed element. Centers the element on screen.*/
function positionLightbox() {
	var contentOuter = $jQ("#lightbox").find(".contentOuter");
	var element = $jQ("#lightbox").find(".modalpopup");
	var x = ($jQ(window).width() - $jQ(element).width()) / 2;
	var y = ($jQ(window).height() - $jQ(element).height()) / 2;
	$jQ(contentOuter).css({top:(y + $jQ(document).scrollTop()) + "px", left:x + "px"});
	$jQ("#lightbox").height($jQ(document).height());
	$jQ("#lightbox").css("top", "0px");
}

function closeLightbox() {
	$jQ("#lightbox").hide();
	$jQ("#videoplayerpopup").html();
}

function bindLightbox() {
    $jQ(".modalelement").not(".event-attached").addClass("event-attached").click(function() { _openLightbox($jQ(this)); });
    $jQ(".modalclose").not(".event-attached").addClass("event-attached").click(function() { closeLightbox(); });
}

function initLightbox() {
	bindLightbox();
	
	if ($jQ("#lightbox")[0]) {
		$jQ(window).resize(
			function() {
				positionLightbox();
			});
		$jQ(window).scroll(
			function() {
				positionLightbox();
			});
	    //$jQ(".lightboxBg").click(function() { closeLightbox(); });
	}	
}

$jQ(function() {	
	initLightbox();
});
