// JavaScript Document

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function prepareGallery() {
	//check for support of methods
	if (!document.createElement) return false;
	if (!document.getElementById) return false;
	if (!document.createTextNode) return false;
	// check for thumbs div
	if (!document.getElementById("thumbs")) return false;
	// get links from thumbs div, and apply showPic to each
	var gallery = document.getElementById("thumbs");
	var links = gallery.getElementsByTagName("a");
	for (var i=0; i < links.length; i++) {
		links[i].onclick = function() {
			return showPic(this);
		}
		links[i].onkeypress = links[i].onclick;
	}
	
}

function showPic(whichpic) {
	// follow link if there's no bigpic img
	if (!document.getElementById("bigpic")) return true;
	// apply new src to bigpic img
	var source = whichpic.getAttribute("href");
	var image = document.getElementById("bigpic");
	image.setAttribute("src",source);

	// get title from link
	if (whichpic.getAttribute("title")) {
	var linktitle = whichpic.getAttribute("title");
	} else {
		var linktitle = " ";
	}

	// get client h2 and add some default text if there isn't any
	if (document.getElementById("title")) {
		var title = document.getElementById("title");
		if (!title.firstChild) {
			var spaceholder = document.createTextNode(" ");
			title.appendChild(spaceholder);
		}
		// apply linktitle to client h2
		if (title.firstChild.nodeType == 3) {
			title.firstChild.nodeValue = linktitle;
		}
	}
	
	// get alt from thumbnail img
	if (whichpic.getElementsByTagName("img")) {
		var smallpic = whichpic.getElementsByTagName("img");
		var alttext = smallpic[0].getAttribute("alt");
	} else {
		var alttext =" ";
	}
 
	//get description p and add some default text if there isn't any
	if (document.getElementById("description")) {
		var description = document.getElementById("description");
		if (!description.firstChild) {
			var spaceholder2 = document.createTextNode(" ");
			description.appendChild(spaceholder2);
		}
		//apply alttext to description p
		if (description.firstChild.nodeType == 3) {
		description.firstChild.nodeValue = alttext;
		}
	}


	return false;
}


function test() {
var gallery = document.getElementById("thumbs");
	var links = gallery.getElementsByTagName("a");
		for (var i=0; i < links.length; i++) {
			var smallpic = links[i].getElementsByTagName("img");
			alert(smallpic[0].getAttribute("alt"));
		
			links[i].onclick = function() {
				var source = this.getAttribute("href");
				alert(source);
				return false;
			}
		}
}

addLoadEvent(prepareGallery); 

