// This routine traps any keystrokes, and tests for whether the navigation keys have been used.
// The left, right arrows go back, forwards.
function checkKeycode(e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	// alert("keycode: " + keycode);
	current_id = parseFloat(url.substring(a+5,b))
	if (keycode == "37") 
	{
		window.location = "index" + current_id + "-" + prev_page + ".html?" + res + timeout
	}
	else if ((keycode == "39") || (keycode == "32")) 
	{
		window.location = "index" + current_id + "-" + next_page + ".html?" + res + timeout
	}
	else if (keycode == "80") 
	{
		window.location = "fullsize" + current_id + "-" + firstfullpage + ".html?" + res + timeout
	}
}
document.onkeydown = checkKeycode


// cross-browser code for getting the ID of the DIV containing the image, this is to ensure
// that the dhtml is as cross-browser as possible, even if the styles aren't.  This is used
// in the init function for locating the image.
function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}


// This convoluted routine is merely to get the start/stop slideshow text to blink
// because IE does not support the blink property
blinke = ""
function blinking()
{
	if (blinke == "")
	{
		blinke = "colour"
		for (var li_item=0;li_item<document.getElementsByTagName("SPAN").length;li_item++)
		{
			if (document.getElementsByTagName("SPAN")[li_item].id=="start1")
			{
				document.getElementsByTagName("SPAN")[li_item].className="full_start"
			}
			else if (document.getElementsByTagName("SPAN")[li_item].id=="start2")
			{
				document.getElementsByTagName("SPAN")[li_item].className="full_start"
			}
//			else if (document.getElementsByTagName("SPAN")[li_item].id=="pause1")
//			{
//				document.getElementsByTagName("SPAN")[li_item].className="full_stop"
//			}
//			else if (document.getElementsByTagName("SPAN")[li_item].id=="pause2")
//			{
//				document.getElementsByTagName("SPAN")[li_item].className="full_stop"
//			}
		}
	}
	else	
	{
		blinke = ""
		for (var li_item=0;li_item<document.getElementsByTagName("SPAN").length;li_item++)
		{
			if (document.getElementsByTagName("SPAN")[li_item].id=="start1")
			{
				document.getElementsByTagName("SPAN")[li_item].className="full_whyte"
			}
			else if (document.getElementsByTagName("SPAN")[li_item].id=="start2")
			{
				document.getElementsByTagName("SPAN")[li_item].className="full_whyte"
			}
//			else if (document.getElementsByTagName("SPAN")[li_item].id=="pause1")
//			{
//				document.getElementsByTagName("SPAN")[li_item].className="full_whyte"
//			}
//			else if (document.getElementsByTagName("SPAN")[li_item].id=="pause2")
//			{
//				document.getElementsByTagName("SPAN")[li_item].className="full_whyte"
//			}
		}
	}
}
window.setInterval("blinking()", 900)

//  This function is intended to vertically size the thin vertical margin that runs along the 
//  right hand edge of the left hand menu bar.
function resize_window() 
{
	document.getElementById('index_left_margin').style.height = totalheight - 80 - 5 + 'px'
	document.getElementById('index_banner').style.width = windowwidth - scroll_width_fudge - 20 + 'px'
	document.getElementById('index_content_holder').style.width = windowwidth - left_margin - scroll_width_fudge - 10 + 'px'
	document.getElementById('index_content').style.width = windowwidth - scroll_width_fudge - left_margin - 10 + 'px'

// Size the thumbnail boxes so that as may as possible fit across the page and are evenly spaced
// Contentwidth is used later as the width for the overall container for all the thumbnails
contentwidth = windowwidth - scroll_width_fudge - left_margin - 15
// Work out how many thumbs can fit in the alloted space, but max of 5
numberofthumbs = Math.floor(contentwidth / 160)
if (numberofthumbs > 5)
{
	numberofthumbs = 5
}
// Work out how much space to give to each thumb so that they are more or less evenly spaced
thumbwidth = Math.floor((contentwidth - 2) / numberofthumbs)
 
	var thumlis=document.getElementsByTagName? document.getElementsByTagName('div') : document.all.tags('div')
	for (var i_tem = 0; i_tem < thumlis.length; i_tem++)
		if (thumlis[i_tem].className=='index_thmholder')
		{
			thumlis[i_tem].style.width=thumbwidth + 'px'
		}

	setTimeout("resize_window()", 10);
}


// These following functions will cause the image to move down on the web page if needed in  
// order to keep it visible and not have it scroll off the page - i.e. if the category tree
// is expanded downwards to such an extent that in viewing the lower part of the category
// tree the image would otherwise disappear off the top of the screen.  The routine is called
// at the end of the init routine below, which is called at by <body onLoad="init()">
// Floating Menu script-  Roy Whittle (http://www.javascript-fx.com/)
// Script featured on/available at http://www.dynamicdrive.com/

// It's a disappointment to me that I had to replicate this function three times to move
// three elements on the page, but I failed in my attempts to turn this into a subroutine.

