// slide object
function slide(src,link,text,target,attr) {
  this.src = src;  // Image URL
  this.link = link;  // Link URL
  this.text = text;  // Text to display
  this.target = target;  // Name of target window ("_blank")
//  this.timeout = 3000  // Custom duration for the slide (ms)
  this.attr = attr;  // Attributes for the target window, like: "width=200,height=300"

  if (document.images) {  // Create an image object for the slide
    this.image = new Image();
  }
  this.loaded = false;  // Flag to tell when load() has already been called

  
  // Methods
  this.load = function() {  // This method loads the image for the slide
    if (!document.images) { return; }
    if (!this.loaded) {
      this.image.src = this.src;
      this.loaded = true;
    }
  }

  
  this.hotlink = function() {  // This method jumps to the slide's link.
    var mywindow;
    if (!this.link) return;  // If this slide does not have a link, do nothing
    if (this.target) {  // Open the link in a separate window?
      if (this.attr) {
        mywindow = window.open(this.link, this.target, this.attr);
      } else {
        mywindow = window.open(this.link, this.target);
      }
      if (mywindow && mywindow.focus) mywindow.focus();  // Pop the window to the front

    } else {
      location.href = this.link;  // Otherwise open link in the current window
    }
  }
}


// slideshow object
function slideshow( slideshowname ) {
  this.name = slideshowname;
  this.repeat = true;  //slideshow loop?
  this.prefetch = -1;  // -1: preload all, 0: load each image when used, n: pre-fetch n images ahead
  this.image;  // IMAGE element on HTML page
  this.textid;  // ID of a DIV element on HTML page that contains the text
//  this.timeout = 3000;
  // this.pre_update_hook = function() { }
  // this.post_update_hook = function() { }
  this.slides = new Array();
  this.current = 0;
  this.timeoutid = 0;


  // Methods
  this.add_slide = function(slide) {  // Add a slide to the slideshow.
    // SLIDES1.add_slide(new slide("s1.jpg", "link.html"))
    var i = this.slides.length;
    if (this.prefetch == -1) {
      slide.load();
    }
    this.slides[i] = slide;
  }


  this.play = function(timeout, step) {  // Implement automatically running slideshow
    this.pause();  // Make sure we're not already playing
    if (timeout) {
      this.timeout = timeout;
    }

    if (typeof this.slides[ this.current ].timeout != 'undefined') {  // Use slide's custom timeout if given
      timeout = this.slides[ this.current ].timeout;
    } else {
      timeout = this.timeout;
    }

    this.timeoutid = setTimeout( this.name + ".loop("+step+")", timeout);  // After timeout, call this.loop()
  }


  this.pause = function() {
    if (this.timeoutid != 0) {
      clearTimeout(this.timeoutid);
      this.timeoutid = 0;
    }
  }


  this.update = function() {  // Update the slideshow image on the page
    if (! this.valid_image()) { return; }  // Make sure the slideshow has been initialized correctly

    if (typeof this.pre_update_hook == 'function') {  // Call the pre-update hook function if one was specified
      this.pre_update_hook();
    }

    var slide = this.slides[ this.current ];  // Convenience variable for the current slide
    var dofilter = false;  // Determine if the browser supports filters
    if (this.image &&
        typeof this.image.filters != 'undefined' &&
        typeof this.image.filters[0] != 'undefined') {
      dofilter = true;
    }

    slide.load();  // Load the slide image if necessary
  
    if (dofilter) {  // Apply the filters for the image transition
      if (slide.filter &&  // Set user-specified custom filter if given
          this.image.style &&
          this.image.style.filter) {
        this.image.style.filter = slide.filter;
      }
      this.image.filters[0].Apply();
    }

    this.image.src = slide.image.src;  // Update the image.
    if (dofilter) {  // Play the image transition filters
      this.image.filters[0].Play();
    }

    this.display_text();  // Update the text
    if (typeof this.post_update_hook == 'function') {  // Call the post-update hook function if one was specified
      this.post_update_hook();
    }

    if (this.prefetch > 0) {  // Pre-fetch images?
      var next, prev, count;
      next = this.current;   // Pre-fetch the next slide image(s)
      prev = this.current;
      count = 0;
      do {
        if (++next >= this.slides.length) next = 0;  // Get the next and previous slide number
        if (--prev < 0) prev = this.slides.length - 1;  // Loop past the ends of the slideshow if necessary

        this.slides[next].load();  // Preload the slide image
        this.slides[prev].load();
      } while (++count < this.prefetch);
    }
  }


  this.goto_slide = function(n) {  // Jump to the slide number specified, like onClick="myslides.goto_slide(5)"
    if (n == -1) {
      n = this.slides.length - 1;
    }
    if (n < this.slides.length && n >= 0) {
      this.current = n;
    }
    this.update();
  }
  

  this.goto_random_slide = function(include_current) {
    var i;
    if (this.slides.length > 1) {  // Make sure there is more than one slide
      do {  // Generate a random slide number
        i = Math.floor(Math.random()*this.slides.length);
      } while (i == this.current);
 
      this.goto_slide(i);
    }
  }


  this.next = function(step) {  // Advance to next slide
    if (!step) {
	    if (this.current < this.slides.length - 1) {
    	  this.current++;
	    } else if (this.repeat) {
    	  this.current = 0;
	    }
    } else {
	    if (this.current < this.slides.length - step) {
    	  this.current=this.current + step;
	    } else if (this.repeat) {
    	  this.current = this.current + step - this.slides.length;
	    }
	}
    this.update();
  }


  this.previous = function(step) {  // Go to previous slide
    if (!step) {
	    if (this.current > 0) {
    	  this.current--;
	    } else if (this.repeat) {
    	  this.current = this.slides.length - 1;
	    }		  
    } else {
	    if (this.current > step-1) {
    	  this.current=this.current - step;
	    } else if (this.repeat) {
    	  this.current = this.current + this.slides.length - step;
	    }		  
	}
    this.update();
  }


  this.shuffle = function() {  // Randomly shuffle order of slides
    var i, i2, slides_copy, slides_randomized;
    slides_copy = new Array();  // Create a copy of array containing slides in sequential order
    for (i = 0; i < this.slides.length; i++) {
      slides_copy[i] = this.slides[i];
    }
    slides_randomized = new Array();  // New array to contain randomized slides

    do {  // Populate new array by looping slides, pick random, remove from ordered list and add to random list
      i = Math.floor(Math.random()*slides_copy.length);
      slides_randomized[ slides_randomized.length ] = slides_copy[i];  // Add slide to end of randomized array
      for (i2 = i + 1; i2 < slides_copy.length; i2++) { // Remove slide from sequential array
        slides_copy[i2 - 1] = slides_copy[i2];
      }
      slides_copy.length--;
    } while (slides_copy.length);

    this.slides = slides_randomized;  // Set slides to randomized array
  }


  this.get_text = function() {  // Return the text of the current slide
    return(this.slides[ this.current ].text);
  }


  this.get_all_text = function(before_slide, after_slide) {
    // Return the text for all of the slides:
    // document.write("<ul>");
    // document.write(s.get_all_text("<li>","\n"));
    // document.write("<\/ul>");
    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) {  // Display the text for current slide
    if (!text) {  // If "text" arg was not supplied, get text from slideshow
      text = this.slides[ this.current ].text;
    }
    if (this.textid) {  // If a text id has been specified, change contents of the HTML element
      r = this.getElementById(this.textid);
      if (!r) { return false; }
      if (typeof r.innerHTML == 'undefined') { return false; }
      r.innerHTML = text;  // Update the text
    }
  }


  this.hotlink = function() {  // Call the hotlink() method for the current slide
    this.slides[ this.current ].hotlink();
  }


  this.save_position = function(cookiename) {  // Save position of slideshow in a cookie
    if (!cookiename) {
      cookiename = this.name + '_slideshow';
    }
    document.cookie = cookiename + '=' + this.current;
  }


  this.restore_position = function(cookiename) {  // Return slideshow to the previous state
    if (!cookiename) {
      cookiename = this.name + '_slideshow';
    }
    var search = cookiename + "=";
    if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search);
      if (offset != -1) {  // if cookie exists
        offset += search.length;  // set index of beginning of value
        end = document.cookie.indexOf(";", offset);  // set index of end of cookie value
        if (end == -1) end = document.cookie.length;
        this.current = parseInt(unescape(document.cookie.substring(offset, end)));
        }
     }
  }


  this.noscript = function() {  // Make a plain HTML version of the slideshow images and text
    $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(step) {// Automatic advancing to next slide, then setting next timeout
//   if (this.current < this.slides.length + step - 1) {  // Make sure the next slide image has finished loading
//      next_slide = this.slides[this.current + step - 1];
//      if (next_slide.image.complete == null || next_slide.image.complete) {
//        this.next(step);
//      }
//    } else {  // we're at the last slide
      this.next(step);
//    }
    this.play(this.timeout, step);  // Keep playing the slideshow
  }


  this.valid_image = function() {  // Return 1 if a valid image has been set for the slideshow
     if (!this.image)
    {
      return false;
    }
    else {
      return true;
    }
  }


  this.getElementById = function(element_id) {  // Return the element corresponding to the id
    if (document.getElementById) {
      return document.getElementById(element_id);
    }
    else if (document.all) {
      return document.all[element_id];
    }
    else if (document.layers) {
      return document.layers[element_id];
    } else {
      return undefined;
    }
  }
  

  this.set_image = function(imageobject) {  // Deprecated method, use: s.image = document.images.myimagename; s.update();
    if (!document.images)
      return;
    this.image = imageobject;
  }


  this.set_textid = function(textidstr) {  // Deprecated method, use: s.textid = "mytextid"; s.update();
    this.textid = textidstr;
    this.display_text();
  }
}