var Menu = new Class({
	initialize: function(menu){
		this.menu = $(menu).getChildren('li');
		this.menu.each(function(item){
			if (item.hasClass('active')){
				if (item.getPrevious('li')) item.getPrevious('li').addClass('previous');
				if (item.getNext('li')) item.getNext('li').addClass('next');
			}
		});
	}
});

var Projects = new Class({
	Implements: [Options],
	options: {
		'delay'		: 5000
	},
	initialize: function(items,trigger,options){
		this.setOptions(options);
		this.length 	= items.length;
		this.items		= items;
		this.previous	= 0;
		this.current 	= 0;
		var periodical;
		if (this.length != 1) periodical = this.next.periodical(this.options.delay,this);
		trigger.addEvents({
			'mouseenter': function(){
				$clear(periodical);
			},
			'mouseleave': function(){
				periodical = this.next.periodical(this.options.delay,this);
			}.bind(this),
			'click': function(){
				window.location = this.items[this.current].getElement('a').get('href');
			}.bind(this)
		});
		this.next(0);
	},
	next: function(){
		if (this.current == this.length) this.current = 0;
		if (this.current != 0) { this.previous = this.current - 1; } else { this.previous = this.length - 1; }
		this.items[this.previous].set('styles',{'z-index':0});
		this.items[this.current].set('styles',{'z-index':1}).fade('hide').removeClass('hidden');
		this.items[this.current].fade('in');
		(function(){ this.items[this.previous].addClass('hidden'); }).delay(1000,this);
		(function(){ this.current++ }).delay(this.options.delay - 100,this);
	}
});

window.addEvent('domready',function(){
	var menu = new Menu('menu');
	var projects = new Projects($$('.reference-item'),$('reference-trigger'),{
		delay: 3500
	});
});
