/*
 * Slide show script
 */

/*
 * The slide time in milliseconds.
 */
var slideTime = 4000;

/*
 * The fade time in milliseconds
 * (Internet Explorer only).
 */
var fadeTime = 500;

/*
 * Array of SlideShow objects.
 */
var slideShowArray = new Array();

/*
 * Number of running slide shows.
 */
var runningSlideShows = 0;

/*
 * Define DOM Node types if necessary.
 */
if (!window.Node || !window.Node.ELEMENT_NODE)
{
  var Node =
  {
    ELEMENT_NODE: 1,
    ATTRIBUTE_NODE: 2,
    TEXT_NODE: 3,
    CDATA_SECTION_NODE: 4,
    ENTITY_REFERENCE_NODE: 5,
    ENTITY_NODE: 6,
    PROCESSING_INSTRUCTION_NODE: 7,
    COMMENT_NODE: 8,
    DOCUMENT_NODE: 9,
    DOCUMENT_TYPE_NODE: 10,
    DOCUMENT_FRAGMENT_NODE: 11,
    NOTATION_NODE: 12
  };
}

/*
 * Check if image blending is supported.
 */
var agent = navigator.userAgent.toLowerCase();
var appver = parseInt(navigator.appVersion);
var canBlend = (agent.indexOf('msie') != -1) &&
               (agent.indexOf('opera') == -1) &&
               (appver >= 4) &&
               (agent.indexOf('msie 4') == -1) &&
               (agent.indexOf('msie 5.0') == -1);

/*
 * SlideShow object constructor.
 */
function SlideShow(slideShowSource,
                   slideImgId,
                   captionId)
{
  // Initialize SlideShow properties.
  this.slideShowSource = slideShowSource;
  this.slideImgId = slideImgId;
  this.slideImgElement = null;
  this.captionId = captionId;
  this.captionElement = null;
  this.imageCache = null;
  this.cycleLimit = 3;
  this.currentCycle = 0;
  this.currentSlide = 0;
  this.runShow = false;

  // Preload the images.
  this.imageCache = new Array();
  for (ii = 0; ii < this.slideShowSource.length; ii++)
  {
    var image = new Image();
    image.src = this.slideShowSource[ii][0];
    this.imageCache[ii] = image;
  }

  // Add this SlideShow to the array of SlideShow objects.
  slideShowArray.push(this);
}

/*
 * SlideShow start method.
 */
SlideShow.prototype.start = function()
{
  // Quit if DOM isn't supported.
  if (!document.getElementById)
  {
    return;
  }

  // Get the image and caption elements.
  if (!this.slideImgElement)
  {
    this.slideImgElement = document.getElementById(this.slideImgId);
  }
  if (!this.captionElement && this.captionId)
  {
    this.captionElement = document.getElementById(this.captionId);
  }

  // Show the current slide.
  this.showSlide(this.currentSlide);

  // Run this slide show.
  if (!this.runShow)
  {
    this.runShow = true;
    runningSlideShows++;
    if (runningSlideShows == 1)
    {
      setTimeout(slideShowTimeout, slideTime);
    }
  }
}

/*
 * SlideShow stop method.
 */
SlideShow.prototype.stop = function()
{
  // Stop this slide show.
  if (this.runShow)
  {
    this.runShow = false;
    runningSlideShows--;
  }
}

/*
 * SlideShow reset method.
 */
SlideShow.prototype.reset = function()
{
  // Stop the slide show.
  this.stop();

  // Reset to the first slide.
  this.currentCycle = 0;
  this.currentSlide = 0;
  this.showSlide(0);
}

/*
 * SlideShow showSlide method.
 */
SlideShow.prototype.showSlide = function(slideNum)
{
  // Set the image.
  if (this.slideImgElement)
  {
    this.slideImgElement.src = this.slideShowSource[slideNum][0];
  }

  // Set the caption.
  if (this.captionElement)
  {
    var textNode = document.createTextNode(this.slideShowSource[slideNum][1]);
    if (this.captionElement.lastChild &&
        (this.captionElement.lastChild.nodeType == Node.TEXT_NODE))
    {
      this.captionElement.replaceChild(textNode,
                                       this.captionElement.lastChild);
    }
    else
    {
      this.captionElement.appendChild(textNode);
    }
  }
}

/*
 * SlideShow nextSlide method.
 */
SlideShow.prototype.nextSlide = function()
{
  // Advance to the next slide.
  this.currentSlide++;
  if (this.currentSlide >= this.slideShowSource.length)
  {
    this.currentSlide = 0;
    this.currentCycle++;
  }

  // Show the next slide.
  if (this.slideImgElement)
  {
    if (canBlend)
    {
      this.slideImgElement.style.filter =
                          "progid:DXImageTransform.Microsoft.Fade(duration=(fadeTime / 1000.0))";
      this.slideImgElement.filters[0].Apply();
    }
    this.showSlide(slideShow.currentSlide);
    if (canBlend)
    {
      this.slideImgElement.filters[0].Play();
    }
  }
}

/*
 * SlideShow slideTimeout method.
 */
SlideShow.prototype.slideTimeout = function()
{
  // If the slide show is running then advance to the next slide.
  if (this.runShow)
  {
    this.nextSlide();
  }

  // If this slide show is over then stop it.
  if (this.currentCycle >= this.cycleLimit)
  {
    this.stop();
  }
}

/*
 * SlideShow timeout function.
 */
function slideShowTimeout()
{
  var ii;

  // Call the slideTimeout() method for all slide shows.
  for (ii = 0; ii < slideShowArray.length; ii++)
  {
    slideShowArray[ii].slideTimeout();
  }

  // Restart the timer.
  if (runningSlideShows > 0)
  {
    setTimeout(slideShowTimeout, slideTime);
  }
}
