function swapPic(picture) {
        if (!document.getElementById("placeholder")) return true;
        var source = picture.getAttribute("href");
        var placeholder = document.getElementById("placeholder");
        if (placeholder.nodeName != "IMG") return true;
        placeholder.setAttribute("src",source);
        return false;
}


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

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

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


function preparePlaceholder() {
        if (!document.createElement || !document.createTextNode || !document.getElementById) return false;
        if (!document.getElementById("galleryRight")) return false;
        var placeholder = document.createElement("img");
        placeholder.setAttribute("id","placeholder");
        placeholder.setAttribute("src","art/placeholder.gif");
        placeholder.setAttribute("alt","my image gallery");
        var gallery = document.getElementById("galleryRight");
        insertAfter(placeholder,gallery);
}


/*--------------General purpose functions------------------*/
function insertAfter(newElement,targetElement) {
        var parent = targetElement.parentNode;
        if(parent.lastChild == targetElement) {
        parent.appendChild(newElement);
        }else{
                parent.insertBefore(newElement,targetElement.nextSibling);
        }
}

/*--------------open in new window functions------------------*/
//this is written by Andrew Porter Glendinning (www.serve.com/)
function pop() {
	
	// Fetch all the a elements in the document.
	var links = document.getElementsByTagName('a');

	// Loop through the a elements in reverse order for speed.
		for (var i = links.length; i != 0; i--) {
			
			// Pull out the element for this iteration.
			var a = links[i-1];
			
			// If the element doesn't have an href, skip it.
			if (!a.href) continue;

			// If the element has a className that contains 'external' attach the onclick handler.
			if (a.className && a.className.indexOf('external') != -1) a.onclick = PopWin;
	}
}
	
function PopWin(e) {
	
	// Accommodate IE's non-standard event handling.
	if (!e) var e = window.event;
	var a = e.target ? e.target : e.srcElement;

	// Open a new window with the link's href.
	var newwin = window.open(a.href);

	return !newwin;                               
}


/*--------------addload------------------*/
//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);
addLoadEvent(pop);
