var usesListener = false, usesAttach = false;
var backUrl = "default.htm", forwardUrl = "default.htm";

if (document.addEventListener != null)
{
	window.addEventListener("load", loadSlideshowControls, true);
	usesListener = true;
}
else if (document.attachEvent != null)
{
	window.attachEvent("onload", loadSlideshowControls);
	usesAttach = true;
}
else
	window.onload = loadSlideshowControls;

function loadSlideshowControls()
{
	var results = location.href.match(/slide(\d+)\.htm/i);
	if (results != null)
	{
		var num = parseInt(results[1]);
		if (num > 1) backUrl = "slide" + (num - 1) + ".htm";
		if (num < 25) forwardUrl = "slide" + (num + 1) + ".htm";
	}
	else
	{
		alert('Use your arrow keys or the links at the top of the page to navigate through the slides.')
		forwardUrl = "slide1.htm";
	}
	
	if (usesListener)
	{
		document.addEventListener("keyup", navigateSlides, true);
		window.addEventListener("unload", unloadSlideshowControls, true);
	}
	else if (usesAttach)
	{
		document.attachEvent("onkeyup", navigateSlides);
		window.attachEvent("onunload", unloadSlideshowControls);
	}
	else
	{
		document.onkeyup = navigateSlides;
		window.onunload = unloadSlideshowControls;
	}
}

function navigateSlides(evt)
{
	evt = evt || window.event;
	var keyCode = evt.keyCode || evt.which;
	
	if (keyCode == 37)
		location.href = backUrl;
	else if (keyCode == 39)
		location.href = forwardUrl;
}

function unloadSlideshowControls()
{
	if (usesListener)
	{
		window.removeEventListener("load", loadSlideshowControls);
		document.removeEventListener("keyup", navigateSlides);
		window.removeEventListener("unload", unloadSlideshowControls);
	}
	else if (usesAttach)
	{
		window.detachEvent("onload", loadSlideshowControls);
		document.detachEvent("onkeyup", navigateSlides);
		window.detachEvent("onunload", unloadSlideshowControls);
	}
	else
	{
		window.onload = null;
		document.onkeyup = null;
		window.onunload = null;
	}
}