function Float_Image()
{
	startX = left_margin;
	startY = 90;
	function ml(id)
	{
		var el=document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
		if(document.layers)el.style=el;
		el.sP=function(x,y){this.style.left=x + 'px';this.style.top=y + 'px';};
		el.x = startX;
		el.y = startY;
		return el;
	}
	window.stayTopLeft=function()
	{
		divheight = document.getElementById('index_content_holder').offsetHeight
		divwidth = document.getElementById('index_content_holder').offsetWidth
		
		startX = left_margin // need to continuously remeasure where we want to position the image
		startY = 90 // need to continuously remeasure where we want to position the image
		if (startY < top_margin) startY = top_margin; // don't let the image go above the top margin
		toohigh = divheight + top_margin + 24 - windowheight // how far does the image hang below the document window 
		toowide = divwidth + left_margin + 24 - windowwidth // how far does the image hang to the right of the document window 
		scrollamount = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop; // how far scrolling has already occurred

		if (totalheight == windowheight)
		{
			scrollpercent = 0; // if there is no scrolling possible, reflect this in the percentage parameter
		}
		else
		{
			scrollpercent = scrollamount / (totalheight - windowheight) // the percent of the maximum scroll that has already occurred
		}
		var scrolltarget = (totalheight - windowheight)- toohigh  // this is the maximum we want the the image to move

		if ((toohigh > 0) && (toohigh < scrolltarget)) // If the image doesn't fit on the screen, only move it if the category tree extends below the image, otherwise you'd never get to see the bottom of the image and the page keeps growing
		{
			ftlObj.y += ( ( (1-scrollpercent)*startY + scrollpercent*scrolltarget) - ftlObj.y)/jumps;
		}
		else if (toohigh < 0) // normal movement of image if it fits on the screen
		{
			ftlObj.y += (scrollamount + startY - ftlObj.y)/jumps;
		}
		ftlObj.y = Math.floor(ftlObj.y)
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("stayTopLeft()", millisecs);
	}
	ftlObj = ml("index_content_holder");
	stayTopLeft();
}

// These next two functions are duplicates of the previous one,
// except that they can be abbreviated because a number of the variables 
// have already been defined in the previous function, and as these routines
// are repeated, the values are constantly updated.

function Float_Heading()
{
	strtX = 10;
	strtY = 0;
	function mlh(id)
	{
		var elh=document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
		if(document.layers)elh.style=elh;
		elh.sPh=function(x,y){this.style.left=x + 'px';this.style.top=y + 'px';};
		elh.x = strtX;
		elh.y = strtY;
		return elh;
	}
	window.stayTopLefth=function()
	{
		htlObj.y += (scrollamount + strtY - htlObj.y)/jumps;
		htlObj.y = Math.floor(htlObj.y)
		htlObj.sPh(htlObj.x, htlObj.y);
		setTimeout("stayTopLefth()", millisecs);
	}
	htlObj = mlh("index_banner");
	stayTopLefth();
}

function Float_Controls()
{
	sttX = 0;
	sttY = 75;
	function mln(id)
	{
		var eln=document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
		if(document.layers)eln.style=eln;
		eln.sPn=function(x,y){this.style.left=x + 'px';this.style.top=y + 'px';};
		eln.x = sttX;
		eln.y = sttY;
		return eln;
	}
	window.stayTopLeftn=function()
	{
		gtlObj.y += (scrollamount + sttY - gtlObj.y)/jumps;
		gtlObj.y = Math.floor(gtlObj.y)
		gtlObj.sPn(gtlObj.x, gtlObj.y);
		setTimeout("stayTopLeftn()", millisecs);
	}
	gtlObj = mln("index_controls");
	stayTopLeftn();
}

//Optional persistence feature from dynamicdrive.com
function get_cookie(Name) {
	//Get cookie routine by Shelley Powers 
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) 
	{
		offset = document.cookie.indexOf(search)
		// if cookie exists
		if (offset != -1) 
		{
			offset += search.length
    		// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}
function set_cookie(cookieName,cookieValue,nDays) {
	if (cookieName==="fp_scroll") cookieValue = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=0;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	if (nDays == 0)
	{
		document.cookie = cookieName+"="+escape(cookieValue)+";path=/;";
	}
	else
	{
		document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString()+";path=/;";
	}
}
scrollamountprevious = get_cookie("fp_scroll")


// This function is called from the "body tag onload action" to do a range of things 
// that need to be done last
function init()
{
	//set_dimensions()
	//resize_window()

	if (floatimage=="yes") Float_Image();
	if (floatheading=="yes") Float_Heading();
	if (floatnavigationcontrols=="yes") Float_Controls();
	if (rememberscroll=="yes") window.scroll(0,scrollamountprevious)

	//preload next image, previously defined
	if ((loadNext.length > 0) && (loadNext.indexOf('<#') == -1))
	{
	  nextimage.src = loadNexxt;
	}

}

// This function simply pops up the instructions window
function instruct()
{
	instructions=window.open('../instructions.html','instructions','width=600,height=450,location=no,menubar=no,scrollbars=yes,resizable=yes,status=no,toolbar=no')
}

