// Image management Javascript:
// Image manipulation adapted from original by Jeremy Keith: http://www.domscripting.com
// addLoadEvent courtesy of Simon Willison: http://simon.incutio.com

// prepareImageNav: Set up image navigation links to swap images around
function prepareImageNav() {

	// DOM moat: If you can't hack it, go home.
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	
	// Check: Does this page have a swappable gallery img?
	if (!document.getElementById("galleryimg")) return false;
	
	// Get all the links in the "images" div, give them img swapping functionality
	var images_div = document.getElementById("images");
	var nav_list = images_div.getElementsByTagName("a");
	for ( var i=0; i < nav_list.length; i++ ){
		nav_list[i].onclick = function() {
			return showImage(this);
		}
	}
}

// Run prepareImageNav when the page loads
addLoadEvent(prepareImageNav);

//======================================

// showImage: Swap a new image into the target image
function showImage(newimage) {

	if (!document.getElementById("galleryimg")) return true;
	
	// IMAGE EDITING
	// Get new img src from URL
	var new_pointer = new String(newimage.getAttribute("href"));
	var new_pointer_split = new_pointer.split("=");
	var new_src = new_pointer_split[1] + ".jpg";
	var new_txt = img_texts[new_pointer_split[1]];	// Uses global variable set in header.php
	
	// Find the target image and reset that bad larry!
	var gallery_img = document.getElementById("galleryimg");
	if (gallery_img.nodeName != "IMG") return true;
	gallery_img.setAttribute("src", new_src);	// Image
	gallery_img.setAttribute("alt", new_txt);	// Alt text
	gallery_img.setAttribute("title", new_txt);	// Title text
	
	// STYLE EDITING
	// Unset the previous "current" nav object ("className" must be used for IE)
	var imageholder = document.getElementById("galleryimg").parentNode.parentNode;
	var image_nav = imageholder.getElementsByTagName("a");
	for (var i = 0; i < image_nav.length; i++) {
		if (image_nav[i].className == "current"){
			image_nav[i].className = null;
		}
	}
	
	// Set the selected one to be current
	newimage.className = "current";	
	
	return false;
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
