// Functions to contol the fading of images
// The code that includes this file must define and se the variable fadeTargetId

var	menuTarget;
var menuInitTimer;
var menuFadeTimer;

menuFadeSetup();

function menuFadeSetup()
{
	if ((document.getElementById) &&
		(menuTarget=document.getElementById(menuTargetId)))
	{
		menuTarget.style.visibility = "hidden";
		
		if (typeof menuInitTimer != 'undefined')
		{
			window.clearTimeout(menuInitTimer);
		}
	}
	else {
		menuInitTimer = window.setTimeout("menuFadeSetup()",2);
	}
}

function menuFadeInit()
{
	if (document.getElementById)
	{
		/* get a handle on the fadeable object, to make code later more manageable */
		menuFadeSetup(); // shouldn't be necessary, but IE can sometimes get ahead of itself and trigger menuFadeInit first
		
		/* set the initial opacity in a (hopefully) cross browser way
		   notice that because of the way the image is in front, and not obfuscated
		   by another object we need to "fade out", i don't need a fallback mechanism
		   to show/hide the covering object...the image is just there, full stop */
		
		if (menuTarget.style.MozOpacity!=null)
		{  
			// Mozilla's pre-CSS3 proprietary rule
			menuTarget.style.MozOpacity = 0;
		}
		else if (menuTarget.style.opacity!=null)
		{
			// CSS3 compatible
			menuTarget.style.opacity = 0;
		}
		else if (menuTarget.style.filter!=null)
		{
			// IE's proprietary filter
			menuTarget.style.filter = "alpha(opacity=0)";
		}
		
		// make the object visible again
		menuTarget.style.visibility = 'visible';
		menuFadeTimer = window.setTimeout("menuFadeIn(0)", 300);
	}
}

function menuFadeIn(opacity)
{
	if (menuTarget)
	{
		if (opacity <= 100)
		{
			if (menuTarget.style.MozOpacity!=null)
			{
				/* Mozilla's pre-CSS3 proprietary rule */
				menuTarget.style.MozOpacity = (opacity/100)-.001;
				/* the .001 fixes a glitch in the opacity calculation which normally results in a flash when reaching 1 */
			}
			else if (menuTarget.style.opacity!=null)
			{
				/* CSS3 compatible */
				menuTarget.style.opacity = (opacity/100)-.001;
			}
			else if (menuTarget.style.filter!=null)
			{
				/* IE's proprietary filter */
				menuTarget.style.filter = "alpha(opacity="+opacity+")";
				/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
			}
			opacity += 10;
			menuFadeTimer = window.setTimeout("menuFadeIn("+opacity+")", 30);
		}
		else
		{
			if (menuTarget.style.MozOpacity!=null)
			{
				menuTarget.style.MozOpacity = 1;
			}
			else if (menuTarget.style.opacity!=null)
			{
				menuTarget.style.opacity = 1;
			}
			else if (menuTarget.style.filter!=null)
			{
				menuTarget.style.filter = 100;
			}
		}
	}
}

function menuFadeCancel()
{
	window.clearTimeout(menuFadeTimer);
}
