// ############################################################################
// ##
// ##  ROTATOR/FADER 
// ##  Version 2.0
// ##  
// ##  This function requires fadomatic.js to be loaded.
// ##
// ############################################################################

// These globals must be set for the fader functions to work
var gblnRotatorEnabled = true;
var gblnRotatorRestarting = false;
var garrRotatorElements = new Array();

// Rotator Configurations
gintRotatorTimer = 8500;             // How long each item stays on the screen.
gintRotatorNextOffset = 1500;        // The pause between fade out and fade in when items change.
gintRotatorRestartTimer = '';        // How long the rotator waits to resume after 'next' or 'previous' is clicked. '' = off. Otherwise MUST be longer than gintRotatorTimer to avoid nasty flickering
gintRotatorFadeSpeed = 1.85;         // The fade in/out speed in general rotation mode.
gintRotatorSwitchFadeOutSpeed = 50;  // The fade out speed when 'next' or 'previous' is clicked.
gintRotatorSwitchFadeInSpeed = 10;   // The fade in speed when 'next' or 'previous' is clicked. 
gintRotatorSwitchNextOffset = 100;   // ?
var gstrRotatorElements = ''         // Comma separated list of IDs of page elements to rotate

// Load and start the rotator system using the comma separated list of rotator
// elements set up above. Alternatively the garrRotatorElements array can be
// manually populated and a call can be made directly to RotatorRotateElement()
function RotatorStart(intStartElement)
{
	arrRotatorElements = gstrRotatorElements.split(',');
	
	for (i = 0; i < garrRotatorElements.length; i++)
	{
		objRotatorElement = document.getElementById(arrRotatorElements[i]);
		objRotatorElement.style.display = 'block';
		garrRotatorElements[i] = new Fadomatic(objRotatorElement,gintRotatorFadeSpeed,0);
	}
	
	RotatorRotateElement(intStartElement);
}

function RotatorRotateElement(intFaderElementID,blnNow)
{
	if (!gintRotatorTimer || !gintRotatorNextOffset || !gintRotatorFadeSpeed)
	{
		gblnRotatorEnabled = false;
	}
	else
	{
		intShowNextTimer = gintRotatorTimer + gintRotatorNextOffset;
	}

	if (gblnRotatorEnabled)
	{
		garrRotatorElements[intFaderElementID]._rate = gintRotatorFadeSpeed;
		garrRotatorElements[intFaderElementID].fadeIn();

		setTimeout('RotatorHideElement(' + intFaderElementID + ')',gintRotatorTimer);

		intFaderElementID++;

		if (intFaderElementID == garrRotatorElements.length)
		{
			intFaderElementID = 0;
		}

		setTimeout('RotatorRotateElement(' + intFaderElementID + ')',intShowNextTimer);
	}
}

function RotatorHideElement(intFaderElementID,intSpeed)
{
	if (gblnRotatorEnabled)
	{
		if (!intSpeed)
		{
			if (gintRotatorFadeSpeed)
			{
				intSpeed = gintRotatorFadeSpeed;
			}
			else
			{
				intSpeed = 100; // Immediate
			}
		}

		garrRotatorElements[intFaderElementID]._rate = intSpeed;
		garrRotatorElements[intFaderElementID].fadeOut();
	}
}

function RotatorSwitchElement(intCurrentElementID,strBrowse)
{
	if (gintRotatorSwitchFadeOutSpeed && gintRotatorSwitchFadeInSpeed && gintRotatorSwitchNextOffset)
	{
		RotatorSwitchPlay('on');

		intCurrentElementID = eval(intCurrentElementID);
		gblnRotatorEnabled = true;

		garrRotatorElements[intCurrentElementID].haltFade();
		RotatorHideElement(intCurrentElementID,gintRotatorSwitchFadeOutSpeed);

		if (strBrowse == 'previous')
		{
			intCurrentElementID--;
		}
		else
		{
			intCurrentElementID++;
		}

		if (intCurrentElementID == garrRotatorElements.length)
		{
			intCurrentElementID = 0;
		}
		else if (intCurrentElementID < 0)
		{
			intCurrentElementID = garrRotatorElements.length - 1;
		}

		objFadeElement = document.getElementById(garrRotatorElements[intCurrentElementID]);
		garrRotatorElements[intCurrentElementID]._rate = gintRotatorSwitchFadeInSpeed;
		setTimeout('garrRotatorElements[' + intCurrentElementID + '].fadeIn();',gintRotatorSwitchNextOffset);

		gblnRotatorEnabled = false;

		if (gintRotatorRestartTimer && !gblnRotatorRestarting)
		{
			setTimeout('RotatorRestart(' + intCurrentElementID + ')',gintRotatorRestartTimer);
		}
	}
}

// Re-starts with the next project after the current project
function RotatorRestart(intCurrentElementID)
{
	gblnRotatorRestarting = true;
	
	if (gintRotatorNextOffset)
	{
		gblnRotatorEnabled = true;

		RotatorSwitchPlay('off');
		RotatorHideElement(intCurrentElementID,gintRotatorFadeSpeed);

		intFaderElementID = intCurrentElementID + 1;
		if (intFaderElementID == garrRotatorElements.length)
		{
			intFaderElementID = 0;
		}
		setTimeout('RotatorRotateElement(' + intFaderElementID + ',true)',gintRotatorNextOffset);
	}
}

// Re-starts from the original start project
function RotatorReset()
{
	RotatorSwitchPlay('off');
	for (i = 0; i < garrRotatorElements.length; i++)
	{
		garrRotatorElements[i]._rate = gintRotatorFadeSpeed;
		garrRotatorElements[i].fadeOut();
	}
	gblnRotatorEnabled = true;
	RotatorRotateElement(gintStartElement);
}

function RotatorSwitchPlay(strOnOff)
{
	arrPlayButtons = document.getElementsByName('playbutton');
	
	for (i = 0; i < arrPlayButtons.length; i++)
	{
		if (strOnOff == 'on')
		{
			document.getElementById('playbutton-' + i).style.display = 'inline';
		}
		else
		{
			document.getElementById('playbutton-' + i).style.display = 'none';
		}
	}

}

