/*
 * Photo Gallery Functionality
 * 
 * This script set powers the Photo Galleries.
 * It includes switching out thumbnails for larger photos and allows
 * people to view the photos without a page refresh.
 *
 */

function showPic(whichpic) {
	if (!document.getElementById("img-lg")) return true;
	//var orientation = whichpic.getAttribute("class");
	var orientation = whichpic.className;
	
	var theImg = document.getElementById("img-lg");
	theImg.className = '';
	theImg.className = orientation;
	
	if (!document.getElementById("placeholder")) return true;
	var fullsource = whichpic.getAttribute("href");
	var splitsource = fullsource.split("imgSrc=");
	var source = splitsource[1];
	
	var placeholder = document.getElementById("placeholder");
	//placeholder.setAttribute("src",source);
	placeholder.src = source;
	if(orientation == 'portrait') {
		placeholder.height = '220';
		placeholder.width = '146';
	}
	if(orientation == 'landscape') {
		placeholder.height = '185';
		placeholder.width = '278';
	}
	
	if (!document.getElementById("caption")) return false;
	if (whichpic.getAttribute("title")) {
		var text = whichpic.getAttribute("title");
	} else {
		var text = "";
	}
	var description = document.getElementById("caption");
	if (description.firstChild.nodeType == 3) {
		description.firstChild.nodeValue = text;
	}
	return false;
}

function preparePlaceholder() {
	if (!document.createElement) return false;
	if (!document.createTextNode) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("photos")) return false;
	
	var placeholder = document.createElement("img");
	placeholder.setAttribute("id","placeholder");
	placeholder.setAttribute("src","/img/galleries/placeholder.gif");
	placeholder.setAttribute("alt","my image gallery");
	
	var description = document.createElement("p");
	description.setAttribute("id","caption");
	
	var desctext = document.createTextNode("Select a thumbnail to view the larger photo.");
	description.appendChild(desctext);
	
	var imgLarge = document.createElement("div");
	imgLarge.setAttribute("id","img-lg");
	imgLarge.setAttribute("class", "landscape");
	
	imgLarge.appendChild(placeholder);
	imgLarge.appendChild(description);
	
	var photoarea = document.getElementById("photos");
	photoarea.appendChild(imgLarge);
}

function prepareGallery() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("thumbnails")) return false;
	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);
		}
	}
}

addLoadEvent(preparePlaceholder);
addLoadEvent(prepareGallery);