var isplaying; var rt;

function slide(src,link,text,textdesc,target,attr,alt) {
  
  this.src = src;
  this.link = link;
  this.text = text;
  this.textdesc = textdesc;
  this.target = target;
  this.attr = 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) {
      if (this.attr) {
        window.open(this.link, this.target, this.attr);
  
      } else {
        window.open(this.link, this.target);
      }

    } else {
      location.href = this.link;
    }
  }
}

function slideshow( slideshowname ) {
	
  this.name = slideshowname;
  this.repeat = true;
  this.prefetch = -1;
  this.image;
  this.textid;
  this.textdescid;

  this.timeout = 4000;

  this.slides = new Array();
  this.current = 0;
  this.timeoutid = 0;
	
  // Add Slide
  this.add_slide = function(slide) {
  
    if (!document.images) { return; }
    var i = this.slides.length;
    if (this.prefetch == -1) {
      slide.load();
    }
    this.slides[i] = slide;
  }
	
   // Play Slide	
  this.play = function(timeout) {
	//Allow play if more than one image
	if (this.slides.length > 1) {
		this.pause();
		isplaying = true;
		this.whileplaying(isplaying);
	
	    if (timeout) {
	      this.timeout = timeout;  
	    }
	    this.timeoutid = setTimeout( this.name + ".loop()", this.timeout);
	} else {
		//Hide Slideshow buttons if only one photo
		var photoprevnextobj = document.getElementById('photoprevnext');
		photoprevnextobj.style.display = "none";
	}
  }

  this.whileplaying = function(isplaying) {
  		var playingobj = document.getElementById('playingstate');
		
		if(isplaying && playingobj.style.display != "") {
			playingobj.style.display = "";
		} else {
			playingobj.style.display = "none";
			if (this.timeoutid != 0)
			    {
			      clearTimeout(this.timeoutid);
			      this.timeoutid = 0;
			    }
		}
  };

  // Pause Slide	
  this.pause = function() {
  
	if (this.timeoutid != 0)
    {
      clearTimeout(this.timeoutid);
      this.timeoutid = 0;
    }
	isplaying = false;
	this.whileplaying(isplaying);
  }

  // Update	
  this.update = function() {

    var slide = this.slides[ this.current ];
    var dofilter = (this.image.filters && this.image.filters[0]);

    if (! this.valid_image()) { return; }
  
    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();
    }

    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();
  }
  
  //Next
  this.next = function() {
  	//Close Thumbview
  	var obj = document.getElementById('imgthumbholder');
	obj.style.display = 'none';
	var obj2 = document.getElementById('imgthumbholderbg');
	obj2.style.display = 'none';
	
	isplaying = false;
	this.whileplaying(isplaying);
	if (this.current < this.slides.length - 1) {
      this.current++;
    } else if (this.repeat) {
      this.current = 0;
    }
  
    this.update();
  }

  //Previous
  this.previous = function() {
	//Close Thumbview
  	var obj = document.getElementById('imgthumbholder');
	obj.style.display = 'none';
	var obj2 = document.getElementById('imgthumbholderbg');
	obj2.style.display = 'none';
	
	isplaying = false;
	this.whileplaying(isplaying);
	if (this.current > 0) {
      this.current--;
    } else if (this.repeat) {
      this.current = this.slides.length - 1;
    }
  
    this.update();
  }

  //Text
  this.get_text = function() {
    return(this.slides[ this.current ].text);
  }

  this.display_text = function(text, textdesc) {
    
	if (!text) {
      text = this.slides[ this.current ].text;
    }
	if (!textdesc) {
      textdesc = this.slides[ this.current ].textdesc;
    }

	if (this.textdescid) {
      if (!document.getElementById){ return false; }
      var rt = document.getElementById(this.textdescid);
      if (!rt) { return false; }
      rt.innerHTML = textdesc;
    }
    if (this.textid) {
      if (!document.getElementById){ return false; }
      var r = document.getElementById(this.textid);
      if (!r) { return false; }
      r.innerHTML = text; 
    }
  }

  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 cookie exists
      if (offset != -1) { 
        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.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();
    }
    this.play();
  }

  //--------------------------------------------------
  this.valid_image = function() {
    if (!this.image)
    {
      this.pause;
      window.status = "Error: slideshow image not initialized for " + this.name;  
      return 0;
    }
    else {
      return 1;
    }
  }
  
  this.set_textid = function(textidstr) {
  // This function is deprecated; you should use
  // the following code instead:
  // s.textid = "mytextid";
  // s.update();
    this.textid = textidstr;
    this.display_text();
  }

}

