
function slide(src) {
  this.src = src;
  // this.timeout = 3000

  if (document.images) {
    this.image = new Image();
  }

  this.loaded = false;
  this.load = function() {
    if (!document.images) { return; }
    if (!this.loaded) {
      window.status = "Loading Images, Please Wait...";
      this.image.src = this.src;
      this.loaded = true;
    }
  }
 
}

function outOf(c){
	window.status = "Displaying Slide " + (c + 1) + " of " + i;
}
function slideshow(slideshowname, toggleButton) {
  this.name = slideshowname;
  this.repeat = true;
  // -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;
  this.image;
  this.timeout = 2500;
  this.slides = new Array();
  this.current = 0;
  this.timeoutid = 0;
  this.btnSlideshow = toggleButton;
  this.allowMouseOver = true;
  this.thumbnailObj = null;
  this.thumbnailClass = "miniThumbnails";
  this.thumbnailSelectedClass = "miniThumbnailsSelected";

  this.add_slide = function(slide) {
    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;
    }
    outOf(this.current);
    this.timeoutid = setTimeout( this.name + ".loop()", timeout);
  }


  this.pause = function() {
    if (this.timeoutid != 0) {
      clearTimeout(this.timeoutid);
      this.timeoutid = 0;
    }
  }


  this.update = function() {
   
    if (! this.valid_image()) { return; }
    var slide = this.slides[ this.current ];
    var dofilter = false;
    if (this.image &&
        typeof this.image.filters != 'undefined' &&
        typeof this.image.filters[0] != 'undefined') {
      dofilter = true;
    }
    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();
    }

    if (this.prefetch > 0) {
      var next, prev, count;
      next = this.current;
      prev = this.current;
      count = 0;
	  
      do {
        if (++next >= this.slides.length) next = 0;
        if (--prev < 0) prev = this.slides.length - 1;
        this.slides[next].load();
        this.slides[prev].load();


      } while (++count < this.prefetch);
    }
  }


  this.next = function() {
    if (this.current < this.slides.length - 1) {
      this.current++;
    } else if (this.repeat) {
      this.current = 0;
    }

    this.update();
   	outOf(this.current);
  }


  this.previous = function() {
    if (this.current > 0) {
      this.current--;
    } else if (this.repeat) {
      this.current = this.slides.length - 1;
    }
  
    this.update();
   	outOf(this.current);
  }


  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 { // last slide
      this.next();
    }

    this.play( );
  }



  this.valid_image = function() {
    // Returns 1 if a valid image has been set for the slideshow
  
    if (!this.image)
    {
      return false;
    }
    else {
      return true;
    }
  }


  this.getElementById = function(element_id) {
    // This method returns 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.toggleSlideshow = function() {
	window.status = "";
	if (this.thumbnailObj) {
		this.thumbnailObj.className = this.thumbnailClass;
		this.thumbnailObj = null;
	}
	if(this.playing){
		this.stopSlideshow();
		this.allowMouseOver = true;
	}else{
		this.startSlideshow();
		this.allowMouseOver = false;
	}
  }
  
  this.stopSlideshow = function() {
  	this.pause();
	this.playing = false;
	this.getElementById(this.btnSlideshow).innerHTML = "start slideshow &raquo;";
  }
  
  this.startSlideshow = function() {
  	this.next();
	this.play();
	this.playing = true;
	this.getElementById(this.btnSlideshow).innerHTML = "stop slideshow";
  }
  
  this.showSlide = function(slideNumber) {
  	if(this.playing){
  		this.stopSlideshow();
  	}
  	window.status = "";
	this.current = slideNumber;
	this.update();
	try {
		var slide = this.slides[ slideNumber ];
		this.image.src = slide.src;
  		return false;
  	} catch (ex) { 
  		//alert(ex);
  		return false;
  	}
  }

  this.showSlideClick = function(slideNumber, obj) {
	if (this.thumbnailObj) {
		this.thumbnailObj.className = this.thumbnailClass;
	}
  	if (this.thumbnailObj == obj) {
		this.allowMouseOver = true;
		this.thumbnailObj = null;
	} else {
		obj.className = this.thumbnailSelectedClass;
		this.allowMouseOver = false;
		this.showSlide(slideNumber);
		this.thumbnailObj = obj;
	}
	return false;
  }
  this.showSlideMouseOver = function(slideNumber) {
	if (this.allowMouseOver) {
		this.showSlide(slideNumber);
	}
	return false;
  }

}

