
SlideShow = function(el, imageAndLinkArray, refreshInterval, imageWidth, imageHeight, icons) {
  this.d = d;
  this.imgIndex = 0;
  this.imgAndLinkArray = imageAndLinkArray;
  this.refreshInterval = refreshInterval;
  this.imageWidth = imageWidth;
  this.imageHeight = imageHeight;
  this.icons = icons;
  
  d.style.position = "relative";
  
  var dChildren = d.childNodes;
  for (var childIdx = 0; childIdx < dChildren.length; childIdx++) {
    var el = dChildren[childIdx];
    if (el.nodeName == 'A') {
      this.linkElement = el;
    }
  }
  
  dChildren = this.linkElement.childNodes;
  for (var childIdx = 0; childIdx < dChildren.length; childIdx++) {
    var el = dChildren[childIdx];
    if (el.nodeName == 'IMG') {
      this.imgElement = el;
    }
  }
  
  this.initMouseEvents = function(imgElt) {
    imgElt.onmouseover = function(evt) {
      if (!this.s.isMouseOver) {
        this.s.isMouseOver = true;
        this.s.showControls();
        if (this.s.timeout) {
          clearTimeout(this.s.timeout);
          this.s.timeout = null;
        }
      }
    }
    
    imgElt.onmouseout = function(evt) {
      if (this.s.isMouseOver) {
        var el = getMouseoutEventTarget(evt == null ? window.event : evt);
        if (!elementIsDescendent(el, this.s.d)) {
          this.s.isMouseOver = false;
          this.s.hideControls();
          if (this.s.timeout) {
            clearTimeout(this.s.timeout);
          }
          this.s.timeout = setTimeout("window.animations[" + this.s.animIndex + "].frame()", this.s.refreshInterval);
        }
      }
    }
    
    imgElt.s = this;
  }
  

  this.nextImage = function() {
    this.imgIndex++;
    if (this.imgIndex >= this.imgAndLinkArray.length) {
      this.imgIndex = 0;
    }
    var imgAndLink = this.imgAndLinkArray[this.imgIndex];
    this.transitionTo(imgAndLink);
  };
  

  this.transitionTo = function(newIL) {
    if (this.transitionAnimation != null) {
      this.transitionAnimation.finalFrame();
      this.transitionAnimation.abort = true;
      this.transitionAnimation = null;
    }
    
    var newA = document.createElement("a");
    var newImg = document.createElement("img");
    setElementOpacity(newImg, 0.0);
    
    newImg.src = newIL.imageUrl;
    newA.href = newIL.linkUrl;
    newImg.alt = newIL.altText;
    newImg.style.position = "absolute";
    newImg.width = this.imageWidth;
    newImg.height = this.imageHeight;
    
    this.initMouseEvents(newImg);

    this.d.insertBefore(newA, this.linkElement);
    newA.appendChild(newImg);
    
    var a = new Animate (
      250, 
      20, 
      function(t) { 
        if (this.shouldAbort()) {t = 0;}
        setElementOpacity(this.newImg, t); 
        setElementOpacity(this.oldImg, 1.0-t);
      },
      function() {
        if (this.shouldAbort()) {
          this.s.d.removeChild(this.newA); this.s.imgElement.style.position = "static";
        } else {
          this.s.d.removeChild(this.s.linkElement); this.s.imgElement = this.newImg; this.s.linkElement = this.newA; this.s.imgElement.style.position = "static";
          this.s.transitionAnimation = null;
        }
      }
    );
      
    a.shouldAbort = function() {
      return this.abort || (this.s.isMouseOver && ! this.mouseOverAtStart);
    }
    a.newImg = newImg;
    a.oldImg = this.imgElement;
    a.newA = newA;
    a.s = this;
    a.mouseOverAtStart = this.isMouseOver;
    this.transitionAnimation = a;
    a.go();
    
  }
  
  this.showControls = function() {

    if (this.nextButton) {
      try {
        d.removeChild(this.nextButton);
      } catch (e) {
      }
      this.nextButton = null;
    }
    this.nextButton = document.createElement("div");

    this.nextButton.style.position = "absolute";
    this.nextButton.style.left = "" + (this.imageWidth - 100) + "px";
    this.nextButton.style.top = "20px";
    this.nextButton.style.float = "left";
    setElementOpacity(this.nextButton, 0.0);
    
    var nextLink = document.createElement("a");
    nextLink.href = "javascript:document.getElementById(\"" + this.d.id + "\").s.nextImage()";
    nextLink.s = this;
    
    var nextImage = document.createElement("img");
    nextImage.src = this.icons.nextButtonImage;
    nextImage.alt = "Next image";
    nextImage.onmouseover = function(evt) {
    	this.src = this.hoverImage;
    }
    nextImage.onmouseout = function(evt) {
    	this.src = this.normalImage;
    }
    nextImage.normalImage = this.icons.nextButtonImage;
    nextImage.hoverImage = this.icons.nextButtonHighlightedImage;
    
    nextLink.appendChild(nextImage);
    
    this.nextButton.appendChild(nextLink);
    this.d.appendChild(this.nextButton);
    
    var a = new Animate (
      100, 
      20, 
      function(t) { 
        setElementOpacity(this.nextButton, t); 
      },
      function() {
      }
    );
      
    a.nextButton = this.nextButton;
    a.nextLink = this.nextLink;
    a.go();
  }
  
  this.hideControls = function() {
    if (this.nextButton != null) {
    
      var a = new Animate (
        100, 
        20, 
        function(t) { 
          setElementOpacity(this.nextButton, 1.0 - t); 
        },
        function() {
          try {
            this.d.removeChild(this.nextButton);
          } catch (e) {
          }
          this.nextButton = null;
        }
      );
      a.nextButton = this.nextButton;
      a.s = this;
      a.d = this.d;
      a.go();
    }
  }
  
  
  this.frame = function() {
    if (!this.isMouseOver) {
      if (this.timeout) {
        clearTimeout(this.timeout);
        this.timeout = null;
      }
      this.nextImage();
      if (!this.timeout) {
        this.timeout = setTimeout("window.animations[" + this.animIndex + "].frame()", this.refreshInterval);
      }
    }
  }
  
  this.preloadImages = function() {
    var imageArray = new Array();
    for (var i = 1; i < this.imgAndLinkArray.length; i++) {
      imageArray[i] = new Image(this.imageWidth, this.imageHeight);
    }
    imageArray[this.imgAndLinkArray.length - 1].s = this;
    imageArray[this.imgAndLinkArray.length - 1].onload = this.go;
    
    for (var i = 1; i < this.imgAndLinkArray.length; i++) {
      imageArray[i].src = this.imgAndLinkArray[i].imageUrl;
    }
  }
  
  this.go = function() {
    if (!this.timeout) {
      this.timeout = setTimeout("window.animations[" + this.s.animIndex + "].frame()", this.s.refreshInterval);
    }
  }
  
  if (this.imgAndLinkArray.length > 1) {
    
    this.initMouseEvents(this.imgElement);

    this.isFinished = false;
    
    this.animIndex = addAnimation(this);
    
    this.timeout = null;
    
    setTimeout("document.getElementById(\"" + this.d.id + "\").s.preloadImages()", 1000);
    
  } else {
  
    this.isFinished = true;
    
  }

  
};


