function Carousel( oArgument )
{
	this.init( oArgument );
}

Carousel.prototype.init = function( oArgument )
{
	if ( typeof oArgument == "undefined" )
		oArgument = new Object;

	window[ ( this._self = "oCarousel" + typeof oArgument.id != "undefined" ? oArgument.id : "" ) ] = this;

	this._wrapper = document.getElementById( oArgument.id );
	this._nodename = typeof oArgument.nodename == "undefined" ? "img" : oArgument.nodename;

	this._step = typeof oArgument.step == "undefined" ? 0 : oArgument.step;
	this._delay = typeof oArgument.delay == "undefined" ? 3500 : oArgument.delay;
	this._animatedelay = typeof oArgument.animatedelay == "undefined" ? 35 : oArgument.animatedelay;
	this._start = typeof oArgument.start == "undefined" ? 0 : oArgument.start;
	this._end = typeof oArgument.end == "undefined" ? 50 : oArgument.end;
	this._timer = null;

	this._item = this._wrapper.getElementsByTagName( this._nodename );

	this._path = this._calculatePath( this._start, this._end );

	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._calculatePath = function( nFrom, nTo )
{
	var aReturn = new Array();

	var nItem = 0;
	while ( nItem++ < this._step)
		aReturn.push( Math.round( Keen.movement.smooth( nFrom, nTo, nItem, this._step ) ) );

	return aReturn;
};

Carousel.prototype._initCurrent = function()
{
	var nItem = this._item.length;
	while ( nItem-- > 0 )
		if ( this._item[ nItem ].className.match( /\s?(show)/gi ) )
			return nItem;

	return 0;
}


Carousel.prototype._display = function()
{
	clearTimeout( this._timer );

	var nItem = this._item.length;
	while ( nItem-- > 0 )
	{
		this._item[ nItem ].className = this._item[ nItem ].className.replace( /\s?(show)/gi, '' );
		this._item[ nItem ].className = this._item[ nItem ].className.replace( /\s?(hide)/gi, '' );

		if ( nItem != this._current )
			this._item[ nItem ].className = " hide";
	}

	this._currentstep = 0;
	this._item[ this._current ].className = " show";
	this._animate( true );

	nItem = null;
	oAnimate = null;
};

Carousel.prototype._animate = function( bDisplay )
{
	clearTimeout( this._timer );

	var oAnimate = this._item[ this._current ];

	if ( bDisplay )
	{
		if ( ++this._currentstep < this._path.length )
		{
			oAnimate.style.top = this._path[ this._currentstep ] + "px";
			this._timer = setTimeout( this._self + "._animate( true );", this._animatedelay );
		}
		else
		{
			this._timer = setTimeout( this._self + "._animate( false );", this._delay );
		}
	}
	else
	{
		if ( --this._currentstep > 0 )
		{
			oAnimate.style.top = this._path[ this._currentstep ] + "px";
			this._timer = setTimeout( this._self + "._animate( false );", this._animatedelay );
		}
		else
		{
			this._current++;
			if ( this._current >= this._item.length )
				this._current = 0;

			this._timer = setTimeout( this._self + "._display( true );", this._animatedelay );
		}
	}
};
