/*==================================================*
 $Id: slideshow.js,v 1.7 2003/05/25 16:00:09 pat Exp pat $
 Copyright 2000-2003 Patrick Fitzgerald
 http://slideshow.barelyfitz.com/

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *==================================================*/
 
/*
	Modified for SmartGirl
	by Sandra Gani, sgani@andrew.cmu.edu
	Summer 2003.
*/ 
 

// There are two objects defined in this file:
// "slide" - contains all the information for a single slide
// "slideshow" - consists of multiple slide objects and runs the slideshow

//==================================================
// Slide Object
// This is the constructor function for the slide object.
// It is called automatically when you create a new slide object.
//==================================================
function slide(src, title, text) {

  // Image URL
  this.src = src;

  // Title of display
  this.title = title;

  // Text to display
  this.text = text;

  // Create an image object for the slide
  if (document.images) {
    this.image = new Image();
  }

  //--------------------------------------------------
  this.load = function() {
  // This function loads the image for the slide

    if (!document.images) { return; }

    if (this.image.src != this.src) {
      this.image.src = this.src;
    }
  }
}

//==================================================
// Slideshow Object
// This is the constructor function for the slideshow object.
// It is called automatically when you create a new object.
//==================================================
function slideshow( slideshowname ) {

  // Name of this object
  this.name = slideshowname;

  // When we reach the last slide, should we loop around to start the slideshow again?
  this.repeat = true;

  // Number of images to pre-fetch.
  // -1 = preload all images.
  //  0 = load each image is it is used.
  //  n = pre-fetch n images ahead of the current image.
  this.prefetch = -1;

  // IMAGE element on your HTML page.
  this.image;
  
  // These are private variables
  this.slides = new Array();
  this.current = 0;
  this.timeoutid = 0;

  //--------------------------------------------------
  // Public methods
  //--------------------------------------------------
  this.add_slide = function(slide) {
  // Add a slide to the slideshow.
  
    // If this version of JavaScript does not allow us to
    // change images, then we can't do the slideshow.
    if (!document.images) { return; }
  
    var i = this.slides.length;
  
    // Prefetch the slide image if necessary
    if (this.prefetch == -1) {
      slide.load();
    }

    this.slides[i] = slide;
  }

  //--------------------------------------------------
  this.update = function() {
  // This function updates the slideshow image on the page
  
    // Make sure the slideshow has been initialized correctly
    if (! this.valid_image()) { return; }
  
    // Load the slide image if necessary
    this.slides[ this.current ].load();
  
    // Update the image.
    this.image.src = this.slides[ this.current ].image.src;
  
   // Update the title
    this.display_title();
  
    // Update the text
    this.display_text();

    // Pre-fetch the next slide image(s) if necessary
    if (this.prefetch > 0) {
      for (i = this.current + 1;
           i <= (this.current + this.prefetch) && i < this.slides.length;
           i++) {
        this.slides[i].load();
      }
    }
  }

  //--------------------------------------------------
  this.next = function() {
  // This function advances to the next slide.
  
    // Increment the image number
    if (this.current < this.slides.length - 1) {
      this.current++;
    } else if (this.repeat) {
      this.current = 0;
    }
  
    this.update();
  }

  //--------------------------------------------------
  this.previous = function() {
  // This function goes to the previous slide.
  
    // Decrement the image number
    if (this.current > 0) {
      this.current--;
    } else if (this.repeat) {
      this.current = this.slides.length - 1;
    }
  
    this.update();
  }

  //--------------------------------------------------
  this.display_title = function(title) {
  // Display the text for the current slide
  
    // If the "title" arg was not supplied (usually it isn't),
    // get the title from the slideshow
    if (!title) {
      title = this.slides[ this.current ].title;
    }
  
    // If a title id has been specified,
    // then change the contents of the HTML element
    if (this.titleid) {
      // Make sure we don't cause an error
      // for browsers that do not support getElementById
     if (!document.getElementById){ return false; }
  
     r = document.getElementById(this.titleid);
     if (!r){ return false; }
      r.innerHTML = title;
    }
  }
    
  //--------------------------------------------------
  this.display_text = function(text) {
  // Display the text for the current slide
  
    // If the "text" arg was not supplied (usually it isn't),
    // get the text from the slideshow
    if (!text) {
      text = this.slides[ this.current ].text;
    }
  
    // If a text id has been specified,
    // then change the contents of the HTML element
    if (this.textid) {
      // Make sure we don't cause an error
      // for browsers that do not support getElementById
      if (!document.getElementById){ return false; }
  
      r = document.getElementById(this.textid);
      if (!r){ return false; }
      r.innerHTML = text;
    }
  }

  //--------------------------------------------------  
  // Private methods
  //--------------------------------------------------
 
  //--------------------------------------------------
  this.valid_image = function() {
  // Returns 1 if a valid image has been set for the slideshow
  
    if (!this.image)
    {
      // Stop the slideshow
      this.pause;
  
      // Display an error message
      window.status = "Error: slideshow image not initialized for " + this.name;
          
      return 0;
    }
    else {
      return 1;
    }
  }

}