Boutikcircus_Slideshow = Class.create({
    
    initialize: function(slideshow,width,nb,tempo) /* nb : nombre de produits à afficher dans le slide */
    {
        this.slideshow = $(slideshow);
        this.count = this.slideshow.getElementsByClassName('element-slideshow').length - nb + 1;
        this.width = width;
        this.position = 0;
        this.tempo = tempo;
        this.puceActiveName = 'puce_0';
        $(this.puceActiveName).addClassName('active');
        if (tempo != 0 && this.count>0) {
            this.autoAnime(this);
        }
    },
    
    animeRight: function()
    {
        if (this.tempo > 0)
            this.anim.stop();
        if (this.count > 0) {
        if (this.position == this.width*(this.count-1)) {
            new Effect.Move(this.slideshow, { x: this.width*(this.count-1), y: 0, duration: .4 });
            this.position = 0;
        } else {
            new Effect.Move(this.slideshow, { x: -this.width, y: 0, duration: 0.2 });
            this.position = this.position+this.width;
        }
        if (this.tempo > 0)
            this.autoAnime(this);
        }
    },
    
    animeLeft: function()
    {
        if (this.tempo > 0)
            this.anim.stop();
        if (this.count > 0) {
            if (this.position == 0) {
                new Effect.Move(this.slideshow, { x: -this.width*(this.count-1), y: 0, duration: .4 });
                this.position = this.width*(this.count-1);
            } else {
                new Effect.Move(this.slideshow, { x: this.width, y: 0, duration: 0.2 });
                this.position = this.position-this.width;
            }
            if (this.tempo > 0)
                this.autoAnime(this);
        }
    },
    
    autoAnime: function(data)
    {
        this.anim = new PeriodicalExecuter( function(pe) {
            if (data.position == data.width*(data.count-1)) {
                new Effect.Move(data.slideshow, { x: data.width*(data.count-1), y: 0, duration: .4 });
                data.position = 0;
            } else {
                new Effect.Move(data.slideshow, { x: -data.width, y: 0, duration: 0.2 });
                data.position = data.position+data.width;
            }
            $(data.puceActiveName).removeClassName('active');
            data.puceActiveName = 'puce_' + data.position/data.width;
            $(data.puceActiveName).addClassName('active');
        }, this.tempo);
    },
    
    setPosition: function(data)
    {
        if (this.tempo > 0)
            this.anim.stop();
        if (this.count > 0) {
            new Effect.Move(this.slideshow, { x: this.position - data*this.width, y: 0, duration: 0.2 });
            this.position = data * this.width;
            $(this.puceActiveName).removeClassName('active');
            this.puceActiveName = 'puce_' + this.position/this.width;
            $(this.puceActiveName).addClassName('active');
            if (this.tempo > 0)
                this.autoAnime(this);
        }
    }
});


