var num_shows = 0;
var shows = new Array();

function createSlideShow(image_id,caption_id,time){
  shows[num_shows] = new SlideShow(image_id,caption_id,time,num_shows);
  return shows[num_shows++];
}

function SlideShow(image_id,caption_id,time,seq) {
    this._show_seq = seq;
    this._image_id = image_id;
    this._image_background_id = image_id+"_background";
    this._caption_id = caption_id;
    this._slide_show_speed = parseInt(time*1500);
    this._pics = new Array();
    this._preLoad = new Array();
    this._captions = new Array();
    this._num_pics = 0;
    this._current = 0;
    this._image = null;
    this._image_background = null;
    this._switching_time = 1000;
    this._speed = Math.round(this._switching_time / 100);
}

SlideShow.prototype._show_seq;
SlideShow.prototype._image_id;
SlideShow.prototype._image_background_id;
SlideShow.prototype._caption_id;
SlideShow.prototype._slide_show_speed;
SlideShow.prototype._pics;
SlideShow.prototype._preLoad;
SlideShow.prototype._captions;
SlideShow.prototype._num_pics;
SlideShow.prototype._current;
SlideShow.prototype._image;
SlideShow.prototype._image_background;
SlideShow.prototype._switching_time;
SlideShow.prototype._speed;

SlideShow.prototype.changeOpac = function(opacity, elem) {
  elem.style.opacity = (opacity / 100);
  elem.style.MozOpacity = (opacity / 100);
  elem.style.KhtmlOpacity = (opacity / 100);
  elem.style.filter = "alpha(opacity=" + opacity + ")";
}

SlideShow.prototype.restoreImage = function(){
  this._image.src = this._image_background.src;
  this.changeOpac(100, this._image);
}

SlideShow.prototype.add = function(image,text){
  this._pics[this._num_pics] = image;
  this._captions[this._num_pics] = text;
  this._num_pics++;
}

SlideShow.prototype.initialize = function() {
  this._image = document.getElementById(this._image_id);
  this._caption = document.getElementById(this._caption_id);
  this._image_background = this._image.cloneNode(false);
  this._image_background.id = this._image_background_id;
  $(this._image).css({'display':'block','position':'absolute','z-index':'0'});
  $(this._image).wrap("<div id='slideshow_wrapper_"+this._show_seq+"'></div>");
  $("#slideshow_wrapper_"+this._show_seq).css({'margin':'0','padding':'0'});
  $("#slideshow_wrapper_"+this._show_seq).append($(this._image_background));
  for(i=0; i<this._num_pics; i++){
    this._preLoad[i] = new Image();
    this._preLoad[i].src = this._pics[i];
  }
  this._image.src = this._preLoad[0].src;
  this._current = -1;
}

SlideShow.prototype.startSlideShow = function() {
  this.initialize();
  this.runSlideShow();
}

SlideShow.prototype.runSlideShow = function() {
  this._current = (this._current+1+this._num_pics)%this._num_pics;
  this._image_background.src = this._preLoad[this._current].src;
  if(this._caption) setTimeout("shows["+this._show_seq+"]._caption.innerHTML = shows["+this._show_seq+"]._captions[shows["+this._show_seq+"]._current]",(50 * this._speed));
  for(i = 100; i>=0; i--) {
    setTimeout("shows["+this._show_seq+"].changeOpac(" + i + ",shows["+this._show_seq+"]._image"+")",( (100-i) * this._speed));
  }
  setTimeout("shows["+this._show_seq+"].restoreImage()", this._switching_time);
  t = setTimeout("shows["+this._show_seq+"].runSlideShow()", this._slide_show_speed);
}
