/**
 * TABS

 * USAGE EXAMPLE:
 *  
 * 	new Tabs('a.handle','ul.elems', {
 * 		debug:true,
	 	startIndex:0,
		activeClass:'active'
 * 	})
 *
 *
 * @version		1.3
 *
 * @author		Phil Pérusse <pperusse [at] tqs [dot] ca>
 * @copyright	Author
 *
 * @Change log 1.3 (2010-03-23):
 * 		Bug fixes : 
 *		1 - Added Line : 
		 	if($type(this.elems)!='element' || $type(this.handles)!='element') return;
			to make sure element exists
 * 		Features: 		
 		1 - Add fade between elements
		2 - Add auto-cycler
 * @Change log 1.2:
 * 		Bug fixes : 
 *			none (!)
 * 		Features: 		
 		1 - Add Google Analytics call, on demand
		#Todo : 2 - Pogner le hash et cliquer pour.
 *
 
 * @Options:
 	startIndex:0, //On which tab to start.
	cycle:false, //Cycle : if we get to the last tab, next will send us to the first
	debug:false, //Debugging mode w/login
	activeClass:'active', //Class the handle will have when active
	onInit:Class.empty, //Fucntion call
	onChange:Class.empty, //Function call
	gaCall:false, //Call Google Analytics
	useHash:false, //Not implemented v1.3.1
	hashFlg:'tb', //Not implemented v1.3.1
	cyleTime:5000, //Cylce time between
	useCycler:false, //Use the auto-cycler
	useFader:false //Fade between elements (needs absolute positioning!!!)
 
 */
var Tabs = new Class({
	Implements:[Options,Events],
	options: {
		startIndex:0,
		cycle:false,
		debug:false,
		activeClass:'active',
		onInit:Class.empty,
		onChange:Class.empty,
		gaCall:false,
		useHash:false,
		hashFlg:'tb',
		cyleTime:5000,
		useCycler:false,
		useFader:false,
		otherHandles:null
	},
	initialize: function(handles,elems,options){
		this.log('Tabs initialized');
		this.setOptions(options);
		this.handles=$$(handles) || handles;
		this.elems=$$(elems) || elems;
		//if($type(this.elems)!='element' || $type(this.handles)!='element') return;
		this.log('Number of elements : '+this.elems.length);
		this.log('Number of handles : '+this.handles.length);
		this.log(this.options);
		this.options.useFader?this.elems.fade('hide'):this.elems.setStyle('display','none');
		
		this.handles.each(function(el,i){
			el.addEvent('click',function(e){
				e.preventDefault();
				this.options.useCycler=false;
				this.activate(i);
				this.makeGaCall(i);
			}.bind(this));
		}.bind(this));
		this.log(this.options.otherHandles);
		if($type(this.options.otherHandles)=="array"){
			this.log("Other Handles : "+this.options.otherHandles);
			this.options.otherHandles.each(function(el,i){
				el.addEvent('click',function(e){
					e.preventDefault();
					//this.handles[i].fireEvent('click');
					this.activate(i);
				}.bind(this));
			}.bind(this));
		}
		
		this.timer=null;
		this.currentIndex = this.options.startIndex;
		this.log('Start Index : '+this.options.startIndex);
		this.activate(this.options.startIndex);
		this.fireEvent('onInit');
		
	},
	activate:function(i){
		this.timer=$clear(this.timer);
		if(i>this.handles.length-1)return this.activate(this.handles.length-1);
		this.log('Activating : '+i);
		if($type(this.options.otherHandles)=="array"){
			this.options.otherHandles[this.currentIndex].removeClass(this.options.activeClass);
			this.options.otherHandles[i].addClass(this.options.activeClass);
		}
		this.handles[this.currentIndex].removeClass(this.options.activeClass);
		this.handles[i].addClass(this.options.activeClass);
		this.options.useFader?this.elems[this.currentIndex].fade('out'):this.elems[this.currentIndex].setStyle('display','none');
		this.options.useFader?this.elems[i].fade('in'):this.elems[i].setStyle('display','block');
		this.currentIndex=i;
		this.fireEvent('onChange',i);
		if(this.options.useCycler)this.timer=this.next.delay(this.options.cyleTime,this);
	},
	next:function(){		
		this.log('Next : '+Math.min(this.currentIndex.toInt()+1,this.handles.length-1));
		if(this.currentIndex.toInt()+1>this.handles.length-1 && this.options.cycle)		
			this.activate(0);
		else
			this.activate(Math.min(this.currentIndex.toInt()+1,this.handles.length-1));
	},
	previous:function(){
		this.log('Previous : '+Math.max(this.currentIndex.toInt()-1,0));		
		if(this.currentIndex.toInt()-1<0 && this.options.cycle)		
			this.activate(this.handles.length-1);
		else
			this.activate(Math.max(this.currentIndex.toInt()-1,0));
	},
	getIndex:function(){
		this.log('Current : '+this.currentIndex);
		return this.currentIndex;
	},
	last:function(){
		this.activate(this.handles.length-1);
	},
	makeGaCall:function(i){
		if(!this.options.gaCall)return;
		link = (this.handles[i].get('href') || this.handles[i].getChildren('a')[0].get('href') || '');
		hash = new URI(link).get('fragment');
		toLink = new URI().get('directory');
		try{
			pageTracker._trackPageview(toLink+hash);
			this.log("Just called Analytics with : "+toLink+hash);
		}catch(err){};
	},
	log: function(text) {
		if (this.options.debug && window.console) console.log(text);
	}
});





