// Author: Sev Cardy, based on code by Ian Fyfe (Blue Pyramid Software Ltd.)
// Copyright (C) 2002 Blue Pyramid Software Ltd
// The following code is copyright and must not be used without prior permission from Blue Pyramid Software Ltd.

function slide(element, delay, src) {
	this.element = element;
	this.delay = delay;
	this.src = src;
	this.loaded = false;
}
function slideshow(delay,dir) {
	this.dir = (dir)? dir:''
	this.slides = new Array()
	this.last = -1
	this.repeat = false
	this.image = new Image
	this.delay = delay
	this.name="slideshow"+(slideshow.count++)
	this.obj=this.name+"Object"
	eval(this.obj+"=this")
	this.add = slideshowadd
	this.start = slideshowstart
	this.showslide = showslide
	this.refresh = slideshowrefresh
}
function slideshowadd(element) {
	var iarg=1, index=this.slides.length
	while (iarg<arguments.length){
		this.slides[index] = new slide(element, this.delay, this.dir+arguments[iarg++]);
		if (element) this.slides[index].element.onload="";
		index++
	}
	this.last = this.slides.length-1
}

function slideshowstart(index) {
	if (!index) index=0
	if (document.images) {
		if (arguments.length > 1) {
			var rep = arguments[1].toUpperCase()
			this.repeat=(rep="REPEAT");
		}
		if (this.last>=0) {
			if (this.slides[index]!=null) {
				this.image.src=this.slides[index].src;
				this.index=index;
				if (!this.slides[index].loaded) {
					//window.status = "Loading image " + index + ": " + this.slides[index].src;
				}
				window.setTimeout(this.obj +".showslide(" + index + ")", this.slides[index].delay);
			} else {
				// IMG onload needs time to complete before document onload can be processed!
				if (index < this.last) {
					window.setTimeout(this.obj +".start(" + (++index) + ")", 500);
				} else{
					if (repeat) window.setTimeout(this.obj +".start(0)", 500);
				}
			}
		} else {
			// IMG onload needs time to complete before document onload can be processed!
			window.setTimeout (this.obj + ".start(" + index + ")", 500);
		}
	}
}

function slideshowrefresh() {
	if (document.images) {
		var index;
		var previous=null;
		for (index = 0; index<=this.last; index++) {
			if (this.slides[index]) {
				with (this.slides[index]) {
					if (loaded && (element!=previous)) {
						element.src = src;
						previous = element;
					}
				}
			}
		}
	}
}

function showslide(index){
	with (this) {
		if (slides[index]!=null) {
			with (slides[index]) {
				if (loaded || image.complete) {
					loaded = true;
					//window.status = "Done:" + index;
					if (element) {
						if (element.filters) {
							if (element.filters.length>0) {
								element.filters[0].Apply();
								element.src = src;
								element.filters[0].Play();
							}
							else
								element.src = src;
						}
						else
							element.src = src;
					}// no action for null element. Img pre-loaded for future use.

					if (index < last){
						window.setTimeout (this.obj+".start("+ (++index)+")",500);
					} else {
						if (repeat) window.setTimeout (this.obj+".start(0)",500);
					}
				} else {
					window.setTimeout (this.obj+".showslide("+index+")",500);
				}
			}
		}
	}
}
slideshow.count=0
