////////////////////////////////////////////////////////////
//
// 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: $3.00
//
// Only the purchaser of this script may use and modify it.
//
////////////////////////////////////////////////////////////

// get elements by class
function getElementsByClass(needle, tag) {
	var class_elements = new Array();

	if (tag == null) {
		tag = "*";
	}

	var haystack = document.getElementsByTagName(tag);

	for (c1 = 0, c2 = 0; c1 < haystack.length; c1++) {
		if (haystack[c1].className == needle) {
			class_elements[c2] = haystack[c1];
			c2++;
		}
	}

	return class_elements;
}

// create global variable for navigating XML data
var 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 navigation links
window.onload = function() {
	// 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;
	}

	// get jump links
	var jump_links = getElementsByClass("jump", "a");

	// jump links go to specific images
	for (n = 0; n < jump_links.length; n++) {
		jump_links[n].onclick = function() {
			i = this.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("alt")[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();
		}
	}
}