/*
	$Id: overlay.js 6445 2008-05-14 04:56:05Z mjhorne $
*/

var overlaySpeedVertical = 300;
var overlaySpeedHorizontal = 700;
var overlayTimeBeforeStart = 2000;

var timer = null;

function initialiseOverlayLightbox(showOnload, timeToClose)
{
	$("#overlayLightbox").attr({
		elementWidth : $("#overlayLightbox").width(),
		elementHeight : $("#overlayLightbox").height(),
		elementMargin : -1 * $("#overlayLightbox").width() / 2,
		offsetMargin : $("#overlayLightbox").width() / 2 + (964 - $("#overlayLightbox").width()) / 2
	});

	var offset = $("#overlayLightbox").attr("offsetMargin");
	$("#overlayLightbox").css({marginLeft: parseInt(offset), width: 0, height: 0, opacity: 1});

	if(showOnload)
	{
		openOverlayLightbox();
		timer = setTimeout("closeOverlayLightbox()", timeToClose)
	}
}

function openOverlayLightbox()
{
	$("#overlayLightbox").show();
	$("#overlayLightbox").animate({height: 156}, "fast")
						 .animate({width: $("#overlayLightbox").attr("elementWidth"), marginLeft: $("#overlayLightbox").attr("elementMargin"), opacity: 0.5}, overlaySpeedHorizontal)
						 .animate({height: $("#overlayLightbox").attr("elementHeight"), opacity: 1}, overlaySpeedVertical);

  	replayFlashMovie('overlayLightboxFlash');

	$("#overlayLightboxOverlay").show();
	$("#expandOverlayLightbox").hide();
}


function replayFlashMovie(id)
{  
	var flashMovie = null;
	if(window.document[id])
	{  
		flashMovie =  window.document[id];  
	}  
	if(navigator.appName.indexOf("Microsoft Internet")==-1)
	{  
		if(document.embeds && document.embeds[id])
		{  
			flashMovie =  document.embeds[id];  
		}  
	} 
	else
	{  
		flashMovie =  document.getElementById(id);  
	}

	flashMovie.Rewind();  
	flashMovie.Play();  
}


function closeOverlayLightbox()
{
	$("#overlayLightbox").hide();
	$("#overlayLightbox").animate({height: 156}, overlaySpeedVertical)
						 .animate({width: 0, marginLeft:  $("#overlayLightbox").attr("offsetMargin"), opacity: 0.5}, overlaySpeedHorizontal);

	$("#overlayLightboxOverlay").hide();
	$("#expandOverlayLightbox").show();

	if(timer)
	{
		clearTimeout(timer);
	}
}


function initialiseOverlayOverlay(showOnload)
{
	$("#overlayLightboxOverlay").hide();
	$("#overlayLightboxOverlay").css({
		height : document.documentElement.scrollHeight + "px"
	});

	if(showOnload)
	{
		$("#overlayLightboxOverlay").show();
	}
}

function initialiseOverlayExpand()
{
	var expand = document.createElement("A");
	expand.id = "expandOverlayLightbox";
	expand.href = "#";

	$("#body").append(expand);
	$("#homeBody").append(expand);

	$("#expandOverlayLightbox").html("<span>Show overlay</span>");
}

function initialiseOverlay(showOnload, timeToClose)
{
	if(!activeOverlay)
	{
		return false;
	}

	overlay = $("<div>");
	overlay.attr("id", "overlayLightboxOverlay");
    $("body").prepend(overlay);

	if(!timeToClose || (timeToClose <= 0))
	{
		var timeToClose = 20000;
	}

	initialiseOverlayExpand();

	if(showOnload && (overlayTimeBeforeStart > 0))
	{
		setTimeout(function() { initialiseOverlayOverlay(showOnload); }, overlayTimeBeforeStart);
		setTimeout(function() { initialiseOverlayLightbox(showOnload, timeToClose); }, overlayTimeBeforeStart);
	}
	else
	{
		initialiseOverlayOverlay(showOnload);
		initialiseOverlayLightbox(showOnload, timeToClose);
	}

	initialiseOverlayClicks();
}


function initialiseOverlayClicks()
{
	$("#expandOverlayLightbox").click(function() {
		openOverlayLightbox();
		return false;
	});

	$("#closeOverlayLightbox").click(function() {
		$("#expandOverlayLightbox").show();
		closeOverlayLightbox();
		return false;
	});
}
