/* object type: slide

   simple object to hold an image in a slide show

   attributes:
     src - URI for slide image
     alt - alt attrib to apply to IMG tag
     image - Image obj ref

   methods:
     load - loads the image

   constructor:
     slide(src,alt) - construct new slide with specific src and alt attrib

   usage:
     s = new slide("slides/img1.gif","first image");
*/
function Slide(src,alt)
{
    this.src = src != undefined ? src : "";
    this.alt = alt != undefined ? alt : "";

    if (alt) { this.alt = alt; }
    
    this.alt = alt;

    if (document.images) {
      // browser supports Image object
      this.image = new Image();
    }

    this.load = function() {
        // "load" only works if browser supports Image object
        if (!document.images) { return; }

        if (this.image.src != this.src) {
            // browser supports Image object
            this.image.src = this.src;
        }
    }
}

/* object type: slideshow

   slide show animation controller. if supported by browser, will use a
   blend transition between slides.

   attributes:
     imgName - value of name attribute of IMG tag this slideshow controls
     current - index of current slide
     crossfade - duration of blend transition in seconds, 0 for no transition
     delay - duration of pause between slides in millisec
     preload - number of images to preload, -1 to preload all
     repeat - (boolean) repeat show after reaching last slide?
     
   methods:
     addSlide - adds a slide. slides are played in order they are added.
     next - show next slide (does not reset delay timer)
     play - start/continue automated play of slideshow

   constructor:
     slideshow(imgName) - construct slideshow with specified name attrib

 */
function SlideShow(self,imgName)
{
    this.self = self;
    this.imgName = imgName;

    // defaults
    this.current = -1;
    this.crossfade = 1;
    this.delay = 5000;
    this.preload = 1;           // preload only 1 image
    this.repeat = true;


    this.slide = new Array();

    this.addSlide = function(newSlide) {        
        // slide show requires browser support for document image array
        if (!document.images) { return; }

        // new slide will be appended to array
        var i = this.slide.length;

        if (this.preload == -1 || (this.preload == 1 && i == 0)) {
          newSlide.load();
        }

        this.slide[i] = newSlide;
    }
    
    this.next = function() {
        // slide show requires browser support for document image array
        if (!document.images) { return; }

        // use transition if IE
        var useTransition = document.all && (this.crossfade > 0);

        if (this.repeat)
          {
              this.current = (this.current + 1) % this.slide.length;
          }
        else
          {
              if (this.current < (this.slide.length - 1)) {
                  this.current++;
              }
          }   
    
        var img = document.images[this.imgName];

        // IE: use blended transition
        if (useTransition) {
          img.style.filter = "blendTrans(duration=" + this.crossfade + ")";
          img.filters.blendTrans.apply();
          img.filters.blendTrans.play();
        }
        
        // load new image
        img.src = this.slide[this.current].src;
        img.alt = this.slide[this.current].alt;

        // preload next image
        for (var i=this.current+1; (i < this.preload && i < this.slide.length);
             i++)
          {
            this.slide[i].load();
          }
    }

    this.play = function() {
        // slide show requires browser support for document image array
        if (!document.images) { return; }
        
        this.next();

        // request play() to be called again after delay ms.
        if (this.delay > 0) {
          var d = setTimeout(this.self + ".play()", this.delay);
        }
    }
}
