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

var	boxTarget;
var boxInitTimer;
var boxFadeTimer;

boxFadeSetup();

function boxFadeSetup()
{
	if ((document.getElementById) &&
		(boxTarget=document.getElementById(boxTargetId)))
	{
		boxTarget.style.visibility = "hidden";
		
		if (typeof boxInitTimer != 'undefined')
		{
			window.clearTimeout(boxInitTimer);
		}
	}
	else {
		boxInitTimer = window.setTimeout("boxFadeSetup()",2);
	}
}

function boxFadeInit()
{
	if (document.getElementById)
	{
		/* get a handle on the fadeable object, to make code later more manageable */
		boxFadeSetup(); // shouldn't be necessary, but IE can sometimes get ahead of itself and trigger boxFadeInit 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 (boxTarget.style.MozOpacity!=null)
		{  
			// Mozilla's pre-CSS3 proprietary rule
			boxTarget.style.MozOpacity = 0;
		}
		else if (boxTarget.style.opacity!=null)
		{
			// CSS3 compatible
			boxTarget.style.opacity = 0;
		}
		else if (boxTarget.style.filter!=null)
		{
			// IE's proprietary filter
			boxTarget.style.filter = "alpha(opacity=0)";
		}
		
		// make the object visible again
		boxTarget.style.visibility = 'visible';
		boxFadeTimer = window.setTimeout("boxFadeIn(0)", 300);
	}
}

function boxFadeIn(opacity)
{
	if (boxTarget)
	{
		if (opacity <= 100)
		{
			if (boxTarget.style.MozOpacity!=null)
			{
				/* Mozilla's pre-CSS3 proprietary rule */
				boxTarget.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 (boxTarget.style.opacity!=null)
			{
				/* CSS3 compatible */
				boxTarget.style.opacity = (opacity/100)-.001;
			}
			else if (boxTarget.style.filter!=null)
			{
				/* IE's proprietary filter */
				boxTarget.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;
			boxFadeTimer = window.setTimeout("boxFadeIn("+opacity+")", 30);
		}
		else
		{
			if (boxTarget.style.MozOpacity!=null)
			{
				boxTarget.style.MozOpacity = 1;
			}
			else if (boxTarget.style.opacity!=null)
			{
				boxTarget.style.opacity = 1;
			}
			else if (boxTarget.style.filter!=null)
			{
				boxTarget.style.filter = 100;
			}
		}
	}
}

function boxFadeCancel()
{
	window.clearTimeout(boxFadeTimer);
}
