var Slideshow = Class.create();
Slideshow.prototype = {
    _current: 0,
    initialize: function(id, speed) {
      this._id = id;
      this._ss = $(this._id);
      this._speed = speed;
      this._ss.observe('mouseover', this.handleMouseOver.bindAsEventListener(this));
      this._ss.observe('mouseout', this.handleMouseOut.bindAsEventListener(this));
      this._ss.childElements().each( function(el){el.setStyle({position: 'absolute'});});
      this._ss.childElements().each( function(el){
        if (el.getHeight() > this._ss.getHeight()) this._ss.setStyle({'height': el.getHeight()+'px'});
        [el.down('.imgprev'), el.down('.imgnext')].each( function(button){
          button.setOpacity(0.3);
          var options = {
            handleMouseOver: function() {this.setOpacity(0.7);},
            handleMouseOut: function() {this.setOpacity(0.3);}
          }
          Object.extend(button, options);
          button.observe('mouseover', button.handleMouseOver);
          button.observe('mouseout', button.handleMouseOut);
          button.observe('click', this.handleClick.bindAsEventListener(this));
        }.bind(this));
      }.bind(this));
      this.startLoop();
    },
    handleClick: function(event) {
      event.stop();
      var el = Event.element(event);
      var step = 1;
      if(el.hasClassName('imgprev')) step=-1;
      this.nextSlide(step);
    },
    nextSlide: function(step) {
      var slides = this._ss.childElements();
      slides[this._current].hide();
      this._current=this._current+step;
      if(this._current>slides.length-1) this._current = 0;
      if(this._current<0) this._current = slides.length-1;
      slides[this._current].show();
    },
    loop: function() {
      this.nextSlide(1);
      this.startLoop();
    },
    startLoop: function() {
      this._to = setTimeout(this._id+".loop()",this._speed);
    },
    stopLoop: function() {
      clearTimeout(this._to);
    },
    handleMouseOver: function() {this.stopLoop();},
    handleMouseOut: function() {this.startLoop();}
}