


function imageGallery(galleryEl, height, width) {
	this.curr = -1;
	this.imgs = new Array();
	this.imgEl;
	this.captionEl;
	var self = this;
	
	if(typeof galleryEl == "string")
		galleryEl = document.getElementById(galleryEl);
	
	galleryEl.className = "imageGallery";
	galleryEl.innerHTML = "";
	galleryEl.style.cssText = "";
	
	var mainImg = document.createElement("img");
	if(height)
		mainImg.height = height;
	if(width)
		mainImg.width = width;
	mainImg.className = "mainImage";
	//mainImg.src = imgs[0].src

	galleryEl.appendChild(mainImg);
	this.imgEl = mainImg;

	//nav table
	var table = document.createElement("table");
	table.width = "100%";
	table.className = "imageGalleryNav";
	var tbody = document.createElement("tbody");
	var tr = document.createElement("tr");

	var td = document.createElement("th");
	td.className = "imageGalleryNavOut";
	td.style.cursor = "pointer";
	td.align = "center";
	td.onmouseover = mouseOver;
	td.onmouseout = mouseOut;
	td.innerHTML = "&lt; BACK";
	td.onclick = function() { self.previous(); };
	tr.appendChild(td);

	var td = document.createElement("th");
	td.className = "imageGalleryNavOut";
	td.style.cursor = "pointer";
	td.align = "center";
	td.onmouseover = mouseOver;
	td.onmouseout = mouseOut;
	td.innerHTML = "NEXT &gt;";
	td.onclick = function() { self.next(); };
	tr.appendChild(td);

	tbody.appendChild(tr);

	var tr = document.createElement("tr");
	var td = document.createElement("td");
	td.className = "imageGalleryCaption";
	td.colSpan = 2;
	tr.appendChild(td)
	this.captionEl = td;

	tbody.appendChild(tr);
	
	table.appendChild(tbody);
	galleryEl.appendChild(table);

	function mouseOver(ev) { 
		var td = (ev) ? ev.target : window.event.srcElement;
		td.className = 'imageGalleryNavOver'; 
	}
	function mouseOut(ev) { 
		var td = (ev) ? ev.target : window.event.srcElement;
		td.className = 'imageGalleryNavOut'; 
	}
}
imageGallery.prototype.setImages = function() {
	if(arguments.length) {
		for(var i=0; i < arguments.length; i++) {
			var img = new Image();
			img.src = arguments[i];
			this.imgs[this.imgs.length] = img;
		}
		this.curr = 0;
		this.showImage();
	}
}
imageGallery.prototype.showImage = function() {
	//this.curr += dir;
	if(this.curr < 0)
		this.curr = this.imgs.length - 1;
	else if(this.curr >= this.imgs.length)
		this.curr = 0;
	this.imgEl.src = this.imgs[this.curr].src;	
	this.captionEl.innerHTML = "Image " + (this.curr + 1) + " of " + this.imgs.length;
}

imageGallery.prototype.next = function(dir) {
	this.curr += 1;
	this.showImage();
}
imageGallery.prototype.previous = function(dir) {
	this.curr -= 1;
	this.showImage();
}
imageGallery.prototype.first = function(dir) {
	this.curr = 0;
	this.showImage();
}
imageGallery.prototype.last = function(dir) {
	this.curr = this.imgs.length - 1;
	this.showImage();
}
