<!--

/*==================================================*
 $Id: slideshow.js,v 1.11 2003/09/18 17:24:04 pat Exp $
 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
 *==================================================*/

// 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
//==================================================
function slide(src,link,text,target,attr) {
  // This is the constructor function for the slide object.
  // It is called automatically when you create a new slide object.
  // For example:
  // s = new slide();

  // Image URL
  this.src = src;

  // Link URL
  this.link = link;

  // Text to display
  this.text = text;

  // Name of the target window ("_blank")
  this.target = target;

  // Custom duration for the slide, in milliseconds.
  // This is an optional parameter.
  // this.timeout = 3000

  // Attributes for the target window:
  // width=n,height=n,resizable=yes or no,scrollbars=yes or no,
  // toolbar=yes or no,location=yes or no,directories=yes or no,
  // status=yes or no,menubar=yes or no,copyhistory=yes or no
  // Example: "width=200,height=300"
  this.attr = attr;

  // 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;
    }
  }

  //--------------------------------------------------
  this.hotlink = function() {
    // This function jumps to the slide's link.
    // If a window was specified for the slide, then it opens a new window.

    if (!this.link) return false;

    if (this.target) {

      // If window attributes are specified, use them to open the new window
      if (this.attr) {
        window.open(this.link, this.target, this.attr);
  
      } else {
        // If window attributes are not specified, do not use them
        // (this will copy the attributes from the originating window)
        window.open(this.link, this.target);
      }

    } else {
      // Open the hotlink in the current window
      location.href = this.link;
    }
  }
}

