if (!Sim) var Sim = {};


Sim.Carrousel = Class.create({
  "carrousel": null,
  "size": 0,
  "pos": 1,
  "executer": null,

  initialize: function(elt) {
    this.carrousel = $(elt);
    this.size = this.carrousel.select('.carrousel-slide').size();
    if (this.size < 1) return false;
    this.pos = 1;
    this.executer = new PeriodicalExecuter(this.next.curry(1).bind(this), 6);

    $("carrousel-prev-stepper").observe('click', this.prev.bind(this));
    $("carrousel-next-stepper").observe('click', this.next.curry(0).bind(this));
  },

  prev: function() {
    var prev_slide = $("carrousel-slide-" + this.pos);
    this.pos = ((this.pos - 1) < 1) ? this.size : (this.pos - 1);
    var next_slide = $("carrousel-slide-" + this.pos);

    prev_slide.hide();
    next_slide.show();
    next_slide.down(".carrousel-image").setStyle({"visibility": "visible", "opacity": 0});
    new Effect.Opacity(next_slide.down(".carrousel-image"), { from:0, to:1 });
  },

  next: function(exec) {
    if (exec < 1) {
      this.executer.stop();
    }

    var prev_slide = $("carrousel-slide-" + this.pos);
    this.pos = ((this.pos + 1) > this.size) ? 1 : (this.pos + 1);
    var next_slide = $("carrousel-slide-" + this.pos);

    prev_slide.hide();
    next_slide.show();
    next_slide.down(".carrousel-image").setStyle({"visibility": "visible", "opacity": 0});
    new Effect.Opacity(next_slide.down(".carrousel-image"), { from:0, to:1 });
  }

});


