 var mooCase = new Class({

	Implements: [Events, Options],

	options: {
		elements:			'elements',
		togglers:			'togglers',
		activeClassName:	'active',
		timer:				5,
		speed:				10,
		transition:			Fx.Transitions.Quart.easeOut
	},

	initialize: function(element,options){
		if($(element)){
			this.setOptions(options);
			this.elements = $$('#' + element + ' .' + this.options.elements + ' li');
			if(this.elements[0]==null){
				this.log('Invalid elements ' + this.options.elements);
				return false;
			}
			this.elements.set('tween',{
				duration:(this.options.speed*100),
				transition:this.options.transition,
				link:'cancel'
			});
			this.togglers = $$('#' + element + ' .' + this.options.togglers + ' li a');
			if(this.elements[0]==null){
				this.log('Invalid togglers ' + this.options.togglers);
				return false;
			}
			this.current = 0;
			this.start();
		} else {
			this.log('The container ' + element + ' does not exist');
		}
	},
	
	start: function(){
		this.togglers[this.current].getParent().addClass(this.options.activeClassName);
		this.elements.each(function(el,i){
			if(i!=0) el.setStyle('opacity',0);
		});
		var periodical = this.slide.periodical(this.options.timer * 2000, this);
		this.togglers.each(function(el,i){
			el.addEvents({
				'mouseenter':function(){
					$clear(periodical);
					if(i != this.current) {
						this.fader(this.elements[this.current],0);
						this.togglers[this.current].getParent().removeClass(this.options.activeClassName);
						this.fader(this.elements[i],1);
						this.togglers[i].getParent().addClass(this.options.activeClassName);
						this.current = i;
					}
				}.bind(this),
				'mouseleave':function(){
					periodical = this.slide.periodical(this.options.timer * 2000, this);
				}.bind(this)
			});
		}.bind(this));
	},
	
	slide: function(){
		this.fader(this.elements[this.current],0);
		this.togglers[this.current].getParent().removeClass(this.options.activeClassName);
		if(this.elements[this.current] == this.elements.getLast()) {
			this.current = 0;
		} else {
			this.current++;
		}
		this.fader(this.elements[this.current],1);
		this.togglers[this.current].getParent().addClass(this.options.activeClassName);
	},
	
	fader: function(element,opacity){
		element.get('tween').start('opacity',opacity);
	},
	
	log: function(text, args) {
		if (window.console) console.log(text.substitute(args || {}));
	}

});
