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) {
			this.image.src = this.src;
			this.loaded = true;
		}
	}
}

function outOf(c){
	if(document.ss_form){
		document.ss_form.counter.value = (c + 1) + " of " + ss.slides.length;
	}
}

function slideshow(slideshowname) {
	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 = 2;
	this.image;
	this.timeout = 3000;
	this.slides = new Array();
	this.current = 0;
	this.timeoutid = 0;

	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;
		}
	}

	var ss_last_img_portrait = false;
	
	this.update = function() {
		// get current img dimensions:
		var pic_w = dims[this.current][0];
		var pic_h = dims[this.current][1];
		
		// only shows with large slideshow
		if(page == 'large'){
			//var default_w = 530;
			//var default_h = 630;
			// check if landscape or portrait
			var scaler_x = default_w / pic_w;
			var scaler_h = default_h / pic_h;
			var ss_ratio = "L";
			if(pic_w <= pic_h){
				// portrait
				document.images["ss_img"].width = pic_w * scaler_h;
				document.images["ss_img"].height = pic_h * scaler_h;
				ss_ratio = "P";
			}else{
				//landscape
				document.images["ss_img"].width = pic_w * scaler_x;
				document.images["ss_img"].height = pic_h * scaler_x;
			}
		}else{
			var scaler = 1;
			if(pic_w <= pic_h){
				scaler = max_h / pic_h;
				// portrait
				document.images["ss_img"].height = pic_h * scaler;
				document.images["ss_img"].width = pic_w * scaler;
				
				ss_last_img_portrait = true;
			}else{
				if(!max_w){
					document.images["ss_img"].width = pic_w;
					document.images["ss_img"].height = pic_h;
				}else{
					scaler = max_w / pic_w;
					// constraint on width
					document.images["ss_img"].width = max_w;
					document.images["ss_img"].height = pic_h * scaler;
				}			
				ss_last_img_portrait = false;
			}
		}
		if(document.getElementById('room_name')){
			document.getElementById('room_name').innerHTML = "<b>"+dims[this.current][2]+"</b> " + dims[this.current][3];
		}
		
		// end large slideshow
		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() {
		clearTimeout(ss.timeoutid);
		if (this.current < this.slides.length - 1) {
			this.current++;
		} else if (this.repeat) {
			this.current = 0;
		}
	
		this.update();
		outOf(this.current);
		ss.play();
	}
	
	
	 this.previous = function() {
		clearTimeout(ss.timeoutid);
		if (this.current > 0) {
			this.current--;
		} else if (this.repeat) {
			this.current = this.slides.length - 1;
		}
	
		this.update();
		outOf(this.current);
		ss.play();
	}
	
	
	 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;
		}
	}

}
