﻿// ----------------------------------------------
// File:		MassFadeEffect.js
// Author:		Nathan Derksen
// Description:	Manages image fade transitions amongst a number of images
// Example:
// massFadeInstance.addTarget("imageId", 20);
// ----------------------------------------------


// Global variable made available for convenience. Not done through a singleton due to performance optimization.
// This is a optimization sensitive grouping of code.
var massFadeInstance;

// ----------------------------------------------
// Function:	MassFadeEffect()
// Author:		Nathan Derksen
// Description:	Base class
// Inputs:		<None>
// Returns:		<Nothing>
// ----------------------------------------------
function MassFadeEffect()
{
	this.objectList = new Array();
	this.isRunning = false;
}
MassFadeEffect.fadeCompleteHandler = null;
// ----------------------------------------------
// Function:	addTarget()
// Author:		Nathan Derksen
// Description:	Indicate an image that is to be given a fade transition
// Inputs:		<String> targetId - The ID of the image to fade
//				<Number> rate - The number of percentage points to modify the fade for each animation interval. 
//						Positive values are for fade in, negative values are for fade out
// Returns:		<Nothing>
// ----------------------------------------------
MassFadeEffect.prototype.addTarget = function(targetId, rate)
{
	var targetElement = document.getElementById(targetId);
	if (rate)
	{
		if (rate == 0 || rate == null || rate == "")
		{
			rate = 20;
		}
	}
	else
	{
		rate = 20;
	}
	
	if (targetElement)
	{
		targetElement.style.visibility = "visible";
		if (rate < 0)
		{
			this.objectList[targetId] = {handle:targetElement, opacity:100+rate, rate:rate};
			setOpacity(targetElement, 100+rate);
		}
		else
		{
			this.objectList[targetId] = {handle:targetElement, opacity:0+rate, rate:rate};
			setOpacity(targetElement, 0+rate);
		}

		if (this.isRunning == false)
		{
			this.isRunning = true;
			setTimeout("updateOpacities()", 50);
		}
	}
};


// ----------------------------------------------
// Function:	updateOpacities()
// Author:		Nathan Derksen
// Description:	Change the opacities of each image in the list of images to transition according to its current and target opacities.
// Inputs:		<None>
// Returns:		<Nothing>
// ----------------------------------------------
function updateOpacities() 
{
	var itemArray = massFadeInstance.objectList;
	var itemHandle;
	var itemFound = false;
	
	for (var item in itemArray)
	{
		itemFound = true;
		itemHandle = itemArray[item];
		
		if (itemHandle.rate > 0 && itemHandle.opacity >= 100)
		{
			setOpacity(itemHandle.handle, 100);
			delete massFadeInstance.objectList[item];
		}
		else if (itemHandle.rate < 0 && itemHandle.opacity <= 0)
		{
			setOpacity(itemHandle.handle, 0);
			delete massFadeInstance.objectList[item];
		}
		else
		{
			setOpacity(itemHandle.handle, itemHandle.opacity);
			itemHandle.opacity += itemHandle.rate;
		}
	}

	if (itemFound == true)
	{
		setTimeout("updateOpacities()", 50);
	}
	else
	{
		massFadeInstance.isRunning = false;
		if (MassFadeEffect.fadeCompleteHandler) {
			MassFadeEffect.fadeCompleteHandler();
		}
	}
};



// ----------------------------------------------
// Function:	setOpacity()
// Author:		Nathan Derksen
// Description:	Change the opacity of a particular image
// Inputs:		<Element> sourceObj - The object to modify
//				<Number> number - The new opacity number
// Returns:		<Nothing>
// ----------------------------------------------
function setOpacity(sourceObj, opacity) 
{
	opacity = (opacity == 100)?99.999:opacity;

	// IE/Win
	if (opacity < 99.999)
	{
		sourceObj.style.filter = "alpha(opacity:"+opacity+")";
	}
	else
	{
		sourceObj.style.filter = "";
	}

	// Safari<1.2, Konqueror
	sourceObj.style.KHTMLOpacity = opacity/100;

	// Older Mozilla and Firefox
	sourceObj.style.MozOpacity = opacity/100;

	// Safari 1.2, newer Firefox and Mozilla, CSS3
	sourceObj.style.opacity = opacity/100;
};

// Initialize the fade manager.
massFadeInstance = new MassFadeEffect();
