////////////////////////////////////////////////////////////
//
// xmlSlideshow v1.0 - a JavaScript/XML slideshow
//
////////////////////////////////////////////////////////////
//
// This script allows you to display images in a slideshow.
//
// See readme.txt for more information.
//
// Author: Jon Thomas <http://www.fromthedesk.com/code>
// Last Modified: 02/09/2008
// Price: $2.00
//
// Only the purchaser of this script may use and modify it.
//
////////////////////////////////////////////////////////////

// create global variable for navigating XML data
i = 0;

// create variable for XML data
var xmlDoc;

// get XML document in Internet Explorer
if (window.ActiveXObject) {
	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}

// get XML document in other browsers (e.g., Firefox)
else if (document.implementation && document.implementation.createDocument) {
	xmlDoc = document.implementation.createDocument("", "", null);
}

// set XML document setting
xmlDoc.async = false;

// load XML file
xmlDoc.load("images.xml");

// get "image" tags in XML file
var slideshow = xmlDoc.getElementsByTagName("image");

// prepare slideshow links onload
addLoadEvent(prepareSlideshowLinks);

// prepare slideshow links
function prepareSlideshowLinks() {
	// next link increments counter "i"
	document.getElementById("next").onclick = function() {
		i++;
		getImage();
		return false;
	}

	// previous link decrements counter "i"
	document.getElementById("prev").onclick = function() {
		i--;
		getImage();
		return false;
	}

	// this link skips to specific photo indicated by link title
	document.getElementById("computer_lab").onclick = function() {
		i = document.getElementById("computer_lab").title;
		getImage();
		return false;
	}

	// this link skips to specific photo indicated by link title
	document.getElementById("science_lab").onclick = function() {
		i = document.getElementById("science_lab").title;
		getImage();
		return false;
	}

	// this link skips to specific photo indicated by link title
	document.getElementById("gym").onclick = function() {
		i = document.getElementById("gym").title;
		getImage();
		return false;
	}
}

// get image data from
function getImage() {
	// reset counter
	if (i == slideshow.length) {
		i = 0;
	}

	// reset counter
	if (i < 0) {
		i = slideshow.length - 1;
	}

	// update HTML with this image's data
	document.getElementById("placeholder").setAttribute("src", slideshow[i].getElementsByTagName("filename")[0].firstChild.nodeValue);
	document.getElementById("title").firstChild.nodeValue = slideshow[i].getElementsByTagName("caption")[0].firstChild.nodeValue;
}

// navigate slideshow with left and right arrow keys
document.onkeyup = function(e) {
	// capture "onkeyup" event
	if (!e) {
		e = window.event;
	}

	// navigate with arrow keys in other browsers (e.g., Firefox)
	if (e.which) {
		// left arrow key gets previous image
		if (e.which == 37) {
			i--;
			getImage();
		}

		// right arrow gets next image
		if (e.which == 39) {
			i++;
			getImage();
		}
	}

	// navigate with arrow keys in Internet Explorer
	else if (e.keyCode) {
		if (e.keyCode == 37) {
			i--;
			getImage();
		}

		if (e.keyCode == 39) {
			i++;
			getImage();
		}
	}
}