/*
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',
		text_elements:'text_elements',
		activeClassName:'active',
		timer:5,
		speed:10,
		transition:Fx.Transitions.Quart.easeOut,
		flag: 0
	},

	initialize: function(element, options, togglers){
		if($(element)){
			this.setOptions(options);
			this.elements = $$('#' + element + ' .' + this.options.elements + ' li');
			if($$('#' + element + ' .' + this.options.text_elements + ' li').length > 0) {
				this.text_elements = $$('#' + element + ' .' + this.options.text_elements + ' li');
				this.text_elements[0].setStyle('opacity', 1);
				if(this.elements[0]==null){
					this.log('Invalid elements ' + this.options.elements);
					return false;
				}
				this.flag = 1;
			}

			if(this.text_elements[0]==null){
				this.log('Invalid text_elements ' + this.options.text_elements);
				return false;
			}			

			this.elements.set('tween',{
				duration:(this.options.speed*100),
				transition:this.options.transition,
				link:'cancel'
			});
			
			if(this.flag) {
				this.text_elements.set('tween',{
					duration:(this.options.speed*100),
					transition:this.options.transition,
					link:'cancel'
				});
			}
			
			if(this.options.togglers) {
				this.togglers = $$('#' + element + ' .' + this.options.togglers + ' li a');
				if(this.elements[0]==null){
					this.log('Invalid togglers ' + this.options.togglers);
					return false;
				}

				if(this.text_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(){
		if(this.togglers) {
			this.togglers[this.current].getParent().addClass(this.options.activeClassName);
		}
		this.elements.each(function(el,i){
			if(i!=0) el.setStyle('opacity',0);
			el.addEvents({
				'mouseenter':function(){
					if(!this.togglers) $clear(periodical);
					$clear(periodical);
				}.bind(this),
				'mouseleave':function(){
					if(!this.togglers) periodical = this.slide.periodical(this.options.timer * 1000, this);
					periodical = this.slide.periodical(this.options.timer * 1000, this);
				}.bind(this)
			});
		}.bind(this));
		if(this.flag) {
			this.text_elements.each(function(el,i){
				/*if(i!=0) el.setStyle('opacity',0);*/
			});		
		}
		if(!this.togglers) var periodical = this.slide.periodical(this.options.timer * 1000, this);
		var periodical = this.slide.periodical(this.options.timer * 1000, this);
		if(this.togglers) {
			this.togglers.each(function(el,i){
				el.addEvents({
					'mouseenter':function(){
						if(!this.togglers) $clear(periodical);
						$clear(periodical);
						if(i != this.current) {
							this.fader(this.elements[this.current],0);
							if(this.flag) {
								/*this.fader(this.text_elements[this.current],0);*/
							}
							this.togglers[this.current].getParent().removeClass(this.options.activeClassName);
							this.fader(this.elements[i],1);
							if(this.flag) {
								/*this.fader(this.text_elements[i],1);*/
							}
							this.togglers[i].getParent().addClass(this.options.activeClassName);
							this.current = i;
						}
					}.bind(this),
					'mouseleave':function(){
						if(!this.togglers) periodical = this.slide.periodical(this.options.timer * 1000, this);
						periodical = this.slide.periodical(this.options.timer * 1000, this);
					}.bind(this)
				});
			}.bind(this));
		}
	},
	
	slide: function(){
		this.fader(this.elements[this.current],0);
		if(this.flag) {
			/*this.fader(this.text_elements[this.current],0);*/
		}
		if(this.togglers) {
			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);
		if(this.flag) {
			/*this.fader(this.text_elements[this.current],1);*/
		}
		
		if(this.togglers) {
			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 || {}));
	}

});
