/*
 * Image gallery support code
 * Copyright 2007 Matthew Chapman
 */

var imagebase = "http://www.zmatt.net/photos-pub/";
var thumbctr = document.getElementById("thumbctr");
var currentthumb = thumbctr.firstChild.firstChild;
var timer = null;
var slideshow = false;

function setImage(thumb) {
	currentthumb.className="thumb";
	thumb.className="thumbSel";
	currentthumb=thumb;
	document.getElementById("image").src = imagebase + thumb.id + ".jpg";

	if ((thumb.offsetLeft-5) < thumbctr.scrollLeft)
		thumbctr.scrollLeft = thumb.offsetLeft-5;
	else if ((thumb.offsetLeft+155) > (thumbctr.scrollLeft+thumbctr.clientWidth))
		thumbctr.scrollLeft += thumb.offsetLeft+155-thumbctr.scrollLeft-thumbctr.clientWidth;
}

function nextImage() {
	if (currentthumb.nextSibling)
		setImage(currentthumb.nextSibling);
}

function prevImage() {
	if (currentthumb.previousSibling)
		setImage(currentthumb.previousSibling);
}

function rewind() {
	setImage(currentthumb.parentNode.firstChild);
}

function startSlideshow() {
	slideshow = true;
	document.getElementById("slideshow").className = "toggleSel";
	timer = window.setTimeout(currentthumb.nextSibling ? "nextImage()" : "rewind()", 1000);
}

function stopSlideshow() {
	slideshow = false;
	document.getElementById("slideshow").className = "toggle";
	if (timer) {
		window.clearTimeout(timer);
		timer = null;
	}
}

function startStopSlideshow() {
	if (slideshow) {
		stopSlideshow();
	} else {
		startSlideshow();
	}
}

function runSlideshow() {
	if (slideshow) {
		if (currentthumb.nextSibling) {
			if (timer)
				window.clearTimeout(timer);
			timer = window.setTimeout("nextImage()", 3000);
		} else {
			stopSlideshow();
		}
	}
}

function scrollleft() {
	thumbctr.scrollLeft = Math.floor((thumbctr.scrollLeft-5-thumbctr.clientWidth)/157)*157+5;
}

function scrollright() {
	thumbctr.scrollLeft = Math.floor((thumbctr.scrollLeft-5+thumbctr.clientWidth)/157)*157+5;
}
