function showPic(whichpic) {
	if (!document.getElementById("placeholder")) return true;
	var source = whichpic.getAttribute("href");
	var placeholder = document.getElementById("placeholder");
	if (placeholder.nodeName != "IMG") return true;
	placeholder.setAttribute("src",source);
	
	if (!document.getElementById("description")) return false;
	if (whichpic.getAttribute("title")) {
		var text = whichpic.getAttribute("title");
	}else{
		var text ="";
	}
	var description = document.getElementById("description");
	if (description.firstChild.nodeType == 3) {
		description.firstChild.nodeValue = text;
	}

	return false;
}


function prepareGallery() {
        if (!document.getElementsByTagName || !document.getElementById) return false;
        if (!document.getElementById("imagegallery")) return false;

        var gallery = document.getElementById("imagegallery");
        var links = gallery.getElementsByTagName("a");

        for (var i=0; i < links.length; i++) {
                links[i].onclick = function() {
                        return showPic(this);
                }
        }
}


function preparePlaceholder() {
	if (!document.createElement || !document.createTextNode || !document.getElementById) return false;
	if (!document.getElementById("rightSide")) return false;
	var placeholder = document.createElement("img");
	placeholder.setAttribute("id","placeholder");
	placeholder.setAttribute("src","images/placeholder.gif");
	placeholder.setAttribute("alt","my image gallery");
	var description = document.createElement("p");
	description.setAttribute("id","description");
	var desctext = document.createTextNode("Please click on a thumbnail to view images");
	description.appendChild(desctext);
	var gallery = document.getElementById("rightSide");
	insertAfter(placeholder,gallery);
	insertAfter(description,placeholder);
}


/*--------------General purpose functions------------------*/

//function to insert a node after another node
//this was in DOM Scripting by Jeremy Keith
function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if(parent.lastChild == targetElement) {
	parent.appendChild(newElement);
	}else{
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}


//funtion to load multiple functions on page load
//this is written by Simon Willison (www.simon.incutio.com/)
function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
                window.onload =func;
        }else{
                window.onload = function() {
                        oldonload();
                        func();
                }
        }
}

//now to pass it the functions i want invoked when page downloads
addLoadEvent(preparePlaceholder);
addLoadEvent(prepareGallery);