/*
Image roulation functions
Copyright (c) 2002 Ylab, http://www.ylab.nl
Author: Yohan Creemers
version 1.2
1.1: supports multiple slideshow's in one page
1.2: Minor bugfix for IE5.0

Sample use:
var show1 = new Slideshow("show1",2,4);
show1.addItem("../images/logo.gif", "Ylab, Design for new media");
<img onload="show1.initialize(this)">
*/


//Define Slideshow object
//objectName: name of the object
//delay: initial delay in seconds [optional, defaults to 3 seconds]
//interval: seconds between two slides [optional, defaults to 3 seconds]
function Slideshow(objectName, delay, interval){
  //Define properties
  this.name = objectName;
  this.delay = (!delay ? 3 : delay);
  this.interval = (!interval ? 3 : interval);
  this.init = false;
  this.img_sources = new Array();
  this.img_decriptions = new Array();
  this.imagearray = new Array();
  this.nLoadedImages = -1;
  this.nDefinedImages = -1;
  this.counter = 0;
  this.timeoutId;

  //Define methods
  this.addItem = method_AddItem;
  this.initialize = method_Initialize;
  this.preloadImage = method_PreloadImage;
  this.slideNext = method_Slidenext;
  this.showPicture = method_ShowPicture;
  this.stop = method_Stop;

  //METHODS
  //Add a slide to the show
  //image_src: path to the image
  //image_alt: descriptive text
  function method_AddItem(image_src, image_alt) {
    this.nDefinedImages++;
    this.img_sources[this.nDefinedImages]=image_src;
    this.img_decriptions[this.nDefinedImages]=image_alt;
  }

  //initialization
  //img: image object in the html page <img>
  function method_Initialize(img){
    //run only once
    if (this.init || !document.images) {return;}
    this.init = true; //been here

    this.slide=img;
    //put current images at the end of the image-array
    this.addItem(this.slide.src,this.slide.alt);
    //try to set filter
    if (this.slide.filters){
      this.slide.style.filter="blendTrans(duration=1)";
      this.slide.canPlay = !!this.slide.filters.blendTrans;
    }

    //Cache next slide
    this.preloadImage();
    //Set time for first transition
    var code = this.name + ".slideNext()";
    this.timeoutId = window.setTimeout(code, this.delay*1000);
  }

  //Cache images
  function method_PreloadImage() {
    if (!document.images) {return;}
    this.nLoadedImages++;
    this.imagearray[this.nLoadedImages] = new Image;
    this.imagearray[this.nLoadedImages].src = this.img_sources[this.nLoadedImages];
  }

  //Define what's the next image
  function method_Slidenext(){
    if (!document.images) {return;}
    if (this.counter < this.nLoadedImages) {this.counter++;}
    else {this.counter = 0;}
    this.showPicture();
  }

  //Replace picture by the next one
  function method_ShowPicture(){
    if (!document.images) {return;}
    if (this.slide.canPlay){this.slide.filters.blendTrans.apply();}
    this.slide.src=eval("this.imagearray["+this.counter+"].src");
    if (this.slide.canPlay) {this.slide.filters.blendTrans.play();}
    this.slide.alt=this.img_decriptions[this.counter];

    //Cache next slide (if neccessary)
    if (this.nLoadedImages < this.nDefinedImages){this.preloadImage();}
    //Schedule next transition
    var code = this.name + ".slideNext()";
    this.timeoutId = window.setTimeout(code, this.interval*1000);
  }

  function method_Stop(){
    if(this.timeoutId){
      window.clearTimeout(this.timeoutId);
    }
  }
}
