/*****

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact. 

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html

Modified for Echo Music by Mike Davis, 9/2/2009.

Depends on http://yui.yahooapis.com/2.7.0/build/yahoo-dom-event/yahoo-dom-event.js

Must be initialized by calling xFade(). Example:
	xFade("home_rotator", "home_rotator_content", "home_rotator_buttons", "home_rotator_button");

*****/
function xFade(containerName, elmClass, buttonContainerID, buttonClass)
{
	YAHOO.util.Event.onDOMReady(so_init);
	
	var d = document;
	var elms = new Array();
	var buttons = new Array();
	var timeoutID = null;
	var current = 0;
	var timePerItem = 3000;
	
	function so_init()
	{
		if (!d.getElementById || !d.createElement)
			return;

		var container = d.getElementById(containerName);
		if (container)
		{
			elms = YAHOO.util.Dom.getElementsByClassName(elmClass, 'div', container);
			if (elms.length > 1)
			{
				for (i = 1; i < elms.length; i++)
				{
					elms[i].xOpacity = 0;
				}
				YAHOO.util.Dom.setStyle(elms[0], 'display', 'block');
				elms[0].xOpacity = 1.00;
				
				// Create buttons.
				xf_createButtons(elms.length);
				
				timeoutID = setTimeout(so_xfade, timePerItem);
			}
			else if (elms.length == 1)
			{
				YAHOO.util.Dom.setStyle(elms[0], 'display', 'block');
			}
			container = null;
		}
	}
	
	function so_xfade()
	{
		var cOpacity = elms[current].xOpacity;
		var nIndex = elms[current+1] ? current + 1 : 0;
	
		var nOpacity = elms[nIndex].xOpacity;
		
		cOpacity -= .05; 
		nOpacity += .05;
		
		YAHOO.util.Dom.setStyle(elms[nIndex], 'display', 'block');
		elms[current].xOpacity = cOpacity;
		elms[nIndex].xOpacity = nOpacity;
		
		setOpacity(elms[current]); 
		setOpacity(elms[nIndex]);
		
		if (cOpacity <= 0)
		{
			YAHOO.util.Dom.setStyle(elms[current], 'display', 'none');
			current = nIndex;
			timeoutID = setTimeout(so_xfade, timePerItem);
		}
		else
		{
			timeoutID = setTimeout(so_xfade, 50);
		}
		
		if (cOpacity <= 0.5)
		{
			// It's necessary to loop, in case the user jumped out of sequence by clicking one of the buttons.
			// Otherwise, we would know that only the previous one needs to change.
			for (i = 0; i < buttons.length; i++)
			{
				if (i == nIndex)
				{
					YAHOO.util.Dom.addClass(buttons[i], 'selected');
				}
				else
				{
					YAHOO.util.Dom.removeClass(buttons[i], 'selected');
				}
			}
		}
		
		function setOpacity(obj)
		{
			if (obj.xOpacity > 1.00)
			{
				obj.xOpacity = 1.00;
				return;
			}
			YAHOO.util.Dom.setStyle(obj, 'opacity', obj.xOpacity);
		}
	}
	
	function xf_createButtons(count)
	{
		if (buttonContainerID)
		{
			var btnContainer = document.getElementById(buttonContainerID);
			if (btnContainer)
			{
				for (i = 0; i < count; i++)
				{
					var btn = document.createElement("a");
					btn.setAttribute("href", "#");
					btn.setAttribute("onclick", "return false;");
					// For IE, which doesn't seem to recognize the onclick when you use setAttribute.
					if (btn.onclick)
						btn.onclick = "return false;";
					YAHOO.util.Dom.addClass(btn, buttonClass);
					if (i == 0)
						YAHOO.util.Dom.addClass(btn, 'selected');
					btn.innerHTML = (i + 1);
					btnContainer.appendChild(btn);
					var objArgs = { index:i };
					// Set up button click event handler.
					YAHOO.util.Event.addListener(btn, "click", xf_buttonClick, objArgs);
					
					btn = null;
				}
				buttons = YAHOO.util.Dom.getElementsByClassName(buttonClass, 'a', btnContainer);
				
				btnContainer = null;
			}
		}
	}
	
	function xf_buttonClick(e, objArgs)
	{
		clearTimeout(timeoutID);
		current = objArgs.index;
		// To prevent any further cross-fading, comment out the next line.
		//timeoutID = setTimeout(so_xfade, timePerItem);
		
		// Set the selected content to full opacity and the others to not display.
		for (i = 0; i < elms.length; i++)
		{
			if (i == objArgs.index)
			{
				elms[i].xOpacity = 1;
				YAHOO.util.Dom.setStyle(elms[i], 'opacity', 1);
				YAHOO.util.Dom.setStyle(elms[i], 'display', 'block');
			}
			else
			{
				elms[i].xOpacity = 0;
				YAHOO.util.Dom.setStyle(elms[i], 'opacity', 0);
				YAHOO.util.Dom.setStyle(elms[i], 'display', 'none');
			}
		}

		// Add the selected class to the appropriate button and remove it from the others.
		for (i = 0; i < buttons.length; i++)
		{
			if (i == objArgs.index)
			{
				YAHOO.util.Dom.addClass(buttons[i], 'selected');
			}
			else
			{
				YAHOO.util.Dom.removeClass(buttons[i], 'selected');
			}
		}
		
		YAHOO.util.Event.preventDefault(e);
		return false;
	}
}