/*
Script: mooCase-v1.1.js
	mooCase - mooCase version 1.1

License:
	MIT-style license.

Copyright:
	Copyright 2008
	Trebbers
	http://www.trebbers.nl

Based on MooTools v1.2.1 Core
	[The MooTools production team](http://mootools.net/developers/).

More information @ www.mootools.nl or http://mootools.trebbers.nl

Keep smiling ;)
*/

var mooCase = new Class({

	Implements: [Events, Options],

	options: {
		elements:'elements',
		togglers:'togglers',
		activeClassName:'active',
		timer:5,
		speed:5,
		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 * 1000, 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 * 1000, 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 || {}));
	}

});