function FPRotator( sId, sSelectedClass, iDelay ){
	this.sId = sId;
	this.aItems = new Array();
	this.iCurrent = 0;
	this.isRunning = false;
	this.sSelectedClass = sSelectedClass;
	this.iDelay = iDelay;

	this.iFirst		= 0;
	this.iStepPx	= 65;
	this.iVisible	= 3;
}

FPRotator.prototype.add = function( sId ){
	this.aItems.push( sId );
}

FPRotator.prototype.start = function(){
	this.isRunning = true;
	var _owner = this;
	this.timeout=setTimeout( function(){rotate(_owner)} , _owner.iDelay);
}

FPRotator.prototype.stop = function(){
	clearTimeout( this.timeout );
	this.isRunning = false;
}

FPRotator.prototype.go = function( iCurrent ){
	this.show( iCurrent );
	this.stop();
}

FPRotator.prototype.show = function( iCurrent ){

	if ( iCurrent<0 || ( iCurrent>=this.aItems.length) ) iCurrent = 0;
	_item = document.getElementById( this.aItems[this.iCurrent] );

	if ( null != _item ){
		_item.style.display = 'none';
	}

	_item_index = document.getElementById( this.aItems[this.iCurrent] + '_index' );
	if ( null != _item_index ){
		_item_index.className='';
	}

	_item = document.getElementById( this.aItems[iCurrent ] );
	if ( null != _item ){
		_item.style.display = 'block';
	}

	_item_index = document.getElementById( this.aItems[iCurrent] + '_index' );
	if ( null != _item_index ){
		_item_index.className=this.sSelectedClass;
	}

	this.iCurrent = iCurrent;

}

FPRotator.prototype.check_visible = function( iCurrent ){
	return ( iCurrent >= this.iFirst ) && ( iCurrent < ( this.iFirst + this.iVisible ) );
}

FPRotator.prototype.scrollTo = function( iCurrent ){

	var _owner = this;

	_container = $('#' + 'masca');
	this.iFirst = iCurrent;
	_offset = this.iFirst*this.iStepPx;

	_container.scrollTo( _offset, 500, {
			axis:'y',
			easing:'easeOutQuart',
			onAfter:function(){
				_owner.start();
				
			}
	});
	
	if ( this.iFirst == 0 ) {
		document.getElementById('buttonsUpSpan').className = 'disabled';
	} else {
		document.getElementById('buttonsUpSpan').className = 'enabled';
	}
	
	if ( ( this.iFirst + this.iVisible) >= this.aItems.length ) {
		document.getElementById('buttonsDownSpan').className = 'disabled';
	} else {
		document.getElementById('buttonsDownSpan').className = 'enabled';
	}	
	

}

FPRotator.prototype.scroll = function( iDirection ){

	if ( iDirection > 0 ){
		iFirst = this.iFirst + this.iVisible;
		if ( iFirst>=this.aItems.length ) return;
	}else{
		iFirst = this.iFirst - this.iVisible;
		if ( iFirst<0 ) return;
	}

	this.iFirst = iFirst;

	this.stop();
	this.scrollTo( this.iFirst );
	this.show( this.iFirst );

}


function rotate( _owner ){

	if ( !_owner.isRunning ) return;
	iNewCurrent = _owner.iCurrent+1;
	if ( iNewCurrent == _owner.aItems.length ) iNewCurrent = 0;

	if ( _owner.check_visible( iNewCurrent ) ){
		_owner.show( iNewCurrent );
	}else{
		_owner.stop();
		_owner.scrollTo( iNewCurrent );
		_owner.show( iNewCurrent );
	}


	clearTimeout( _owner.timeout );
	_owner.timeout=setTimeout( function(){rotate(_owner)} , _owner.iDelay );

}

jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};
