/**
 * @author ajubert
 * Gestion du widget caroussel
 */
var mid_carousel = Class.create();
mid_carousel.prototype = {
    initialize: function(element, items_nbr){
		Effect.Queues.get('mid_carousel').interval = 250;
        this.element = $(element);
        if (!this.element) {
            console.log(element + ' not found. Unable to init carousel widget.');
            return;
        }
        this.options = Object.extend({
            left_arrow_class: ".btn-prev",
			right_arrow_class: ".btn-next",
            container_class: ".list",
			auto_animate: true,
            animate_delay: 5,
            movex: 190,
            movey: 0,
			visible_items_nbr: 1
        }, arguments[2] ||
        {});
		//console.log(this.options);
        this.left_arrow = this.element.select(this.options.left_arrow_class)[0];
        this.right_arrow = this.element.select(this.options.right_arrow_class)[0];
        this.carousel_container = this.element.select(this.options.container_class)[0];
        this.items_nbr = items_nbr / (this.options.visible_items_nbr || 1);
		this.items_nbr = Math.ceil((this.items_nbr));
        this.item_index = 1;
        this.effect = null;
		this.pe = null;
		try {
			if (this.options.auto_animate) {
				this.pe = new PeriodicalExecuter(this.move_right.bind(this), this.options.animate_delay);
			}
		}
		catch(err){}
        if(this.left_arrow) this.left_arrow.observe('click', this.move_left.bind(this));
        if(this.right_arrow) this.right_arrow.observe('click', this.move_right.bind(this));
    },
    
    move_left: function(e){
        //console.log(e);
        if (e != this.pe) 
            this.pe.stop();
        var movex = this.options.movex;
        var movey = this.options.movey;
        if (this.item_index == 1) {
            movex = -(this.options.movex) * (this.items_nbr - 1);
            movey = -(this.options.movey) * (this.items_nbr - 1);
            this.item_index = this.items_nbr;
        }
        else {
            this.item_index--;
        }
        this.effect = new Effect.Move(this.carousel_container, {
            x: movex,
            y: movey,
            mode: 'relative',
            duration: .5,
            queue: 'end',
            scope: 'mid_carousel'
        });
    },
	
    move_right: function(e){
        //console.log(e);
        if (e != this.pe) 
            this.pe.stop();
        var movex = -(this.options.movex);
        var movey = -(this.options.movey);
        if (this.item_index == this.items_nbr) {
            movex = this.options.movex * (this.items_nbr - 1);
            movey = this.options.movey * (this.items_nbr - 1);
            this.item_index = 1;
        }
        else {
            this.item_index++;
        }
        effect = new Effect.Move(this.carousel_container, {
            x: movex,
            y: movey,
            mode: 'relative',
            duration: .5,
            queue: 'end',
            scope: 'mid_carousel'
        });
    }
}

