/*

Author: John Petricini
Application: Image Gallery - Switching an 'image' elements url with another
Website: thetofufacturoy.net
Reference: DOM Scripting by Jeremy Keith (great book)

*/

function showPic (whichPic) {
	//alert("showPic called");
	var source = whichPic.getAttribute("href");
	var text = whichPic.getAttribute("alt") ? whichPic.getAttribute("alt") : "";
	
	//if there is no image holder, return true so the browse will continue the link
	if (!document.getElementById("imageHolder")) return true;
	var imageHolder = document.getElementById("imageHolder");
	imageHolder.setAttribute("src", source);
	
	//if no description id, we can safely quit, if their isnt' an imageHolder there isn't going to be a description style for it
	if (!document.getElementById("description")) return false;
	imageHolder.setAttribute("alt", text);
	
	//if all goes well, return false so we don't follow the link
	return false;
}

function prepareGallery() {
	//alert("prepareGallery called");
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	
	//now that we know the browser is up-to-date with the script, check to see if the page's structure hasn't changed
	if (!document.getElementById("thumbnails")) return false;
	
	//now grab a reference to imagegallery so we can loop through all '<a>nchor elements' and attach an event handler to them
	var gallery = document.getElementById("thumbnails");
	var links = gallery.getElementsByTagName("a");
	
	for (var i=0; i < links.length; i++) {
		links[i].onclick = function () {
			return showPic(this);
		}
	}
}