//==================================================
// slideshow object
//==================================================
function slideshow( slideshowname ) {
  this.name = slideshowname;
  this.repeat = true;
  this.prefetch = -1;
  this.image;
  this.textid;
  this.textarea;
  this.timeout = 6000;
  this.slides = new Array();
  this.current = 0;
  this.timeoutid = 0;

  //--------------------------------------------------
  // Public methods
  //--------------------------------------------------
  this.add_slide = function(slide) {
    if (!document.images) { return; }
  
    var i = this.slides.length;

    if (this.prefetch == -1) {
      slide.load();
    }

    this.slides[i] = slide;
  }

  //--------------------------------------------------
  this.play = function(timeout) {
    this.pause();
  
    if (timeout) {
      this.timeout = timeout;
    }
  
    if (typeof this.slides[ this.current ].timeout != 'undefined') {
      timeout = this.slides[ this.current ].timeout;
    } else {
      timeout = this.timeout;
    }

    this.timeoutid = setTimeout( this.name + ".loop()", timeout);
  }

  //--------------------------------------------------
  this.pause = function() {
  
    if (this.timeoutid != 0) {

      clearTimeout(this.timeoutid);
      this.timeoutid = 0;

    }
  }

  //--------------------------------------------------
  this.update = function() {

    if (typeof this.pre_update_hook == 'function') {
      this.pre_update_hook();
    }

    var slide = this.slides[ this.current ];
    var dofilter = (this.image.filters && this.image.filters[0]);

    if (! this.valid_image()) { return; }
  
    slide.load();
  
    if (dofilter) {

      if (slide.filter &&
          this.image.style &&
          this.image.style.filter) {

        this.image.style.filter = slide.filter;

      }
      this.image.filters[0].Apply();
    }

    this.image.src = slide.image.src;

    if (dofilter) {
      this.image.filters[0].Play();
    }

    this.display_text();

    if (typeof this.post_update_hook == 'function') {
      this.post_update_hook();
    }

    if (this.prefetch > 0) {
      for (i = this.current + 1;
           i <= (this.current + this.prefetch) && i < this.slides.length;
           i++) {
        this.slides[i].load();
      }
    }
  }

  //--------------------------------------------------
  this.goto_slide = function(n) {

    if (n == -1) {
      n = this.slides.length - 1;
    }
  
    if (n < this.slides.length && n >= 0) {
      this.current = n;
    }
  
    this.update();
  }

  //--------------------------------------------------
  this.next = function() {

    if (this.current < this.slides.length - 1) {
      this.current++;
    } else if (this.repeat) {
      this.current = 0;
    }
  
    this.update();
  }

  //--------------------------------------------------
  this.previous = function() {

    if (this.current > 0) {
      this.current--;
    } else if (this.repeat) {
      this.current = this.slides.length - 1;
    }
  
    this.update();
  }

  //--------------------------------------------------
  this.get_text = function() {
  
    return(this.slides[ this.current ].text);
  }

  //--------------------------------------------------
  this.get_all_text = function(before_slide, after_slide) {
  
    all_text = "";
  
    for (i=0; i < this.slides.length; i++) {
  
      slide = this.slides[i];
    
      if (slide.text) {
        all_text += before_slide + slide.text + after_slide;
      }
  
    }
  
    return(all_text);
  }

  //--------------------------------------------------
  this.display_text = function(text) {

    if (!text) {
      text = this.slides[ this.current ].text;
    }
  
    if (this.textarea && this.textarea.value) {
      this.textarea.value = text;
    }
  
    if (this.textid) {

      if (!document.getElementById){ return false; }
      r = document.getElementById(this.textid);
      if (!r) { return false; }
      if (typeof r.innerHTML == 'undefined') { return false; }

      r.innerHTML = text;
    }
  }

  //--------------------------------------------------
  this.hotlink = function() {
  
    this.slides[ this.current ].hotlink();
  }

  //--------------------------------------------------
  this.save_position = function(cookiename) {
  
    if (!cookiename) {
      cookiename = this.name + '_slideshow';
    }
  
    document.cookie = cookiename + '=' + this.current;
  }

  //--------------------------------------------------
  this.restore_position = function(cookiename) {
  
    if (!cookiename) {
      cookiename = this.name + '_slideshow';
    }
  
    var search = cookiename + "=";
  
    if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search);
      if (offset != -1) { 
        offset += search.length;
        end = document.cookie.indexOf(";", offset);
        if (end == -1) end = document.cookie.length;
        this.current = parseInt(unescape(document.cookie.substring(offset, end)));
        }
     }
  }

  //--------------------------------------------------
  this.noscript = function() {
  
    $html = "\n";
  
    for (i=0; i < this.slides.length; i++) {
  
      slide = this.slides[i];
  
      $html += '<P>';
  
      if (slide.link) {
        $html += '<a href="' + slide.link + '">';
      }
  
      $html += '<img src="' + slide.src + '" ALT="slideshow image">';
  
      if (slide.link) {
        $html += "<\/a>";
      }
  
      if (slide.text) {
        $html += "<BR>\n" + slide.text;
      }
  

      $html += "<\/P>" + "\n\n";
    }
  
    $html = $html.replace(/\&/g, "&amp;" );
    $html = $html.replace(/</g, "&lt;" );
    $html = $html.replace(/>/g, "&gt;" );
  
    return('<pre>' + $html + '</pre>');
  }

  //==================================================
  // Private methods
  //==================================================

  //--------------------------------------------------
  this.loop = function() {

    if (this.current < this.slides.length - 1) {
      next_slide = this.slides[this.current + 1];
      if (next_slide.image.complete == null || next_slide.image.complete) {
        this.next();
      }
    } else { // we're at the last slide
      this.next();
    }
    
    this.play( );
  }

  //--------------------------------------------------
  this.valid_image = function() {
  
    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;
    }
  }

  this.set_image = function(imageobject) {

    if (!document.images)
      return;
    this.image = imageobject;
  }

  //--------------------------------------------------
  this.set_textarea = function(textareaobject) {

    this.textarea = textareaobject;
    this.display_text();
  }

  //--------------------------------------------------
  this.set_textid = function(textidstr) {

    this.textid = textidstr;
    this.display_text();
  }
}

//-->

