
var SlideShow = Class.create({
  initialize: function(element, options) {
    options = $H({ transition: 5.0, pause: 2.0 }).merge(options ? options : {});
    this.soptions = options;
    this.element  = $(element);
    this.pictures = this.element.select('li');
    this.picture_index = this.pictures.size() - 1;
  },
  
  animate: function() {
    if (this.pictures.size() < 2) { return false; }
    
    if (!this._animate) {
      this._animate = function() {
        var self    = this;
        var current = this.pictures[this.picture_index];
        this.picture_index = (this.picture_index + 1) % this.pictures.size();
        var next    = this.pictures[this.picture_index];
        
        new Effect.Parallel([
          Effect.Fade(current, { sync: true, duration: this.soptions.get('transition') }),
          Effect.Appear(next,  { sync: true, duration: this.soptions.get('transition') })
        ], {
          afterFinish: function(){ self._animate.delay(self.soptions.get('pause')); }
        });
      };
      this._animate = this._animate.bind(this);
    }
    this._animate.delay(this.soptions.get('pause'));
    
    return false;
  }
  
});

Element.addMethods({
  slideshow: function(element, options) {
    element = $(element);
    element.slideshow = new SlideShow(element, options);
    return element.slideshow;
  }
});
