$(document).ready(function() {
	$(window).resize(resizeOverlay);
});

function initLightBoxEB() {
	$("a[rel='lightboxEB']").each(function(){
		var URL = $(this).attr("href");
		$(this).attr("rev", URL);
		$(this).removeAttr("href");
		$(this).css("cursor", "pointer");
		$(this).click(openLightBoxEB);
	});
}

var sourceObj = null;
var lightboxEB_width = 520;
var lightboxEB_height = 434;

function resizeOverlay() {
	var documentWidth = $(document).width();
	$("#lightboxEB_overlayBG").css("width", documentWidth);
	var bodyWidth = $("body").width();
	var leftOffset = (bodyWidth - $("#lightboxEB_box").width()) / 2;
	var offsetTop = getViewpointTop() + ((getViewpointHeight() - $("#lightboxEB_box").height()) / 2);
	$("#lightboxEB_box").animate({left: leftOffset, top: offsetTop});
}

function openLightBoxEB() {
	if ($.browser.msie && $.browser.version == "6.0")
		$("select").css("visibility", "hidden");
			
	sourceObj = $(this);
	var URL = $(this).attr("rev");
	
	var documentHeight = $(document).height();
	var documentWidth = $(document).width();
	$("body").prepend('<div id="lightboxEB_overlayBG">&nbsp;</div>');
	$("#lightboxEB_overlayBG").css({opacity: 0.7, width: documentWidth, height: documentHeight });

	var tags = '<div id="lightboxEB_box"><div id="lightboxEB_top"><div id="lightboxEB_topRight"></div></div><div id="lightboxEB_middle"><div id="lightboxEB_middleRight"><div id="lightboxEBContent"></div><div style="clear: both"></div></div></div><div id="lightboxEB_bottom"><div id="lightboxEB_bottomRight"><a href="javascript:closeLightBoxEB()"><span class="red">x</span> Close</a></div></div></div>';
	$("body").prepend(tags);

	var sourceOffset = sourceObj.offset();
	var offsetTop = getViewpointTop() + ((getViewpointHeight() - lightboxEB_height) / 2);	
	var bodyWidth = $("body").width();
	var leftOffset = (bodyWidth - lightboxEB_width) / 2;
	$("#lightboxEBContent").addClass("loading");
	$("#lightboxEB_box").css({ width: sourceObj.width(), height: sourceObj.height(), left: sourceOffset.left, top: sourceOffset.top});
	$("#lightboxEB_box").animate({height: lightboxEB_height, width: lightboxEB_width, left: leftOffset, top: offsetTop}
		, 	"slow"
		,	function () {
			$("#lightboxEB_box").css("height", "auto"); // set the height to dynamic so that $("#lightboxEB_box").height() returns the actual height
			$("#lightboxEBContent").load(
				URL,
				function (responseText, textStatus, XMLHttpRequest) {
					$("#lightboxEBContent").removeClass("loading");
					if (textStatus != "success") {
						alert("There was a problem while loading the form. Please try back later.");
					}
					else {
						var topOffset = getViewpointTop() + ((getViewpointHeight() - $("#lightboxEB_box").height()) / 2);
						$("#lightboxEB_box").animate({top: topOffset});
					}
				}
			)				
		}
	);
	return false;
}

function closeLightBoxEB() {
	var sourceOffset = sourceObj.offset();
	$("#lightboxEB_box").animate({height: sourceObj.height(), width: sourceObj.width(), left: sourceOffset.left, top: sourceOffset.top}
		,	function () {
			$("#lightboxEB_overlayBG").remove();
			$("#lightboxEB_box").remove();		
			if ($.browser.msie && $.browser.version == "6.0")
				$("select").css("visibility", "visible");				
		}
	);
}

function getViewpointTop() {
	return window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
}
function getViewpointHeight() {
	return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
}	