function getElement(id){
	return (document.getElementById ? document.getElementById(id) : document.all[id]);
}

/* Goto next image */
function preview_next(){
	var photo = getElement('photo_view');
	update_photo(photo.imgId+1); //iteration+1
}

/* Goto previous image */
function preview_back(){
	var photo = getElement('photo_view');
	update_photo(photo.imgId-1);
}

/* Preview named images */
function previewImg(){
	update_photo(this.imgId);
	return false;
}

function update_photo(num){
	/* If we're at the start, go to the end */
	if(num < 1){
		num = imgTotal;
	/* If we're at the end, go to the start */
	}else if(num > imgTotal){
		num = 1;
	}

	var photo = getElement('photo_view');
	photo.src = imgs[num].src;
	/* Safari was stretching imgs to a default width, not the imgs width.
	   This fixes it and doesn't seem to break anything */
	photo.width = imgs[num].width;
	photo.imgId = num;
}

/* Initalize variables for preloading images */
var imgs = new Array();
/* imgTotal is per-page.  Make global for use in preview functions */
var imgTotal;
window.onload = function (){
		var photo_list = getElement('photo_list');
		var length = photo_list.childNodes.length;
		/* y used for imgs[] key.  Start at one to reflect
		   that the img names start at 1 (makes next/back really easy) */
		for(var x=0, y=1; x < length; x++){
			var child = photo_list.childNodes[x];
			if(child.tagName == 'IMG'){
				/* Attach click event */
				child.onclick = previewImg;
				/* Since imgId is not valid html, add the attribute to the JS object through JS. */
				child.imgId = y;

				/* Pre-load image */
				imgs[y] = new Image;
				/* Image name = thumbs name - "-sm" */
				imgs[y].src = child.src.replace(/\/([0-9]+)-sm\.jpg/, '/$1.jpg');
				y++;
			}
		}

		/* y starts at one, imgs[] auto-adds a 0 key, so the actual num of imgs = length-1 */
		imgTotal = imgs.length-1;

		/* Since imgId is not valid html, add the attribute to the JS object through JS.
		   Using match() because we might not start with image "1" */
		var photo = getElement('photo_view');
		photo.imgId = parseInt(photo.src.match(/\/([0-9]+)\.jpg/)[1]);
	}