/**
 * TABS

 * USAGE:
 *  
 * 	new Tabs('a.handle','ul.elems', {
 * 		debug:true,
	 	startIndex:0,
		activeClass:'active'
 * 	})
 *
 *
 * @version		1.0
 *
 * @author		Phil Pérusse <pperusse [at] tqs [dot] ca>
 * @copyright	Author
 */
var Tabs = new Class({
	Implements:[Options,Events],
	options: {
		startIndex:0,
		cycle:false,
		debug:false,
		activeClass:'active',
		onInit:Class.empty,
		onChange:Class.empty		
	},
	initialize: function(handles,elems,options){
		this.log('Tabs initialized');
		this.setOptions(options);
		this.handles=$$(handles) || handles;
		this.elems=$$(elems) || elems;
		this.log('Number of elements : '+this.elems.length);
		this.log('Number of handles : '+this.handles.length);
		this.elems.setStyle('display','block');	
		this.handles.each(function(el,i){
			el.addEvent('click',function(e){
				e = new Event(e).stop();
				el.addClass(this.options.activeClass);	
				this.activate(i);				
			}.bind(this));
		}.bind(this));
		this.currentIndex = this.options.startIndex;
		this.log('Start Index : '+this.options.startIndex);
		this.activate(this.options.startIndex);
		this.fireEvent('onInit');
		
	},
	activate:function(i){
		if(i>this.handles.length-1)return;
		this.log('Activating : '+i);
		this.handles.removeClass(this.options.activeClass);
		this.handles[i].addClass(this.options.activeClass);
		this.elems.setStyle('display','none');
		this.elems[i].setStyle('display','block');
		this.currentIndex=i;
		this.fireEvent('onChange');
	},
	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,this.handles.length-1));
	},
	getIndex:function(){
		this.log('Current : '+this.currentIndex);
		return this.currentIndex;
	},
	last:function(){
		this.activate(this.handles.length-1);
	},
	log: function(text) {
		if (this.options.debug && window.console) console.log(text);
	}
});




