function Slideshow(numImages, slideshowId, timeout) {
  this.numImages = numImages;
  this.curr = 0;
  this.next = 1;
  this.images = new Array();
  this.captions = new Array();
  for (i = 0;i < this.numImages;i++) {
    this.images[i] = document.getElementById(id = 'slide_' + slideshowId + '_' + i);
    this.captions[i] = document.getElementById('caption_' + slideshowId + '_' + i);
  }
  
  this.imageWidth = $(this.images[0].parentNode).width();
  
  this.timeout = timeout;
  this.fadetime = 700;
  this.captiontime = 500;
  
  selfRef = this;
  setTimeout(function() {selfRef.setDelay();}, this.timeout);
}

Slideshow.prototype.fade = function() {
  selfRef = this;
  $(this.images[this.curr]).fadeOut(this.fadetime, function() {selfRef.swap()});
  var timediff = this.fadetime - this.captiontime;
  setTimeout(function() {selfRef.swapCaption();}, timediff);
}

Slideshow.prototype.swapCaption = function() {
  $(this.captions[this.curr]).animate({'left':'-'+this.imageWidth+'px'}, this.captiontime);
  $(this.captions[this.next]).css('left', this.imageWidth+'px');
  $(this.captions[this.next]).animate({'left':'0'}, this.captiontime);
}

Slideshow.prototype.swap = function() {
  selfRef = this;
  // swap to bottom
  this.images[this.curr].style.zIndex = 0;
  $(this.images[this.curr]).show();

  // change images
  this.curr = this.next;
  this.next++;
  if(this.next >= this.numImages) {
    this.next = 0;
  }
  
  // swap next up
  this.images[this.curr].style.zIndex = 2;
  this.images[this.next].style.zIndex = 1;

  setTimeout(function() {selfRef.setDelay();}, this.timeout);
}

Slideshow.prototype.setDelay = function() {
  if(this.images[this.curr].complete && this.images[this.next].complete) {
    this.fade();
  } else {
    selfRef = this;
    setTimeout(function() {selfRef.setDelay();}, this.timeout);
  }
}
