/* 
 * Hangar Bicocca
 *
 * @package		HB
 * @author		Oskar Krawczyk (o.krawczyk@keepthinking.it)
 * @version		1.2
 * @dependecies	MooTools 1.2+
 * @copyright	Copyright (c) 2009-2010, Oskar Krawczyk (Keepthinking Ltd.)		
 * @link		http://keepthinking.it
 * 
 ======================================================================= */

var Rotor = new Class({
	Implements: [Events, Options],
	
	options: {
		delay: 4000
	},
	
	counter: 0,
	
	initialize: function(options) {
		this.setOptions(options);
		
		this.rotatorCont = $('home-rotor');
		this.rotatorItems = $$('.hrItem');
		this.maxIndex = this.rotatorItems.length-1;
		this.timer = $empty;
				
		if (!this.rotatorCont || this.rotatorItems.length <= 1) return false;
		
		this.timer = this.increment.periodical(this.options.delay, this);

		this.manage();
	},
	
	manage: function(){
		this.rotatorCont.addEvents({
			mouseenter: function(){
				this.timer = $clear(this.timer);
			}.bind(this),
			
			mouseleave: function(){
				this.timer = this.increment.periodical(this.options.delay, this);
			}.bind(this)
		});
	},
	
	increment: function(){
		this.rotatorItems.set('tween', {
			duration: 400,
			link: 'ignore'
		}).fade('out');
		
		this.rotatorItems[this.counter].set('tween', {
			duration: 400,
			link: 'ignore'
		}).fade('in');

		if (this.counter === this.maxIndex){
			this.counter = -1;
		}

		this.counter = ++this.counter;
	}
});
