function Carousel( oArgument )
{
	this.init( oArgument );
}

Carousel.prototype.init = function( oArgument )
{

	if ( typeof oArgument == "undefined" || typeof oArgument.id == "undefined" )
		return false;

	window[ ( this._self = "$_oCarousel" + oArgument.id ) ] = this;

	this._wrapper = document.getElementById( oArgument.id );
	this._nodename = typeof oArgument.nodename == "undefined" ? "img" : oArgument.nodename;

	this._step = typeof oArgument.step == "undefined" ? 10 : oArgument.step;
	this._delay = typeof oArgument.delay == "undefined" ? 8000 : oArgument.delay;
	this._animatedelay = typeof oArgument.animatedelay == "undefined" ? 200 : oArgument.animatedelay;
	this._timer = null;

	this._item = this._wrapper.getElementsByTagName( this._nodename );

	this._current = this._initCurrent();
	if ( this._item.length > 0 )
		this.run();
};

Carousel.prototype.run = function()
{
	this._display();
	this._timer = setTimeout( this._self + ".run()", this._delay );
};

Carousel.prototype._initCurrent = function()
{
	var nItem = this._item.length;
	while ( nItem-- > 0 )
		if ( this._item[ nItem ].className.match( /\s?(stacktop)/gi ) )
			return nItem;

	return 0;
}


Carousel.prototype._display = function()
{
	clearTimeout( this._timer );

	var nItem = this._item.length;
	while ( nItem-- > 0 )
	{
		var nIndex = this._item.length - ( ( this._item.length + ( nItem - this._current ) ) % this._item.length ); 
		this._item[ nItem ].style.zIndex = nIndex;

		if ( nItem != this._current )
		{
			var oDynamic = new Keen.dynamic( this._item[ nItem ] );
			oDynamic.setAlpha( 100 );
		}
	}

	this._currentstep = 0;
	this._animate( true );

	nItem = null;
	oAnimate = null;
};

Carousel.prototype._animate = function( bDisplay )
{
	clearTimeout( this._timer );

	if ( bDisplay )
	{
		if ( ++this._currentstep <= this._step )
		{
			var oAnimate = new Keen.animation( this._item[ this._current ] );
			var nAlpha = 100 - ( ( 100 / this._step ) * this._currentstep );
			oAnimate.fade( nAlpha );
			
			this._timer = setTimeout( this._self + "._animate( true );", this._animatedelay );
		}
		else
		{
			this._timer = setTimeout( this._self + "._animate( false );", this._delay );
		}
	}
	else
	{
		this._currentstep = 0;
		this._current++;
		if ( this._current >= this._item.length )
			this._current = 0;

		this._timer = setTimeout( this._self + "._display();", this._animatedelay );
	}
};
