var Diaporama =  new Class({
  images : [],
  images_src: [],
  current_image_idx : 0,
  //current_image: null,
  timer: null,

  initialize: function() {
    $('slide_gallery').getElements('div.img_container').each(function(item){
      this.images.push(item);
    }.bind(this));
    this.current_image = this.images[0];
    this.start(false);
  },

  addImageSource: function(img_src) {
    this.images_src.push(img_src);
  },


  shiftImageSource: function() {
    if (this.images.length > 0 && this.images_src.length > 0) {
      new_node = this.images[0].clone();
      new_node.setStyle('visibility', 'hidden');
      img_src = this.images_src.shift();
      if (img_node = new_node.getElement('img')) {
        img_node.src = img_src;
      }
      this.images[0].getParent().adopt(new_node);
      this.images.push(new_node);
    }
  },

  switchImage: function() {
    new Fx.Tween(this.images[this.current_image_idx], { property: 'opacity', duration: 1200, fps: 15 }).start(1.0, 0.0); // fade
    this.current_image_idx++;
    if (this.current_image_idx >= this.images.length ) this.current_image_idx = 0;
    new Fx.Tween(this.images[this.current_image_idx], { property: 'opacity', duration: 1200, fps: 15 }).start(0.0, 1.0); //appear

    this.shiftImageSource();
  },

  stop: function() {
    if (!this.timer) return false;
    this.timer = $clear(this.timer);
    return true;
  },

  start: function(force) {
    if (this.timer) return false;
    if (force) { this.switchImage(); }
    this.timer = this.switchImage.periodical(3500, this);
    return true;
  }
});

