Scroller = new Class({
	_items: [],
	_count: 0,
	_total: 0,
    _timeout_id: null,	
	
	initialize: function(element) {
	
		this._items = element.getElements('.item');
		this._total = this._items.length;
		
		//return if no child elements were found
		if(this._total == 0) return;

		// show first element	
		this.animate(this._items[this._count], 0);
				
		// refresh
	    element.getElement('.refresh').addEvent('click', function() {	    		    		    	
	    	this.refresh();  
	    	return false;		    	
	    }.bind(this));


		// auto scroller
		this.setTimeout(5000);
	    	            
	},
	
	setTimeout: function(duration) {
	
	    if (this._timeout_id) {
	        window.clearTimeout(this._timeout_id);
	    }	
	    
	    var callable = function() {
	        this.refresh();
	    }.bind(this);
	    
        this._timeout_id = window.setTimeout(callable, duration);	
	},	
	
	refresh: function() {	    
		this.animate(this._items[this._count], 1);		
		
		if (this._count+1 == this._total) {
			this._count = -1;
		}
		this.animate(this._items[this._count+1], 0);
    	this._count++;	
    	
		this.setTimeout(5000);	
	},
	
	animate: function(element, hide) {
		var myFx = new Fx.Tween(element);
		if (hide == 1) {
			myFx.start('opacity', 1, 0);
		} else {
			myFx.start('opacity', 0, 1);		
		}		
	}

});

document.addEvent('domready', function() {
	new Scroller($('payoff'));
});
