var selectionStr = "_selection";

/* class Thumb */

function Thumb (link, folder, image, caption) {
	this.link = link;
	this.id = folder + "." + image;
	this.folder = folder;
	this.image = image;
	this.caption = caption;
}

Thumb.prototype.thumbHTML = function () {
	if (this.link) {
		return this.anchorRolloverHTML ();
	}
	else {
		return this.rolloverHTML ("thumb");
	}
}

Thumb.prototype.selectionHTML = function () {
	return '<div id="' + this.id + selectionStr + '" ><img src="' + this.folder + "/Images/" + this.image + '.jpg"/><p>' + this.caption + '</p></div>';
}

Thumb.prototype.anchorRolloverHTML = function () {
	return '<a href="' + this.link + '">' + this.rolloverHTML ("thumb_with_link") + '</a>';
}

Thumb.prototype.rolloverHTML = function (my_class) {
	return '<img class="' + my_class + '" id="' + this.id + '" src="' + this.folder + "/Thumbnails/" + this.image + '.jpg"  border="0" + onMouseOver="slider.makeVisible(this)" + onMouseOut="slider.makeHidden(this)" />';
}

/* Class Page */

function Page (thumbs) {
	this.thumbs = thumbs;
	this.previous = null;
	this.next = null;
}

Page.prototype.thumbsHTML = function () {
	var html = "";
	var l_thumbs = this.thumbs;
	for ( var i in l_thumbs ) {
		html = html + l_thumbs[i].thumbHTML ();
	}
	return html;
}

Page.prototype.selectionsHTML = function () {
	var html = "";
	l_thumbs = this.thumbs;
	for (var i in l_thumbs) {
		html = html + l_thumbs[i].selectionHTML ();
	}
	return html;
}

/* Class Slider */

function Slider (millisecs, thumbs) {
	this.millisecs = millisecs;
	this.rollovers = null;
	this.selected = null;
	this.description = null;
	this.currentPage = null;
	this.slideWidth = 0;
	this.period = 0;
	this.increment = 0;
	this.currentPosition = 0;
	this.nextSelection = null;
	this.currentSelection = null;
	this.slidingOut = false;
	this.sliding = false;
	this.first_page = null;
	
	/* Split the thumbs into groups of 5 */
	/* and link them together            */

	var previous_page;
	for (i=0; i<thumbs.length;) {
		var chunk = [];
		for (j=0;  j<5 && i<thumbs.length; j++, i++) {
			chunk [j] = thumbs [i];
		}
		var page = new Page (chunk);
		if (previous_page) { previous_page.next = page; }
		page.previous = previous_page;
		if (!this.first_page) { this.first_page = page; }
		previous_page = page
	}

}

Slider.prototype.reInitSlider = function () {
		this.rollovers = document.getElementById ("rollovers");
		this.selected = document.getElementById ("selected");
		this.description = document.getElementById ("description");
		this.slideWidth = parseInt(this.selected.style.width);
		this.period = Math.round (this.millisecs/50);
		this.increment = Math.round (this.slideWidth/this.period);
		if (this.sliding) {
			 this.currentSelection.style.display = "none";
			 this.selected.style.left = (-this.slideWidth) +"px";
			 this.description.style.left = "0px";
		}
		this.currentPosition = 0;
		this.currentPage = null;
		this.nextSelection = null;
		this.currentSelection = null;
		this.slidingOut = false;
		this.sliding = false;
}

Slider.prototype.goPrevious = function () {
		this.replaceRolloversWith (this.currentPage.previous);
}

Slider.prototype.noPrevious = function () {
		document.getElementById ("prev_blue").style.display = "none";
		document.getElementById ("prev_red").style.display = "none";
		document.getElementById ("prev_grey").style.display = "inline";
}

Slider.prototype.hoverPrevious = function () {
		document.getElementById ("prev_grey").style.display = "none";
		document.getElementById ("prev_blue").style.display = "none";
		document.getElementById ("prev_red").style.display = "inline";
}

Slider.prototype.outPrevious = function () {
	if (this.currentPage.previous) {
		document.getElementById ("prev_grey").style.display = "none";
		document.getElementById ("prev_red").style.display = "none";
		document.getElementById ("prev_blue").style.display = "inline";
	}
}

Slider.prototype.goNext = function () {
		if (this.currentPage) {
			this.replaceRolloversWith (this.currentPage.next);
		}
}

Slider.prototype.noNext = function () {
		document.getElementById ("next_blue").style.display = "none";
		document.getElementById ("next_red").style.display = "none";
		document.getElementById ("next_grey").style.display = "inline";
}

Slider.prototype.hoverNext = function () {
		document.getElementById ("next_grey").style.display = "none";
		document.getElementById ("next_blue").style.display = "none";
		document.getElementById ("next_red").style.display = "inline";
}

Slider.prototype.outNext = function () {
	if (this.currentPage.next) {
		document.getElementById ("next_grey").style.display = "none";
		document.getElementById ("next_red").style.display = "none";
		document.getElementById ("next_blue").style.display = "inline";
	}
}

Slider.prototype.hoverBack = function () {
		document.getElementById ("back_blue").style.display = "none";
		document.getElementById ("back_red").style.display = "inline";
}

Slider.prototype.outBack = function () {
	document.getElementById ("back_red").style.display = "none";
	document.getElementById ("back_blue").style.display = "inline";
}

Slider.prototype.replaceRolloversWith = function (page) {
		this.reInitSlider ();
		this.currentPage = page;
		this.rollovers.innerHTML = page.thumbsHTML ();
		this.selected.innerHTML = page.selectionsHTML ();
		if (page.previous) {
			this.outPrevious ();
		} else {
			this.noPrevious ();
		}			
		if (page.next) {
			this.outNext ();
		} else {
			this.noNext ();
		}			
}

Slider.prototype.slide = function () {
			if (!(this.sliding)) {
				this.reInitSlider ();
			} else if (this.slidingOut){
				if (!(this.currentSelection)) {
					this.currentSelection = this.nextSelection;
					this.next_selection = null;
					this.currentSelection.style.display = "block";
				}
				this.currentPosition += this.increment;
				if (this.currentPosition >= this.slideWidth) {
			 		this.currentPosition = this.slideWidth;
			 		this.description.style.display = "none";
					this.sliding = false;
				}	
			} else { //sliding in
				if (this.currentPosition >= this.slideWidth) {
					this.description.style.display = "block";
				}
				this.currentPosition -= this.increment;
				if (this.currentPosition <= 0) {
					this.currentPosition = 0;
					this.sliding = false;
					if (this.currentSelection) {
						this.currentSelection.style.display = "none";
					}
					this.currentSelection = null;
					if (this.nextSelection) {
						this.slidingOut = true;
						this.sliding = true;
					}
				}
			}
 /* 		this.description.style.left = this.currentPosition + "px"; */
  		this.selected.style.left = (this.currentPosition - this.slideWidth) +"px";
			if (this.sliding) setTimeout("slider.slide ()", this.period);
}

Slider.prototype.makeVisible = function (thumb_img) {
		this.nextSelection = document.getElementById (thumb_img.id + selectionStr);
		if (this.sliding) {
			this.slidingOut = false;
		} else {
			this.slidingOut = true;
			this.sliding = true;
			setTimeout ("slider.slide ()", this.period);
		}
}

Slider.prototype.makeHidden = function (thumb_img) {
		this.nextSelection = null;
		if (this.sliding) {
			this.slidingOut = false;
		} else {
			this.slidingOut = false;
			this.sliding = true;
			setTimeout ("slider.slide ()", this.period);
		}
}

