function Fader(duration) 
{
	// Add the object to the window
	if (!window.faders) window.faders = new Array();
	this.id = window.faders.length;
	window.faders[this.id] = this;

	// Public Properties
	this.Duration = duration;
	
	// Internal properties
	this.elements = new Array();
	this.durations = new Array();
	this.currentElement = 0;
	this.cascade = new Array();
	
	// Methods
	this.Add = faderAdd; // Add an element to the fader queue
	this.Random = faderRandom; // Move to random element in the sequence
	this.Cascade = faderCascade; // Add a fader cascade to this
}

// Add element to fader cycle. duration is optional
function faderAdd(elementName,duration)
{
	var elementIndex = this.elements.length;
	var elementObject = null;
	if (document.getElementById)
		elementObject=document.getElementById(elementName);
	else if (document.all)
		elementObject=document.all[elementName];

	// Add the new element and duration.
	this.elements[elementIndex] = elementObject;
	this.durations[elementIndex] = (duration)?duration:this.Duration;
	
	// hide all but this first element added to the array
	elementObject.style.zIndex = (elementIndex>0)?0:2;
	
	// Start the timer once we have more than one.
	//if (elementIndex > 1)
		faderSwap(this.id,this.currentElement);
}

function faderRandom()
{
	// Chuck it if we don't have anything to select yet.
	if (this.elements.length < 2) return;
	
	this.elements[this.currentElement].style.zIndex=0;
	this.currentElement = Math.round(Math.random()*(this.elements.length)-.5);
	this.elements[this.currentElement].style.zIndex=2;

	faderSwap(this.id,this.currentElement);
}

function faderCascade(faderObject)
{
	faderObject.cascade[faderObject.cascade.length] = this.id;
}

function faderSwap(faderId,targetElement,duration)
{
	var faderObject = window.faders[faderId];
	var cascade = false;

	// Make sure we destroy the old timer.
	if (faderObject.timer) clearTimeout(faderObject.timer);

	if (targetElement != faderObject.currentElement)
	{
		var current = faderObject.elements[faderObject.currentElement];
		var target = faderObject.elements[targetElement];
		
		// If this is the first time then prepare the new element.
		if (target.style.zIndex<2)
		{
			// Move all the elements back before promoting the two involved in the fader.
			for (var i=0;i<faderObject.elements.length;i++)
				faderObject.elements[i].style.zIndex=0;

			current.style.zIndex=1;
			target.style.zIndex=2;
			if (target.filters)
				target.filters.item("DXImageTransform.Microsoft.Alpha").Opacity=0;
			else
				target.style.opacity=0;
		}

		// Increment the opacity. If it's 75% then trigger a cascade, if it's 100% then stop
		if (target.filters)
		{
			target.filters.item("DXImageTransform.Microsoft.Alpha").Opacity+=5;
			if (target.filters.item("DXImageTransform.Microsoft.Alpha").Opacity == 100)
				faderObject.currentElement = targetElement;
			if (target.filters.item("DXImageTransform.Microsoft.Alpha").Opacity == 75)
				cascade = true;
		}
		else // and again for non IE.
		{
			target.style.opacity = parseFloat(target.style.opacity) + 0.05;
			if (target.style.opacity == 1)
				faderObject.currentElement = targetElement;
			if (target.style.opacity == .75)
				cascade = true;
		}
		
		// Loop again on the timer for the next fade step.
		faderObject.timer = setTimeout('faderSwap('+faderId+','+targetElement+');', 20);

		// If we triggered a cascade and there's one set then fire it off.
		if (cascade && faderObject.cascade.length>0)
			for (var i=0; i<faderObject.cascade.length; i++)
			{
				var temp = window.faders[faderObject.cascade[i]];
				faderSwap(temp.id, temp.currentElement, 1);
			}
	}
	else
		// Set the timer for the next fade.
		faderObject.timer = setTimeout(
			'faderSwap('+faderId+','+((targetElement==0)?faderObject.elements.length-1:targetElement-1)+');',
			(duration)?duration:faderObject.durations[targetElement]*1000
		);
}