// JavaScript Document Carousel

var Carousel = new Class({
    Implements: [Options, Events],
    options: {
        tabsId:"tabs",
        contentId:"wrapper",
        num:4,
        speed:6000,
		mask_width:550,
		auto_slide:true
    },
    initialize: function(options){
        this.setOptions(options);
		this.wrapper = $(this.options.contentId);
		this.current = 0;
        
        if(isset($(this.options.tabsId)) && isset($(this.options.contentId))){
            this.tabs = $$("#"+this.options.tabsId + " li");
			this.legendes = $$(".legende");
        }
		//this._move(0);
		if(this.options.auto_slide){
			this.timer = this.next.periodical(this.options.speed,this);
		}

        
    },
    manualMoveTo:function(index){
        clearInterval(this.timer);
        this.moveTo(index);
    },
    moveTo: function(index){
		this._transition(index);
		this._move(index);
    },
	_transition:function(index){
		new Fx.Tween(this.wrapper).start('left', this.wrapper.getStyle('left'), index*(-this.options.mask_width));
	},
	_move:function(index){
		this.wrapper.setStyle('left',index*(-this.options.mask_width));
		if(isset($(this.options.tabsId))){
			this.tabs.each(function(tab){
				tab.removeClass('current');
			});
			this.legendes.each(function(legende){
				legende.removeClass('current');
			});
			this.tabs[index].addClass('current');
			this.legendes[index].addClass('current');
		}
        this.current = index;
	},
    next:function(){
        this.current++;
        if(this.current >= this.options.num){
            this.current = 0;
        }
        this.moveTo(this.current);
    },
	prev:function(){
        this.current--;
        if(this.current < 0){
            this.current = this.options.num -1;
        }
        this.moveTo(this.current);
    },
	manualPrev:function(index){
        clearInterval(this.timer);
        this.prev();
    },
	manualNext:function(index){
        clearInterval(this.timer);
        this.next();
    }
    
});

function isset(variable_name) {
    try {
        if (typeof(eval(variable_name)) != 'undefined')
            if (eval(variable_name) != null)
                return true;
    } catch(e) { }
    return false;
}


