// onload events
window.onload = function() {
	prepareGallery();
	prepareLinks();
}


function popUp(winURL) {
	window.open(winURL,"image_popup");
}

function prepareLinks() {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		if (links[i].getAttribute("class") == "popup") {
			links[i].onclick = function() {
				popUp(this.getAttribute("href"));
				return false;
			}
		}
	}
}



//prepareGallery checks for DOM, gets all a tags within "gallery id," and overrides the click action with showPic.
function prepareGallery() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("gallery")) return false;
	var gallery = document.getElementById("gallery");
	var links = gallery.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
			links[i].onclick = function() {
				return showPic(this);
		}
	}
}


//showPic swaps out images and cutlines based on the href and title of the link it is passed
function showPic(whichpic) {
	if (!document.getElementById("placeholder")) return true;

	var source;
	var children = whichpic.childNodes;
	for (var i=0; i<children.length; i++) {
		if (children[i].nodeName == "IMG") {
			source = children[i].getAttribute("src");
		}
	}
	
	
	var piclink = whichpic.getAttribute("href");
	var placeholder = document.getElementById("placeholder");
	if (placeholder.nodeName != "IMG") return true;
	placeholder.src = source;
	if (!document.getElementById("cutline")) return false;
	var text = whichpic.getAttribute("title") ?
	whichpic.getAttribute("title") : "";
	var cutline = document.getElementById("cutline");
	cutline.innerHTML=text;
/*	if (cutline.firstChild==undefined)
	{
		var txtnode=document.createTextNode("");
		cutline.appendChild(txtnode);
	}
	if (cutline.firstChild.nodeType == 3) {
		cutline.firstChild.nodeValue = text;
	} */
	var producer = whichpic.getAttribute("credit") ?
	whichpic.getAttribute("credit") : "";
	var mediaproducer = document.getElementById("mediaproducer");
	if (mediaproducer.firstChild==undefined)
	{
		var txtnode=document.createTextNode("");
		mediaproducer.appendChild(txtnode);
	}
	if (mediaproducer.firstChild.nodeType == 3) {
		mediaproducer.firstChild.nodeValue = producer;
	}
	var zoom_image = document.getElementById("zoom_image");
	zoom_image.href = piclink;
	zoom_image.title = text;
	return false;
}

/*
function countBodyChildren() {
	var body_element = document.getElementsByTagName("body")[0];
	alert (cutline.firstChild.nodeValue);
}

window.onload = countBodyChildren;
*/	
