﻿var containerName = "FieldContainer";
var slideName = "Slide";

var rotationPauseTime = 5000;
var backgoundPauseTime = 1000;
var framePauseTime = 50;
var opacityInterval = 10;

var containers = new Array();
var currentContainerIndex = 0;
var pauseFading = false;

function body_load()
{
	setupContainers();
	setTimeout("fade(false, 100);", rotationPauseTime);
}

function setupContainers()
{
	containers = new Array();
	currentContainerIndex = 0;
	var divContainers = getElementsByName_ieFix("div", containerName);
	for (var pos = 0; pos < divContainers.length; pos++)
	{
		var container = divContainers[pos];
		containers[pos] = new Object();
		containers[pos].Container = container;
		containers[pos].CurrentIndex = 0;
		containers[pos].Divs = new Array();
		containers[pos].Divs = getElementsByName_ieFix("div", slideName);
		if (containers[pos].Divs.length > 0)
		{
			// Show the first panel
			changeOpacity(pos, 0, 100);

			// Hide the rest
			for (var divIndex = 1; divIndex < containers[pos].Divs.length; divIndex++)
			{
				changeOpacity(pos, divIndex, 0);
			}
		}
	}
}

function changeOpacity(containerIndex, divIndex, opacity)
{
	var container = containers[containerIndex];
	var div = container.Divs[divIndex];
	div.style.opacity = opacity / 100;
	div.style.filter = "alpha(opacity=" + opacity + ")";

	if (opacity == 0)
	{
		div.style.visibility = "hidden";
		div.style.display = "none";
	}
	else if (opacity == 100)
	{
		div.style.visibility = "visible";
		div.style.display = "block";
	}
}

function getNextIndex(array, currentIndex)
{
	var nextIndex = 0;
	if (array.length > (currentIndex + 1))
	{
		nextIndex = currentIndex + 1;
	}
	return nextIndex;
}

function fade(fadeIn, opacity)
{
	if (pauseFading && !fadeIn && opacity >= 100)
	{
		return;
	}
	
	if (opacity < 0)
	{
		opacity = 0;
	}
	else if (opacity > 100)
	{
		opacity = 100;
	}

	var container = containers[currentContainerIndex];
	var div = container.Divs[container.CurrentIndex];
	
	if (fadeIn && opacity <= opacityInterval)
	{
		div.style.visibility = "visible";
		div.style.display = "block";
	}

	div.style.opacity = opacity / 100;
	div.style.filter = "alpha(opacity=" + opacity + ")";

	if (fadeIn)
	{
		if (opacity >= 100)
		{
			currentContainerIndex = getNextIndex(containers, currentContainerIndex);
			setTimeout("fade(false, 100);", rotationPauseTime);
		}
		else
		{
			setTimeout("fade(true, " + (opacity + opacityInterval) + ");", framePauseTime);
		}
	}
	else
	{
		if (opacity <= 0)
		{
			div.style.visibility = "hidden";
			div.style.display = "none";

			container.CurrentIndex = getNextIndex(container.Divs, container.CurrentIndex);
			setTimeout("fade(true, 0);", backgoundPauseTime);
		}
		else
		{
			setTimeout("fade(false, " + (opacity - opacityInterval) + ");", framePauseTime);
		}
	}
}

function getElementsByName_ieFix(tag, name)
{
	var tags = document.getElementsByTagName(tag);
	var elements = new Array();
	for (var tagPos = 0, elementPos = 0; tagPos < tags.length; tagPos++)
	{
		var tag = tags[tagPos];
		var tagName = tag.getAttribute("name");
		if (tagName == name)
		{
			elements[elementPos] = tag;
			elementPos++;
		}
	}
	return elements;
}
