// JavaScript Document
var gImages;
var currentIndex;

function showImage(index){
	currentIndex = index;
	document.getElementById("back").style.display = currentIndex == 0 ? "none" : "inline";
	document.getElementById("forward").style.display = currentIndex == gImages.length - 1 ? "none" : "inline";
	document.getElementById("gallery").innerHTML = "<img src='images/gallery/" + gImages[currentIndex].getAttribute('src') + "' alt='" + optionalAppend(gImages[currentIndex], " ") + "'/>";
	document.getElementById("imageInfo").innerHTML = optionalAppend(gImages[currentIndex], "<br/>");
}

function loadGalleryContent(){
	sendHTTPRequest('GET', 'xml/gallery.xml', null, displayGalleryContent);
}

function displayGalleryContent(request){
	gImages = request.responseXML.documentElement.getElementsByTagName("image");
	var code = "";
	for(var i = 0; i < gImages.length; i++) {
		code = code + "<div><a href='javascript:showImage(" + i + ")'><img src='images/gallery/thumbs/" + gImages[i].getAttribute('src') + "' alt='" + optionalAppend(gImages[i], " ") + "'/></div>";
	}
	document.getElementById("thumbs").innerHTML = code;
	showImage(0);
}

function optionalAppend(node, endString){
	var string = "";
	var attributes = ["artist", "title", "place", "date"];
	for(var j = 0; j < attributes.length; j++) {
		if(node.getAttribute(attributes[j])){
			if(j == attributes.length - 1){
				endString = "";
			}
			string = string + node.getAttribute(attributes[j]) + endString;
		}
	}
	return(string);
}

function nextImage(booly){
	if(booly){
		showImage(currentIndex + 1);
	} else {
		showImage(currentIndex - 1);
	}
}