function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			};
		}
	}
	
	function stripImgName(blah){

	var thisHref = blah.href;
	
	var hrefArray = thisHref.split("/");
	var secondSplit = hrefArray[hrefArray.length-1].split(".");
	var newHref = "./bpimages/gallery/" + secondSplit[0] + ".jpg";
	
	return newHref;
	
	}
	
	function insertAfter(newElement, targetElement){
		
		var parent = targetElement.parentNode;
		if (parent.lastChild == targetElement) {
			parent.appendChild(newElement);
		} else { parent.insertBefore(newElement,targetElement.nextSibling); }
		
	}
	
	function prepareReplacement() {
		
		if (!document.createElement) { return false; }

		if (!document.getElementById || !document.getElementById('thumb_list')) { return false; }

		var replacement = document.createElement('img');
		replacement.setAttribute('id','replacement');
		replacement.setAttribute('src','./bpimages/gallery/1-lg.jpg');
		replacement.setAttribute('alt','');
		replacement.setAttribute('width','363');
		replacement.setAttribute('height','226');

		document.getElementById('photo_frame').appendChild(replacement);

	}
	
	function prepareThumbs() {
		
		if (!document.createElement) { return false; }
		if (!document.getElementById || !document.getElementById('thumb_list')) { return false; }

		var thumb_list = document.getElementById('thumb_list');
		var links = thumb_list.getElementsByTagName('a');
		for (var i=0;i<links.length;i++) {
			links[i].onclick = function() { 
				return changePic(this);
			};
		}
	}

	function changePic(whichPic) {
		
		if (!document.getElementById('replacement')) { return true; }
	
	
		var source = stripImgName(whichPic);
		var photo_frame = document.getElementById('photo_frame');
		var replacement = document.getElementById('replacement');
		var caption = document.getElementById('caption');
		var captionText = whichPic.title;
		
		if (replacement.style.display != 'none'){
			photo_frame.style.backgroundImage = 'url(' + source + ')';
			replacement.style.display = 'none';
			caption.innerHTML = captionText;
		} else {
			replacement.setAttribute('src',source);
			replacement.style.display = '';
			caption.innerHTML = captionText;
		}
		return false;
	}

	addLoadEvent(prepareReplacement);
	addLoadEvent(prepareThumbs);

window.addEvent('domready', function(){
});
							

