
/*****************************************************************
Picture Slidshow funtions v1.0a
*********************************************************/


function slide(src,link,text,target,attr) {
	this.src = src;
	this.link = link;
	this.text = text;
	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.textarea;
	this.timeout = 3000;
	this.slides = new Array();
	this.current = 0;
	this.timeoutid = 0;

	this.add_slide = function(slide) {
		if (!document.images) { return; }
		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;
		}
		this.timeoutid = setTimeout( this.name + ".loop()", this.timeout);
	}
	this.pause = function() {
	    if (this.timeoutid != 0) {
			clearTimeout(this.timeoutid);
			this.timeoutid = 0;
		}
	}

	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.get_text = function() {
		return(this.slides[ this.current ].text);
	}

	this.get_all_text = function(before_slide, after_slide) {
		all_text = "";
		for (i=0; i < this.slides.length; i++) {
			slide = this.slides[i];
			if (slide.text) {
    	    	all_text += before_slide + slide.text + after_slide;
			}
		}
    	return(all_text);
	}

	this.display_text = function(text) {
		if (!text) {
			text = this.slides[ this.current ].text;
		}
  
		if (this.textarea) {
			this.textarea.value = text;
		}

		if (this.textid) {
			if (!document.getElementById){ return false; }
 
			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 (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.noscript = function() {
		$html = "\n";
		for (i=0; i < this.slides.length; i++) {
			
			slide = this.slides[i];
  
			$html += '<p>';
  
			if (slide.link) {$html += '<a href="' + slide.link + '">';}
  
			$html += '<img src="' + slide.src + '" ALT="">';
  
			if (slide.link) {$html += "<\/a>";}
  
			if (slide.text) {$html += "<br>\n" + slide.text;}
  
			$html += "<\/p>" + "\n\n";
		}
  
		$html = $html.replace(/\&/g, "&amp;" );
		$html = $html.replace(/</g, "&lt;" );
		$html = $html.replace(/>/g, "&gt;" );
  
		return('<pre>' + $html + '</pre>');
	}

	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 { 
			this.next();
		}
   
		this.play( );
	}

	this.valid_image = function() {
  
		if (!this.image){
			this.pause;
			window.status = "Error: Image Slideshow image not initialized for " + this.name;
        	return 0;
		}
		else {return 1;}
	}

	this.set_image = function(imageobject) {
		if (!document.images)
			return;
		this.image = imageobject;
	}

	this.set_textarea = function(textareaobject) {
		this.textarea = textareaobject;
		this.display_text();
	}

	this.set_textid = function(textidstr) {
		this.textid = textidstr;
		this.display_text();
	}

}
