var photoPageMode = null;
var photoCurrent = -1;

var photoLoading = new Image();
photoLoading.src="../img/loading_big.gif";

var oldHash;
var hashTimer;
function pollWindowHash() {
	if (oldHash != window.location.hash) {
		oldHash = window.location.hash;
		i = findPhotoInHash();
		if (i != -1)
			photoGotoImage(i);
		else if ((oldHash.substring(0,6) == "#Album") || (oldHash == ''))
			photoGotoAlbum();
	}
}

function findPhotoInHash() {
	if (window.location.hash.substring(0,7) == '#Photo=') {
		i = window.location.hash.substring(7,window.location.hash.length);
		for (j in photos) {
			if (photos[j][0] == i) {
				return j;
			}
		}
	}
	return -1;
}

function photoInit() {
	oldHash = window.location.hash + "not"; // force pollWindowHash to run
	pollWindowHash();
}

function photoChangePageState(newState, newParam) {
	clearInterval(hashTimer);
	hashTimer = setInterval(pollWindowHash, 750);
	
	if (newState == "Photo") {
		document.getElementById("photoNav").style.display = "inline";
		window.location.hash = "#" + newState + "=" + newParam;
	}
	else {
		document.getElementById("photoNav").style.display = "none";
		window.location.hash = '';
	}
	oldHash = window.location.hash;

	if (photoPageMode == newState)
		return;
	else if (photoPageMode != null) // disable the old state
		document.getElementById("photoContent"+photoPageMode).style.display = "none";
	document.getElementById("photoContent"+newState).style.display = "inline";
	photoPageMode = newState;
}

photoThumbsLoaded = false;
function photoThumbLoaded(i) {
	i = +i + 1; // cast to int and increment
	if (i < photos.length) {
		img = document.getElementById("photoThumb" + i);
		img.src="albums/" + photoAlbumPath + "/thumb/" + photos[i][1];
		addListener(img, "load", function() {photoThumbLoaded(i);});
	}
	else
		photoThumbsLoaded = true;
}
function photoGotoAlbum() {
	photoChangePageState("Album", ".");
	document.getElementById("photoImgCount").innerHTML = photos.length;
	if (!photoThumbsLoaded) 
		photoThumbLoaded(-1); // kick off loading images
}

function setImgComment(i) {
	document.getElementById("photoComments").innerHTML = photos[i][6];
}

function commentReqCallback(txt, i) {
	photos[i][6] = txt;
	if (photoCurrent == i) setImgComment(i);
}

/* Change the page to show the image for index i */
function photoGotoImage(i) {
	photoCurrent = i;

	function photoLoadImage(i) {
		if (i >= 0 && i < photos.length) {
			if (photos[i][5].src == '')
				photos[i][5].src="albums/" + photoAlbumPath + "/small/" + photos[i][1];
			if (photos[i][6] == "0") {
				photos[i][6] == "-1";
				makeRequest("comment_get.php", "id="+photos[i][0], commentReqCallback, i);
			}
		}
	}
	
	i = i*1; // make sure i is a number & not a string
	img = document.getElementById("photoImage");
	img.src = photoLoading.src;

	photoChangePageState("Photo", photos[i][0]);
	document.getElementById("photoImgCount").innerHTML =  (i+1) + " / " + photos.length;

	t = document.getElementById("photoNav");
	if (i > 0) t.innerHTML = "<A href=\"?album_id=" + photoAlbumId + "#Photo=" + photos[(i-1)][0] + "\" onclick=\"return photoGotoImage(" + (i-1) + ")\"><-- Prev</a>"
	else t.innerHTML = "";
	
	if (i != (photos.length-1)) {
		if (i > 0) t.innerHTML += "&nbsp;&nbsp;&nbsp;";
		t.innerHTML += "<A href=\"?album_id=" + photoAlbumId + "#Photo=" + photos[(i+1)][0] + "\" onclick=\"return photoGotoImage(" + (i+1) + ")\">Next --></a>";
	}

	if (photos[i][6] == "0" || photos[i][6] == "-1")
		document.getElementById("photoComments").innerHTML = "Loading Caption & Comments &nbsp;&nbsp;&nbsp;<img src=\"../img/loading_big.gif\" height=25px>";
	else
		setImgComment(i);
		
	document.getElementById("photoCaptionText").innerHTML = 
		"Title: " + photos[i][3] + "<br>" +
		"Date: " + photos[i][2] + "<br>" +
		"Description: " + photos[i][4] + "<br>" +
		"<div class=commentReply id=commentReply" + photos[i][0] + "_0>[<a href=\"\" onclick=\"return addComment(" + photos[i][0] + ", 0)\">comment on this photo</a>]</div>";

	img = document.getElementById("photoImage");
	photoLoadImage(i);
	img.src = photos[i][5].src;
	
	// Preload the next & previous images
	photoLoadImage(i+1);
	photoLoadImage(i-1);
	photoLoadImage(i+2);
	photoLoadImage(i-2);

	return false;
}

function loadedCommentForm(txt, callbackParam) {
	document.getElementById("commentReply" + callbackParam).innerHTML = txt;
	myForm = document.getElementById("commentForm" + callbackParam);
	myForm.poster.focus();
}

function addComment(photoId, commentId) {
	document.getElementById("commentReply" + photoId + "_" + commentId).innerHTML = "<hr><img src=\"../img/loading_big.gif\">";

	makeRequest ("comment_form.php", 
		"aid=" + photoAlbumId + "&pid=" + photoId + "&cid=" + commentId,
		loadedCommentForm,
		photoId + "_" + commentId);

	return false;
}
function commentDone(txt, callbackObj) {
	var e;
	if (txt.substr(0, 5) == "Error")  {
		e = document.getElementById("commentResponse" + callbackObj.photoId + "_" + callbackObj.commentId);
		myForm = document.getElementById(callbackObj.formID);
		myForm.captcha.focus();
	}
	else {
		e = document.getElementById("commentReply" + callbackObj.photoId + "_" + callbackObj.commentId);
		e.className += " commentResponse";
	}
	e.innerHTML = txt;
}
function submitComment(formId) {
	var myForm = document.getElementById(formId);	
	var callbackObj = new Object();
	callbackObj.photoId = myForm.photo_id.value;
	callbackObj.commentId = myForm.comment_id.value;
	callbackObj.formID = formId;
	
	var params = "";
	for (var i=0; i < myForm.length; i++) {
		if (myForm.elements[i].className != "button") {
			if (params != "") params = params + "&";
			params = params + myForm.elements[i].name + "=" + myForm.elements[i].value;
		}
	}

	e = document.getElementById("commentResponse" + callbackObj.photoId + "_" + callbackObj.commentId);
	e.innerHTML = "<img src=\"../img/loading_big.gif\">";
	
	makePost(myForm.action, params, commentDone, callbackObj);
	
	return false;
}

/*
	if (photoCurrentImage != null) {
		var myPadding = photos[photoCurrentImage][2].height - photoLoading.height;
		img.style.paddingTop = (myPadding / 2) + "px";
		img.style.paddingBottom = (myPadding - (myPadding / 2)) + "px";
		myPadding = photos[photoCurrentImage][2].width - photoLoading.width;
		img.style.paddingLeft = (myPadding / 2) + "px";
		img.style.paddingRight = (myPadding - (myPadding / 2)) + "px";
	}
*/
