var oldAlign;
var savedKeys = "";
var timer;

function getParam(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regExS = "[\\?&]"+name+"=([^&#]*)";
	var regEx = new RegExp(regExS);
	var results = regEx.exec(document.location.href);
	if (results == null) {
		return "";
	}
	else {
		return results[1];
	}
}



function checkSize() {
	var getHeight = parseInt(getParam("height"));
	var getWidth = parseInt(getParam("width"));
	var cliHeight = 0;
	var cliWidth = 0;
	var tolerance = 16;
	if (cliHeight < document.documentElement.clientHeight) cliHeight = document.documentElement.clientHeight;
	if (cliWidth < document.documentElement.clientWidth) cliWidth = document.documentElement.clientWidth;
	/*if (cliHeight > document.body.clientHeight) cliHeight = document.body.clientHeight;
	if (cliWidth > document.body.clientWidth) cliWidth = document.body.clientWidth;*/
	if (cliHeight > window.innerHeight) cliHeight = window.innerHeight;
	if (cliWidth > window.innerWidth) cliWidth = window.innerWidth;
	//Fails On iPhone :(
	//if (cliHeight > screen.height) cliHeight = screen.height - 128;
	/*if (document.getElementById("navigation") && document.getElementById("navigation").offsetHeight > 25) {
		cliHeight -= (document.getElementById("navigation").offsetHeight - 25);
	}*/
	if (isNaN(getHeight) || (getHeight < cliHeight - tolerance) || (getHeight > cliHeight + tolerance) || isNaN(getWidth) || (getWidth < cliWidth - tolerance) || (getWidth > cliWidth + tolerance)) {
		var newLocation = String(document.location.href);
		newLocation = newLocation.replace(/height=[0-9]*/, "height="+cliHeight);
		newLocation = newLocation.replace(/width=[0-9]*/, "width="+cliWidth);
		document.location.href = newLocation;
	}
}



function handleKeyPress(pressedKey) {
	var pressedKeyCode = pressedKey.keyCode || pressedKey.charCode;
	// Special handling for NumPad
	if (pressedKeyCode > 95 && pressedKeyCode < 106) pressedKeyCode -= 48;
	// Ignore control characters
	if (pressedKeyCode > 32) savedKeys += String.fromCharCode(pressedKeyCode);
	// For debugging ;-)
	//alert(pressedKeyCode + " => " + String.fromCharCode(pressedKeyCode));
	//alert(savedKeys);

	// ESC deletes savedKeys
	if (pressedKeyCode == 27 && (savedKeys.length > 0)) {
		savedKeys = "";
	}
	
	// We are on image or thumbnail page
	if (document.getElementById("mainImage") || document.getElementById("thumbnails")) {
		switch(pressedKeyCode){
			case 35:	//End
			case 76:	//l
				if (document.getElementById("last")) document.location.href = document.getElementById("last");
			break;
			case 36:	//Pos1
			case 70:	//f
				if (document.getElementById("first")) document.location.href = document.getElementById("first");
			break;
			case 37:	//Left Arrow
			//case 80:	//p
				if (document.getElementById("previous")) document.location.href = document.getElementById("previous");
			break;
			case 38:	//Up Arrow
			case 66:	//b
				if (document.getElementById("back")) document.location.href = document.getElementById("back");
			break;
			case 39:	//Right Arrow
			case 78:	//n
				if (document.getElementById("next")) document.location.href = document.getElementById("next");
			break;
		}
	}
	
	// We are on image page
	if (document.getElementById("mainImage")) {
		numPics = parseInt(document.getElementById("numPics").innerHTML);
		switch(pressedKeyCode){
			case 13:	//Enter
				var newPic = parseInt(savedKeys);
				if ((newPic >= 1) && (newPic <= numPics)) {
					document.location.href = document.location.href.replace(/display=[0-9]*/, "display="+newPic);
				}
				savedKeys = "";
			break;
			case 27:	//ESC
				if (document.getElementById("licensePopup")) hideDivNow("licensePopup");
			break;
			case 86:	//v
				//if (document.getElementById("licenseLink")) document.location.href = document.getElementById("licenseLink");
				if (document.getElementById("licensePicture")) showDivNow('licensePicture',-1,-1,'');
			break;
		}
	}
	
	// We are on thumbnail page
	if (document.getElementById("thumbnails")) {
		numPics = parseInt(document.getElementById("numPics").innerHTML);
		switch(pressedKeyCode){
			case 13:	//Enter
				if (savedKeys.substr(0,1) == "P") {
					var num = parseInt(savedKeys.substr(1));
					if ((num >= 1) && (num <= numPics)) {
						document.location.href = document.location.href + "&display=" + num;
					}
				}
				numPage = parseInt(savedKeys);
				if (savedKeys == numPage) {
					var page = "page" + numPage;
					if (document.getElementById(page)) document.location.href = document.getElementById(page);
				}
				savedKeys = "";
			break;
			case 67:	//c
				if (document.getElementById("back")) document.location.href = document.getElementById("back");
			break;
			case 40:	//Arrow Down
			case 68:	//d
				if (document.getElementById("download")) document.location.href = document.getElementById("download");
			break;
		}
	}
	
	// We are on gallery page
	if (document.getElementsByName("gallery")) {
		switch(pressedKeyCode){
			case 13:	//Enter
				var numGallery = parseInt(savedKeys);
				if ((numGallery >= 1) && (numGallery <= document.getElementsByName("gallery").length)) {
					document.location.href = document.getElementsByName("gallery")[numGallery - 1];
				}
				savedKeys = "";
			break;
		}
	}
}



function hideDiv(divID,delay){
	//clearTimeout(timer);
	timer = setTimeout("hideDivNow('"+divID+"')",delay);
}



function hideDivNow(divID){
	//clearTimeout(timer);
	document.getElementById(divID).style.visibility = "hidden";
}



function main() {
	checkSize();
	if (document.addEventListener) document.addEventListener("keydown", handleKeyPress, false);
	if (document.attachEvent) document.attachEvent("onkeydown", handleKeyPress);
}



function showDiv(divID,curX,curY,align,delay){
	clearTimeout(timer);
	setTimeout("showDivNow('"+divID+"',"+curX+","+curY+",'"+align+"')",delay);
}



function showDivNow(divID,curX,curY,align){
	clearTimeout(timer);
	var oldHeight = 1;
	myHeight = getParam("height");
	myWidth = getParam("width");
	oldX = parseInt(document.getElementById(divID).style.left) || (myWidth - parseInt(document.getElementById(divID).style.right) + 16);
	oldY = myHeight - parseInt(document.getElementById(divID).style.bottom);
	//alert(oldX+","+oldY+" => "+curX+","+curY);
	if (align == "") align = oldAlign;
	if (curX != -1 && curY != -1 && ((oldAlign != align) || (curX < (oldX - 16)) || (curX > (oldX + 16)) || (curY < (oldY - 16)) || (curY > (oldY + 16)))) {
		document.getElementById(divID).style.bottom = (myHeight - curY) + "px";
		if (align == "left") {
			document.getElementById(divID).style.right = "";
			document.getElementById(divID).style.left = curX + "px";
		}
		if (align == "right") {
			document.getElementById(divID).style.left = "";
			document.getElementById(divID).style.right = (myWidth - curX + 16) + "px";
		}
	}
	document.getElementById(divID).style.visibility = "visible";
	oldAlign = align;
}

