window.onload = prepareGallery;
			
function prepareGallery () {
	var galleryList  = document.getElementById("galleryList");
	var galleryLinks = galleryList.getElementsByTagName("a");
	
	for (var i = 0; i < galleryLinks.length; i++) {
		galleryLinks[i].onclick = function () { 
			changePic(galleryList, this); 
			return false;
		}
	}
}

function changePic (galleryList, newPic) {
	var galleryItems      = galleryList.getElementsByTagName("li");
	var galleryPhoto      = document.getElementById("photo");
	var galleryPhotoImg   = galleryPhoto.getElementsByTagName("img")[0];
	var curCaption        = galleryPhoto.getElementsByTagName("h5")[0];
	var curPhotoCredit    = galleryPhoto.getElementsByTagName("h6")[0];
	
	var newActiveListItem = newPic.parentNode;
	var newPhotoImg       = newPic.getAttribute("href");
	var newPhotoTitle     = newPic.getAttribute("title");
		
	newPhotoTitle = newPhotoTitle.split("; Photo Courtesy of");
	
	var newCaption     = newPhotoTitle[0];
	var newPhotoCredit;
	
	if (newPhotoTitle[1] == null)
		newPhotoCredit = "";
	else
		newPhotoCredit = "Photo Courtesy of" + newPhotoTitle[1];
	
	/* Deselect all list items */
	for (var i = 0; i < galleryItems.length; i++) {
		galleryItems[i].setAttribute ("class","");
		galleryItems[i].className = "";
	}
	
	/* Select list item that newPic is in */
	newActiveListItem.setAttribute("class","curr");
	newActiveListItem.className = "curr";
	
	/* Change image */
	galleryPhotoImg.setAttribute("src",newPhotoImg);
	galleryPhotoImg.setAttribute("alt",newCaption);
	galleryPhotoImg.setAttribute("title",newCaption);
	
	/* Change caption & credit */
	curCaption.firstChild.nodeValue     = newCaption;
	curPhotoCredit.firstChild.nodeValue = newPhotoCredit;
}