// javascript by posttoast, 2008 (http://www.posttoast.nl)

var slideShow = new Class({
 
  options: {
    slideshow_id: "",
    duration: 3000, // Time between slide in ms
    thumbnails: "",
    thumbnailPrefix: "thumbnail_",
    slidePrefix: "slide_"
  },
 
  initialize: function(options){
    this.setOptions(options);
    this.slideImg = $(this.options.slideshow_id).getElements('div'); 
    this.slideImg.setStyle('display', 'none');
    this.current = 0;
    this.numImg = this.slideImg.length;
    this.start();
  },
 
  initSlideshow: function(){
      this.slideImg[this.current].setStyle('display', 'block');
      this.slideImg[this.current].setStyle('opacity', '0');
      this.slideImg[this.current].fade('in');

    this.slideImg.each(function(el, j){
      if (j != this.current) {
        el.setStyle('display', 'none');
      }
    }.bind(this));
    this.previous = this.current;
    this.current ++;
    if (this.current == this.numImg) this.current = 0;

    this.cycleForward = this.initSlideshow.delay(this.options.duration, this);
  },

  initThumbs: function(){
    this.stopSliding();
    this.slideThumbs = $(this.options.thumbnails).getElements('img'); 
    var slideImg = $(this.options.slideshow_id).getElements('div');
    var thumbnailPrefix = this.options.thumbnailPrefix;
    var slidePrefix = this.options.slidePrefix;
    this.slideThumbs.addEvent('click', function(){
      slideImg.setStyle('display', 'none');
      this.activeImage = this.getProperty('id').replace(thumbnailPrefix, slidePrefix);
      $(this.activeImage).setStyle('display', 'block');
      $(this.activeImage).setStyle('opacity', '0');
      $(this.activeImage).fade('in');
    });
    // this.slideThumbs.addEvent('mouseover', function(){
      // rolloverImage = this.getProperty('src').replace('-thumb', '-thumb-over');
      // this.setProperty('src', rolloverImage);
    // });
    // this.slideThumbs.addEvent('mouseout', function(){
      // rolloverImage = this.getProperty('src').replace('-thumb-over', '-thumb');
      // this.setProperty('src', rolloverImage);
    // });
  },

  stopSliding: function(){
    $clear(this.cycleForward);
  },

  start: function(){
    this.initSlideshow();
    if ($(this.options.thumbnails))this.initThumbs();
  }

});

slideShow.implement(new Options);
