// DBCINEMA IMAGE DISPLAY FUNCTIONS
// By Jim Andrews, May 2008
// This Javascript file is linked to 
// 1.htm, 2.htm, ... lastFile.htm
// All these files are exactly the same
// except for their filename. For my convenience.
// This file supports that convenience.
// The only thing I need to change in it from
// image series to image series is lastFile.

var lastFile=20
//This specifies how many files there are named x.htm
//where x is a positive integer and the files are named
//1.htm, 2.htm, ... x.htm
var pSeriesTitle="LET'S RESTART!!!, a show of computer game art in Leipzig, 2009"
//This specifies the title of the series.

//************************************************************
// FILENAME GETTERS
//************************************************************

function fname() {
	//Returns the filename (of this file) such as 10.htm
	//Assumes the filename appears after a final "/"
	//in window.location.pathname
	var tr = window.location.pathname 
	var len = tr.length 
	var i=len
	var theFilename=""
	var slashNotFound=1
	while (slashNotFound && i>-1) {
		c=tr.substring(i,i+1)
		if (c=="/") {
			slashNotFound=0
			theFilename=tr.substring(i+1)
		}
		else {
			i=i-1
		}
	}
	return theFilename
}

function fnameNoExtension() {
	//Returns the filename with no extension or final period.
	//For instance, if the filename is 12.htm, this returns 12
	var fn=fname()
	var len=fn.length
	var i=len
	var theName=""
	var periodNotFound=1
	while (periodNotFound && i>-1) {
		c=fn.substring(i,i+1)
		if (c==".") {
			periodNotFound=0
			theName=fn.substring(0,i)
		}
		else {
			i=i-1
		}
	}
	return theName
}

//************************************************************
// BACK AND FORWARD
//************************************************************

function backOne() {
	//If the form of the filename of this file is x.htm where x is a 
	//positive integer, then when this function is called, it opens
	//(x-1).htm. For instance, if this file's name is 4.htm, then 
	//3.htm is opened. If this file's name is 1.htm, then index.htm 
	//is opened.
	var fn=fnameNoExtension()
	if (fn != "1") {
		var n=parseInt(fn) - 1
		//document.write(n)
		var targetFilename=n.toString() + ".htm"
		//document.write(targetFilename)	
	}
	else {
		var targetFilename="index.htm"
	}
	window.open(targetFilename, '_self')
}


function forwardOne() {
	//If the form of the filename of this file is x.htm where x is a 
	//positive integer, then when this function is called, it opens
	//(x+1).htm. For instance, if this file's name is 4.htm, then 
	//5.htm is opened. If this file's name/number is lastFile (or 
	//greater) then index.htm is opened.
	var fn=fnameNoExtension()
	var n=parseInt(fn) + 1
	if (n > lastFile) {
		var targetFilename="index.htm"
	}
	else {
		var targetFilename=n.toString() + ".htm"
	}
	window.open(targetFilename, '_self')
}

//************************************************************
// ONLOAD HANDLERS
//************************************************************

function setBackgroundImage() {
	//This sets the document's backgroundImage. If the filename
	//of this file is x.htm where x is a positive integer, then
	//the backgroundImage is set to x.jpg
	var fn=fnameNoExtension()
	var imageName = fn + ".jpg"
	var bagLady="url(" + imageName + ")"
	document.body.style.backgroundImage=bagLady
}

function setNum() {
	//This sets the display of the number of this file
	//to the user in the element named "num".
	var numElement=document.getElementById("num")
	var n=fnameNoExtension()
	numElement.innerHTML=n
}

//************************************************************
// MOUSE EVENT HANDLERS
//************************************************************

function mOver(t) {
	//This gets called onmouseover of the gray controls.
	t.style.backgroundColor="#999999"
	t.style.cursor="pointer"
}

function mOut(t) {
	//This gets called onmouseout of the gray controls.
	t.style.backgroundColor="#666666"
	t.style.cursor="auto"
}

function bodyClick() {
	//When the user clicks the document anywhere,
	//this function runs and moves the image forward
	//if the user is not in the gray controls.
	x=event.clientX
	y=event.clientY
	w = document.getElementById("wrap").offsetWidth + 12
	h = document.getElementById("wrap").offsetHeight + 5
	if ((x > w) || (y > h)) {
		forwardOne()
	}
}

document.onclick=bodyClick

function toggleHelp () {
	//This toggles the little Help window's visibility.
	//It gets called when the user clicks the ? button.
	var help=document.getElementById("help")
	var v=help.style.visibility
	if (v=="visible") {
		help.style.visibility="hidden"
	}
	else {
		help.style.visibility="visible"
	}
}

//************************************************************
// KEYBOARD NAVIGATION
//************************************************************

function keynav(){
	//This gets called whenever the user presses a key.
	//The user can use the arrow keys and the space key.
  var unicode=event.keyCode? event.keyCode : event.charCode
  switch(unicode)
	{
	case 32:
	  //space key
	  window.open('index.htm', '_self')
	  break   
	case 37:
	  //back arrow key
	  backOne()
	  break
	case 38:
		//up arrow key
		window.open('index.htm', '_self')
		break
	case 39:
		//forward arrow key
		forwardOne()
		break
	case 40:
		//down arrow key
		window.open('index.htm', '_self')
		break
	default:
	  //do nothing
	}
}

document.onkeypress=keynav
document.onkeyup=keynav

