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*1000);
  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) {
  if(opacity==0) opacity=1;
  elem.style.opacity = (opacity / 100);
  elem.style.MozOpacity = (opacity / 100);
  elem.style.KhtmlOpacity = (opacity / 100);
  elem.style.filter = "alpha(opacity="+opacity+")";
}

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':'1'});
  $(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._image_background.src = this._preLoad[0].src;
  this.changeOpac(0,this._image_background);
  this._current = 0;
}

SlideShow.prototype.startSlideShow = function() {
  this.initialize();
  t = setTimeout("shows["+this._show_seq+"].runSlideShow()", this._slide_show_speed);
}

SlideShow.prototype.startSlideShow2 = function() {
  this.initialize();
  t = setTimeout("shows["+this._show_seq+"].runSlideShow2()", this._slide_show_speed);
}

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

SlideShow.prototype.runSlideShow = function() {
  this._current = (this._current+1+this._num_pics)%this._num_pics;
  if(this._preLoad[this._current].complete==false) this._current--;
  else {
    this._image.src = this._image.src;
    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(" + (100-i) + ",shows["+this._show_seq+"]._image_background"+")",( (100-i) * this._speed));
      setTimeout("shows["+this._show_seq+"].changeOpac(" + i + ",shows["+this._show_seq+"]._image"+")",( (100-i) * this._speed));
    }
  }
  setTimeout("shows["+this._show_seq+"].restoreImage()",parseInt(this._switching_time*1.02));
  t = setTimeout("shows["+this._show_seq+"].runSlideShow()", this._slide_show_speed);
}

SlideShow.prototype.runSlideShow2 = function() {
  this._current = (this._current+1+this._num_pics)%this._num_pics;
  if(this._preLoad[this._current].complete==false) this._current--;
  else {
    var width = parseInt(this._preLoad[this._current].width)+'px';
    var height = parseInt(this._preLoad[this._current].height)+'px';
    var left = parseInt(parseInt(this._preLoad[this._current].width)/2)+'px';
    $("#"+this._image_id).animate( {
      width: "1px",
      height: height,
      marginLeft: left,
      marginRight: left
    }, parseInt(this._switching_time/2) );
    setTimeout("shows["+this._show_seq+"]._image.src=shows["+this._show_seq+"]._preLoad["+this._current+"].src", this._switching_time/2);
    $("#"+this._image_id).animate( {
      width: width,
      height: height,
      marginLeft: "0px",
      marginRight: "0px"
    }, parseInt(this._switching_time/2) );
  }
  t = setTimeout("shows["+this._show_seq+"].runSlideShow2()", this._slide_show_speed);
}
