/* Syntax: fadeObject("objectName", targetValue, fadeRate)
Note: To change the "frame rate" of the fading process,
change the "frameRate" value at the top of the "fadeObject" function

The "fadeObject" function requires the "get" and "set" opacity functions in order to work
They are included in this script, so there is no need to include all 3 in your document

When the object has finished fading to the specified target opacity, if there is a boolean
variable named "objectName" + "IsDoneFading" that is false, it will be set to true.
This is for use with the "whenTrueDo" function */

function fadeObject(objectName, targetValue, fadeRate)
	{
	/* The "frame rate" of the fade, 41 is about 24 fps */
		var frameRate = 41;
		
		(getOpacity(window[objectName]) > targetValue) ? fadeRate = -fadeRate : null;
		
	/* Set the global "IsDoneFading" tracking variable to false */
		window[objectName + "IsDoneFading"] = false;
		window[objectName + "FadeInterval"] = window.setInterval("objectFader('"+objectName+"',"+targetValue+","+fadeRate+")", frameRate);
	}
	
function objectFader(objectName, targetValue, fadeRate)
	{
		if (fadeRate > 0 && (getOpacity(window[objectName]) + fadeRate) >= targetValue)
			{
				setOpacity(window[objectName], targetValue);
			/* Set the "IsDoneFading" tracking variable to true */	
				window[objectName + "IsDoneFading"] = true;
				clearInterval(window[objectName + "FadeInterval"]);
			}
		else if (fadeRate > 0 && (getOpacity(window[objectName]) + fadeRate) < targetValue)
			{
				setOpacity(window[objectName], getOpacity(window[objectName]) + fadeRate);
			}


		if (fadeRate < 0 && (getOpacity(window[objectName]) + fadeRate) <= targetValue)
			{
				setOpacity(window[objectName], targetValue);
			/* Set the "IsDoneFading" tracking variable to true */				
				window[objectName + "IsDoneFading"] = true;				
				clearInterval(window[objectName + "FadeInterval"]);
			}
		else if (fadeRate < 0 && (getOpacity(window[objectName]) + fadeRate) > targetValue)
			{
				setOpacity(window[objectName], getOpacity(window[objectName]) + fadeRate);
			}
	}


/* Syntax: getOpacity("targetElement") 
Returns the opacity (0-100) of the specified object */

function getOpacity(targetElement)
	{
		var opacityFound = 100;
		
		(targetElement.style.MozOpacity != null && targetElement.style.MozOpacity.length > 0) ? opacityFound = targetElement.style.MozOpacity * 100 : null;
		(targetElement.style.opacity != null && targetElement.style.opacity.length > 0) ? opacityFound = targetElement.style.opacity * 100 : null;
		if (targetElement.style.filter != null && targetElement.style.filter.length > 0)
			{
				for (var i=0; i<targetElement.style.filter.length; i++)
					{
						(targetElement.style.filter.substr(i, 1) == "=") ? opacityFound = parseFloat(targetElement.style.filter.substr(i+1)) : null;
					}
			}
		
		return opacityFound;
	}


/* Syntax: setOpacity("targetElement", value (0-100))
Will change the CSS opacity property of the specified object */

function setOpacity(targetElement, value)
	{
		(targetElement.style.MozOpacity!=null) ? targetElement.style.MozOpacity=(value/100) : null;
		(targetElement.style.opacity!=null) ? targetElement.style.opacity=(value/100) : null;
		(targetElement.style.filter!=null) ? targetElement.style.filter = "alpha(opacity="+value+")" : null;
	}
