function setOpacity(target, opacity) {
  /* this should set opacity in every single browser that has any support for it */
  if (target.style) {
    if (target.style.opacity!=null) {
      target.style.opacity = (opacity/100);
    } else if (target.style.MozOpacity!=null) {
      target.style.MozOpacity = (opacity/100);
    } else if (target.style.MozOpacity!=null) {
      target.style.KhtmlOpacity  = (opacity/100);
    } else if (target.style.filter!=null) {
      target.style.filter = "alpha(opacity="+opacity+")";
    }
  }
}

slideshows = new Array();

function Slideshow(numImages, slideshow_prefix, timeout) {
  this.number = slideshows.length + 1;
  slideshows[this.number] = this;

  this.numImages = numImages;
  this.curr = 0;
  this.next = 1;
  this.images = new Array();
  for (i = 0;i < this.numImages;i++) {
    this.images[i] = document.getElementById(slideshow_prefix + i);
  }
  
  this.opacity = 100;
  
  this.timeout = timeout;
  setTimeout("slideshows["+this.number+"].setDelay()", this.timeout);
}

Slideshow.prototype.fadeOut = function() {
  this.opacity -= 3;
  if (this.opacity >= 0) {
    setOpacity(this.images[this.curr], this.opacity);
    window.setTimeout("slideshows["+this.number+"].fadeOut()", 20);
  } else {
    this.opacity = 100;
    setOpacity(this.images[this.curr], 0);
    
    // swap to bottom
    this.images[this.curr].style.zIndex = 0;
    setOpacity(this.images[this.curr], 100);

    // 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("slideshows["+this.number+"].setDelay()", this.timeout);
  }
}

Slideshow.prototype.setDelay = function() {
  if(this.images[this.curr].complete && this.images[this.next].complete) {
    this.fadeOut();
  } else {
    setTimeout("slideshows["+this.number+"].setDelay()", this.timeout);
  }
}
