//------------------------------------------------
// modifications
//------------------------------------------------

 button_of_play = 2;		
 	// button number of "play/stop" button
	// within first form on form

//------------------------------------------------
// Slide Object
//------------------------------------------------
 
function slide(src,text,target,attr) {
  this.src = "../files/" + docroot + "/" + src + ".jpg";
  this.link = "../files/"+ docroot + "/" + src + "_big.jpg";
  this.text = text;
  this.target = "_blank"; //  target;
  // 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 = "resizable=yes,scrollbars=yes,menubar=yes,location=no,status=no";	// attr;
  if (document.images) {
    this.image = new Image();
  }

  this.load = function() {
    if (!document.images) { return; }
    if (this.image.src != this.src) {
      this.image.src = this.src;
    }
  }

  this.hotlink = function() {
    if (this.target) {
	  this.slideshow.pause();
      if (this.attr) {
        window.open(this.link, this.target, this.attr);
      } else {
        window.open(this.link, this.target);
      }
    } else {
      location.href = this.link;
    }
  }
  
}	// function slide()


//------------------------------------------------
// Slideshow object
//------------------------------------------------

function slideshow( slideshowname,delay ) {
  this.name = slideshowname;
  this.repeat = true;
  this.prefetch = -1;
	  // -1 = preload all images.
	  //  0 = load each image is it is used.
	  //  n = pre-fetch n images ahead of the current image.
  this.image;
	  // IMAGE element on your HTML page.
	  // For example, document.images.SLIDES1IMG
  this.textid;
	  // ID of a DIV element on your HTML page that will contain the text.
	  // For example, "slides2text"
  this.infoid;
  	  // ID of DIV element to show info about the slide
  this.buttonid;
   	  // ID of a button for stop/start toggle
  this.timeout = delay;
	  // Milliseconds to pause between slides

  // These are private variables
  
  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;
	slide.slideshow = this;
  }
  
  this.startstop = function()  {
  	if (this.timeoutid)	{
		this.pause();
	} else {
		this.play();
	}
  }
    
  this.play = function(timeout) {
    this.pause();
    if (timeout) {
      this.timeout = timeout;
    }
    this.timeoutid = setTimeout( this.name + ".loop()", this.timeout);
	// document.forms[0].elements[button_of_play].value = "stop";
      if (!document.getElementById){ return false; }
      r = document.getElementById(this.buttonid);
	  r.value = "stop";
  }

  this.pause = function() {
    if (this.timeoutid != 0)
    {
      clearTimeout(this.timeoutid);
      this.timeoutid = 0;
  	  // document.forms[0].elements[button_of_play].value = "play";
      if (!document.getElementById){ return false; }
      r = document.getElementById(this.buttonid);
	  r.value = "play";
    }
  }

  this.update = function() {
    if (! this.valid_image()) { return; }
    this.slides[ this.current ].load();
    this.image.src = this.slides[ this.current ].image.src;
    this.display_text();
    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.display_text = function(text) {
    if (this.textid) {
	  if (!text) {
		text = this.slides[ this.current ].text; 
	  }
      if (!document.getElementById){ return false; }
      r = document.getElementById(this.textid);
      if (!r){ return false; }
      r.innerHTML = text;
    }
    if (this.infoid) {
      info = "[" + (this.current+1) + 
		     "/" + this.slides.length +
			 "]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + this.slides[ this.current ].src;
	  if (!document.getElementById){ return false; }
      r = document.getElementById(this.infoid);
      if (!r){ return false; }
      r.innerHTML = info;
    }
  }

  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.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();
    }
    // Keep playing the slideshow
    this.play( );
  }


  this.valid_image = function() {
    if (!this.image)
    {
      this.pause;
      // Display an error message
      window.status = "Error: slideshow image not initialized for " + this.name;
      return 0;
    }
    else {
      return 1;
    }
  }

}







