/*  _____             _____   _____
 * |  _  \           |___  | |___  |
 * | | | |              _| |    _| |
 * | | | |  o  o  o    |_  {   |_  {
 * | |_| |            ___| |  ___| |
 * |_____/           |_____/ |_____/
 *
 * Copyright (C) D33 Internet GmbH 2010
 * All rights reserved
 */

/**
 * Class to fade elements
 * 
 * Constructor of fader
 * @param items array of nodes to fade
 * @param params array
 * 	duration - fade time default 1.5
 * 	timeout - time to wait between fades default 1500
 */
function Fader( items, params )
{
	this.items = items;
	this.current = -1;
	this.duration = params['duration'] ? params['duration'] : 1.5;
	this.timeout = params['timeout'] ? params['timeout'] : 1500; 
}

/**
 * Start the fader
 */
Fader.prototype.run = function()
{
	this.current = 0;
	this.currentItem = this.items[0];
	for( var x=0; x<this.items.length; x++ )
	{
		if( x > 0 )
		{
			this.items[x].style.opacity='0.0';
			this.items[x].style.filter = "alpha(opacity:0)";
		}
		this.items[x].style.position='absolute';
	}
	var fader = this;
	setTimeout( function() { fader.nextItemIn(); }, this.timeout );	
}

Fader.prototype.nextItemIn= function()
{
	var theFader = this;
	var item = this.nextItem();
	if( theFader.currentItem )
	{
		new Effect.Opacity(theFader.currentItem, 
					{
						duration:this.duration,
						from:1.0,
						to:0.0,
						afterFinish : function()
						{
							theFader.currentItem.style.zIndex = 1;
						}
					});
	}

	new Effect.Opacity(item, {duration:this.duration, from:0, to:1.0,afterFinish :
		function()
		{ 
			theFader.currentItem = item;
			item.style.zIndex = 2;
			setTimeout( function() { theFader.nextItemIn() }, theFader.timeout );
	 	}});
}

Fader.prototype.reset = function()
{
	this.current = -1;
}

/**
 * Get next item of list
 */
Fader.prototype.nextItem = function()
{
	this.current++;
	if( this.items[this.current] )
	{
		return this.items[this.current];
	}
	else
	{
		this.reset();
		this.current++;
		return this.items[this.current];
	} 
}

