/*
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',
		tocontent:'',
		toTextGroup:'homeTextGroup',
		activeTextGroupBG:true,
		activeClassName:'active',
		timer:10,
		speed:20,
		transition:Fx.Transitions.Quart.easeOut
	},

	initialize: function(element,options){
		if($(element)){
			this.setOptions(options);
			this.elements = $$('#' + element + ' .' + this.options.elements + ' li');
			if(this.options.toTextGroup!=""){
				this.elementTextGroup = $$('#' + element + ' .' + this.options.elements + ' li div.'+this.options.toTextGroup);
				if(this.options.activeTextGroupBG==true){
					this.elementTextGroupBG = $$('#' + element + ' .' + this.options.elements + ' li div.'+this.options.toTextGroup+"_bg");
				}else{
					this.elementTextGroupBG=null;
				}
			}else{
				this.elementTextGroup=null;	
				this.elementTextGroupBG=null;
			}
		 
			if(this.options.tocontent!=""){
				this.contents = $$( this.options.tocontent );
			}else{
				this.contents=null;	
			}
			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.elementTextGroup.set('tween',{
				duration:(10*100),
				transition:this.options.transition,
				link:'cancel'
			});
			if(this.options.activeTextGroupBG==true){
			this.elementTextGroupBG.set('tween',{
				duration:(10*100),
				transition:this.options.transition,
				link:'cancel'
			});
			}
			
			this.togglers = $$(' .' + 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');
		}
	},
	
	setRightInfor:function(title,desc){
		var RightInfor_Title	=	$$("#banner_middle_news h3.travil_title");
		var RightInfor_content	=	$$("#banner_middle_news div.travil_content");
		RightInfor_Title.empty();
		RightInfor_content.empty();
		 
		
		
	},
	
	start: function(){
		this.togglers[this.current].getParent().addClass(this.options.activeClassName);
		this.elements.each(function(el,i){
			if(i!=0) el.setStyle('opacity',0);
		});
		if(this.contents!=null){
			this.contents.each(function(el,i){
				if(i!=0) el.setStyle('opacity',0);
			});
		}
		var my=this;
		if(this.elementTextGroup!=null){
				this.elementTextGroup.each(function(el,i){
				if(i!=0){				 
					my.faderGroup(el,0,-100);
				}else{					 
						my.faderGroup(el,0.7,0);
				}
			});
		}
		
		if(this.elementTextGroupBG!=null){
				this.elementTextGroupBG.each(function(el,i){
					if(i!=0){				 
						my.faderGroup(el,0,-100);
					}else{					 
						my.faderGroup(el,0.7,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.faderItemGroup(this.current,0,-100);
						if(this.contents!=null){this.fader(this.contents[this.current],0);this.fader(this.contents[i],1);}
						this.togglers[this.current].getParent().removeClass(this.options.activeClassName);
						this.fader(this.elements[i],1);
						this.faderItemGroup(i,0.7,0);						
						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.faderItemGroup(this.current,0,-100);
	 
		if(this.contents!=null){this.fader(this.contents[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.faderItemGroup(this.current,0.7,0);
		if(this.contents!=null){
				this.fader(this.contents[this.current],1);
				
				 
			}
		this.togglers[this.current].getParent().addClass(this.options.activeClassName);
	},
	faderItemGroup:function(kitem,opacity,bottom){
		
		this.faderGroup( this.elementTextGroup[kitem],opacity,bottom);	
		if(this.options.activeTextGroupBG==true){
		this.faderGroup( this.elementTextGroupBG[kitem],opacity,bottom);	
		}
	 
	},
	faderGroup:function(element,opacity,bottom){
		//element.get('tween').start('opacity',opacity);
		//element.get('tween').start('bottom',bottom);
		//element.setStyle('opacity',opacity);
		element.setStyle('bottom',bottom);
	},
	fader: function(element,opacity){
		element.get('tween').start('opacity',opacity);
	},
	
	log: function(text, args) {
		if (window.console) console.log(text.substitute(args || {}));
	}

});