﻿var rotatorItems = document.getElementById('rotator').children;

var gCurrentItem = 0;
var gFaderClearArray = new Array();
var gCancelInterval;


InitializeRotator();


function InitializeRotator(rotateInterval)
{
	if (rotateInterval == null)
		rotateInterval = 7000;					// defaulting the interval to 7 seconds

	for (var index = 0; index < rotatorItems.length; index++)
	{
		if (rotatorItems[index].nodeName != 'DIV')
			rotatorItems.splice(index, 1);
	}

	for (counter = 0; counter < rotatorItems.length; counter++)
		rotatorItems[counter].style.display = 'none';


	gCancelInterval = window.setInterval("RotateRight();", rotateInterval);
	ChangeImage(0);
}

function ChangeImage(indexNumber)
{
	gCurrentItem = indexNumber;

	for (counter = 0; counter < rotatorItems.length; counter++)
	{
		if (rotatorItems[counter].style.display != 'none')
			Fader(rotatorItems[counter].id, 400, 100, 0, 25, 0);
	}

	Fader(rotatorItems[gCurrentItem].id, 400, 0, 100, 25, 0);
}

function RotateRight()
{
	gCurrentItem++;
	if (gCurrentItem == rotatorItems.length) gCurrentItem = 0;
	
	ChangeImage(gCurrentItem);
}




// objectID				- the ID of the object to be faded
// duration				- total duration of event in milliseconds (should be a multiple of frequency)
// startingOpacity		- the opacity that it will start on
// endingOpacity		- the opacity that it will end on
// frequency			- how often the event will occur
function Fader(objectID, duration, startingOpacity, endingOpacity, frequency, delay)
{
	window.setTimeout('SetOpacity(document.getElementById("' + objectID + '"),' + startingOpacity + ');', delay);
	var counter = 0;
	var differenceOpacity = endingOpacity - startingOpacity;
	while (frequency * counter < duration)
	{
		counter++;
		gFaderClearArray[counter - 1] = window.setTimeout('SetOpacity(document.getElementById("' + objectID + '"), "' + (startingOpacity + ((frequency * counter / duration) / (1 / differenceOpacity))) + '");', counter * frequency + delay);
	}
}

function SetOpacity(object, value)
{
	if (value == 0) object.style.display = 'none';
	else object.style.display = 'block';

	object.style.opacity = value / 100;
	object.style.filter = 'alpha(opacity=' + value + ')';
}