/**
 * QTabs for Mootools 1.2
 *
 * @version 1.0.0
 * @package qtabs
 * @author Massimo Giagnoni ( http://www.latenight-coding.com )
 * @copyright Copyright (C) 2008 Massimo Giagnoni. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/

var QTabs = new Class({
    Implements:[Options,Events],
    options:{
		flexHeight:false,
		def_tab:0,
		autoplay:0,
		scrolling:0,
		delay:3000,
		duration:500,
		transition:'Quad',
		easing:'easeInOut',
		onActive: function(container, idx){
			var content = this.tcontents[this.sufijo][idx];
			var s = container.getSize();
			var cw = s.x;
			if(this.options.flexHeight) {
				s = content.getSize();
				container.setStyle('height', s.y + 'px');
			}
			//content.setStyle('z-index',2);		
			var d = (this.curTab >= 0 ? this.options.duration : 1);
			var sdir = this.options.scrolling;
			if(sdir == 'a') {
				if(idx > this.curTab) {
					sdir = 'lr';
				} else {
					sdir = 'rl';
				}	
			}
			var lft = [0,0];
			switch(sdir) {
				case 'lr':
					lft = [-cw,0];
					break;
				case 'rl':
					lft = [cw,0];
					break;
			}
	              
			this.fxOn = true;
			if(this.options.transition == 'linear') {
				var t = Fx.Transitions[this.options.transition];
			} else {
				var t = Fx.Transitions[this.options.transition][this.options.easing];
			}
			content.set('morph',{duration: d, transition: t});
			content.morph({
				top: [0,0],
				left: lft,
				opacity: [1,1]
			});
			content.setStyle('z-index',2);
			/*content.effects({
				duration: d,
				transition: t
			}).start({
				top: [0,0],
				left: lft,
				opacity: [1,1]
			});*/
			this.fxEnd.delay(d, this);
			this.tabs[this.sufijo][idx].addClass('open').removeClass('closed');
		},

		onBackground: function(tab, content){
		  $('vvv').set('value' , "onback " + this.sufijo);
			content.setStyle('display', 'none');
			content.setStyle('z-index',1);
			tab.addClass('closed').removeClass('open');
		}
    },

    initialize: function(m_id, options){
        this.tabs={};
        this.curTab = -1;
		    this.sufijo=m_id;
        this.tcontents={};
        this.container={};
        this.setOptions(options);
        this.tabs[m_id] = $('qtabs-'+ m_id).getChildren('li')
        this.container[m_id] = $('current-'+ m_id);
        this.tcontents[m_id] = this.container[m_id].getChildren('div.qtcontent');
		
		for (var i = 0, l = this.tabs[m_id].length; i < l; i++){
            var tab = this.tabs[m_id][i];
            tab.addEvent('mouseenter', this.display.bind(this, i));
            //tab.addEvent('mouseenter', this.mouseEnter.bind(this, i));
            tab.addEvent('mouseleave', this.mouseLeave.bind(this, i));
        }
		
    this.cookietab=Cookie.read(m_id);
    if (this.sufijo != 'izq'){
    if (!this.cookietab) {
      this.display(this.options.def_tab);
    }else{
      this.display(this.cookietab);
    }
    }else{
    this.display(0);
    }
    },

    display: function(i){
      this.cookie = Cookie.write( this.sufijo , i, {duration: 30});
    	if(i < 0 || i >= this.tabs[this.sufijo].length) { i=0; }
        if(this.curTab == i || this.fxOn) {return;}
        
        $clear(this.timer);
        for (var c = 0, l = this.tabs[this.sufijo].length; c < l; c++){
            if (c != i) {
            	this.tabs[this.sufijo][c].addClass('closed').removeClass('open');
            } 
            this.tcontents[this.sufijo][c].setOpacity(0);
            this.tcontents[this.sufijo][c].setStyle('z-index',1);
        }
        this.fireEvent('onActive', [this.container[this.sufijo], i]);
        this.curTab = i;
    },
    mouseEnter: function(i){
    	this.tabs[this.sufijo][i].addClass('hover');
    },
    mouseLeave: function(i){
    	this.tabs[this.sufijo][i].removeClass('hover');
    },
    fxEnd: function() {
		this.fxOn = false;
		if(this.options.autoplay) {
			var i = this.curTab + 1;
			if(i >= this.tcontents[this.sufijo].length) {
				i = 0;
			}
			this.timer = this.display.delay(this.options.delay, this, i);
		}
	}
});