//-----------------------------------------------------------------------------
//	Hover menu functions
//-----------------------------------------------------------------------------

var navCurrent = 0;
var pdcNavCurrent = 0;

function showNav(nav)
{
	navCurrent = nav;
	window.setTimeout("updateNav()", 50);
}

function hideNav(nav)
{
	navCurrent = 0;
	window.setTimeout("updateNav()", 50);
}

function updateNav()
{
	for(i = 1; i <= 5; i++)
	{
		if(navCurrent != i)
		{
			setNavInvisible(i);
		}
		else
		{
			setNavVisible(i);
		}
	}
}

function setNavVisible(nav)
{
	document.getElementById("nav" + nav).style.display = "block";
}

function setNavInvisible(nav)
{
	document.getElementById("nav" + nav).style.display = "none";
}


//-----------------------------------------------------------------------------
//	RotateImages opacity transition functions
//-----------------------------------------------------------------------------

var tickLoop = 0;
var intervalId;
var imageTimeDisplayed = 4000;
var imageTransitionTime = 500;

var currentImage = 0;

//Array of image file paths
var imageList = new Array();

//Array of images
var imageArray = new Array();

function RotateImages()
{
	//Buffer image array
	for (i = 0; i < imageList.length; i++)
	{
		imageArray[i] = new Image();
		imageArray[i].src = imageList[i];
	}

	//Minimum of 2 images must be in the array to transition properly
	if(i > 1)
	{
		setInterval("ImageTransition()", imageTimeDisplayed);
	}
}

function ImageTransition()
{
	//Increment currentImage pointer - it is assumed that the initial image on display is also in array position 0, so is also correct to skip
	currentImage++;
	if(currentImage == imageList.length)
	{
		currentImage = 0;
	}

	//Set the background to the new image displayed after the transition completes
	document.getElementById('ImageTransitionBackground').style.backgroundImage = "url(" + imageList[currentImage] + ")";

	//Begin the transition
	OpacityTransition("ImageTransitionForeground", 0.00, imageTransitionTime);
}

function OpacityTransition(elementId, newOpacity, transitionTime)
{
	//Desired tick time, the tick time calculation will use this number to calculate a tick time as close as possible
	var targetTick = 30;

	//CSS3 compliant browsers use the opacity value directly, legacy IE uses the filter value to create opacity effects but will still store and report from the opacity value. This will be converted to a 0-100 scale for calculation purposes within the function
	var currentOpacity = document.getElementById(elementId).style.opacity * 100;

	//Calculated even number of ticks, rounding the desired transition time over the desired tick time
	var tickCount = Math.round(transitionTime / targetTick);

	//Calculated single tick duration
	var tick = transitionTime / tickCount;

	//Calculated change in opacity per tick
	var opacityStep = (currentOpacity - (newOpacity * 100)) / tickCount;

	//Set the loop counter to the number of ticks to process
	tickLoop = tickCount;

	//Initiate the SetNewOpacity loop
	intervalId = setInterval("SetNewOpacity('"+elementId+"', "+opacityStep+", "+newOpacity+")", tick);
}

function UpdateImageView()
{
	//Set foreground image
	document.getElementById('ImageTransitionImg').src = imageArray[currentImage].src;

	//IE filter value
	document.getElementById('ImageTransitionForeground').style.filter = "alpha(opacity=100)";
	//CSS3 opacity value - opacity set to 0.99 to avoid flickering bug in Firefox
	document.getElementById('ImageTransitionForeground').style.opacity = 0.99;
}

function SetNewOpacity(elementId, opacityStep, finalOpacity)
{
	tickLoop--;

	if(tickLoop == 0)
	{
		//Set the final value for opacity

		//IE filter value
		document.getElementById(elementId).style.filter = "alpha(opacity=" + (parseFloat(finalOpacity) * 100) + ")";
		//CSS3 opacity value
		document.getElementById(elementId).style.opacity = finalOpacity;

		//Set the foreground to the new image and set opacity to full
		UpdateImageView();

		clearInterval(intervalId);
	}
	else
	{
		//adjust the opacity by the value of one calculated step

		//IE filter value
		document.getElementById(elementId).style.filter = "alpha(opacity=" + (parseFloat(document.getElementById(elementId).style.opacity * 100) - parseFloat(opacityStep)) + ")";
		//CSS3 opacity value
		document.getElementById(elementId).style.opacity = parseFloat(document.getElementById(elementId).style.opacity) - parseFloat(opacityStep/100);
	}
}