// ADJUST THIS VALUE TO CHANGE SLIDE FADE SPEED
var fadeSpeed = 200;

var effectComplete = 0;
var photoShowing = 1;
var gotThumbs = 0;
var photo1 = new Image();
var photo2 = new Image();

function loadXMLDoc(url) {
	// setup xml request and do server request
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		//http_request.overrideMimeType('text/xml');
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	http_request.onreadystatechange = parseXML;
	http_request.open('GET', url, true);
	http_request.send(null);
}

function parseXML () {
	// parse returned xml and start loading photo
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			response = http_request.responseXML.documentElement;
			var method = response.getElementsByTagName('method')[0].firstChild.data;
			var isResponse = 1;
			eval(method + '(\'\', isResponse)');
		}
	}
}

function changePhoto(offset) {
	// check to make sure effects are done and continue loading new photo
	if (effectComplete == 1 && photoShowing == 1) {
		effectComplete = 0;
		photoSlide.toggle();
		photoShowing = 0;
		checkButtons();
		getPhoto(offset,'');
	} else {
		checkEffectTimer = window.setTimeout("changePhoto(" + offset + ")", 10);
	}
}

function getPhoto(offset, isResponse) {
	if (isResponse == 1) {
		photoid = null;
		filename = null;
		photoWidth = null;
		photoHeight = null;
		title = null;
		rating = null;
		dateTaken = null;
		photoDescription = null;
		camera = null;
		exposure = null;
		aperture = null;
		if (response.getElementsByTagName('photoid')[0].firstChild != undefined) {
			photoid = response.getElementsByTagName('photoid')[0].firstChild.data;
		}
		if (response.getElementsByTagName('filename')[0].firstChild != undefined) {
			filename = response.getElementsByTagName('filename')[0].firstChild.data;
		}
		if (response.getElementsByTagName('width')[0].firstChild != undefined) {
			photoWidth = response.getElementsByTagName('width')[0].firstChild.data;
		}
		if (response.getElementsByTagName('height')[0].firstChild != undefined) {
			photoHeight = response.getElementsByTagName('height')[0].firstChild.data;
		}
		if (response.getElementsByTagName('title')[0].firstChild != undefined) {
			title = response.getElementsByTagName('title')[0].firstChild.data;
		}
		if (response.getElementsByTagName('rating')[0].firstChild != undefined) {
			rating = response.getElementsByTagName('rating')[0].firstChild.data;
		}
		if (response.getElementsByTagName('dateTaken')[0].firstChild != undefined) {
			dateTaken = response.getElementsByTagName('dateTaken')[0].firstChild.data;
		}
		if (response.getElementsByTagName('description')[0].firstChild != undefined) {
			photoDescription = response.getElementsByTagName('description')[0].firstChild.data;
		}
		if (response.getElementsByTagName('camera')[0].firstChild != undefined) {
			camera = response.getElementsByTagName('camera')[0].firstChild.data;
		}
		if (response.getElementsByTagName('exposure')[0].firstChild != undefined) {
			exposure = response.getElementsByTagName('exposure')[0].firstChild.data;
		}
		if (response.getElementsByTagName('aperture')[0].firstChild != undefined) {
			aperture = response.getElementsByTagName('aperture')[0].firstChild.data;
		}
		// show loader
		showLoader();
		// show the photo only after it loads
		photo1.onload = showPhoto;
		photo1.src = "images/gallery/" + filename;
		getComments();
		// preload next image
		if (response.getElementsByTagName('filename')[1] != undefined) {
			nextFilename = response.getElementsByTagName('filename')[1].firstChild.data;
			photo2.src = "images/gallery/" + nextFilename;
		}
	} else {
		url = 'functions.php?albumID='+albumID+'&method=getPhoto&offset='+offset;
		loadXMLDoc(url);
	}
}

function showPhoto () {
	if (effectComplete == 1) {
		// actually display the photo
		document.getElementById("photoHolder").style.width = (parseInt(photoWidth) + 4) + "px";
		document.getElementById("photoHolder").style.height = (parseInt(photoHeight) + 4) +"px";
		document.getElementById("photo").innerHTML = '<a href="javascript:void(0);" onclick="nextPhoto();"><img class="photo" src="images/gallery/' + filename + '" border="0"/></a>';
		document.getElementById("photoTitle").innerHTML = title;
		document.getElementById("photoRatingNumber").innerHTML = rating;
		document.getElementById("photoInfoTitle").innerHTML = title;
		document.getElementById("photoInfoDateTaken").innerHTML = dateTaken;
		document.getElementById("photoInfoDescription").innerHTML = photoDescription;
		if (camera != null && camera != '') {
			document.getElementById("photoInfoCamera").innerHTML = camera;
		}
		if (exposure != null && exposure != '') {
			document.getElementById("photoInfoExposure").innerHTML = exposure;
		}
		if (aperture != null && aperture != '') {
			document.getElementById("photoInfoAperture").innerHTML = " @ " + aperture;
		}
		document.getElementById("photoLoader").style.visibility = "hidden";
		effectComplete = 0;
		photoSlide.toggle();
		photoShowing = 1;
	} else {
		checkEffectTimer = window.setTimeout("showPhoto();", 10);
	}
}

function checkButtons () {
	// check whether or not to display the next and prev buttons
	if (photoPointer == 0) {
		document.getElementById("prevButton").style.visibility = "hidden";
	} else {
		document.getElementById("prevButton").style.visibility = "visible";
	}
	if (photoPointer == numPhotos - 1) {
		document.getElementById("nextButton").style.visibility = "hidden";
	} else {
		document.getElementById("nextButton").style.visibility = "visible";
	}
}

function showLoader () {
	// show div between loading photos
	if (effectComplete == 1) {
		if (photoShowing == 0) {
			document.getElementById("photoLoader").style.visibility = "visible";
		}
	} else {
		checkEffectTimer = window.setTimeout("showLoader();", 50);
	}
}

function prevPhoto () {
	// show previous photo
	if (effectComplete == 1 && photoShowing == 1) {
		if (photoPointer != 0) {
			photoPointer--;
			changePhoto(photoPointer);
		}
	} else {
		checkEffectTimer = window.setTimeout("prevPhoto();", 10);
	}
}

function nextPhoto () {
	// show next photo
	if (effectComplete == 1 && photoShowing == 1) {
		if (photoPointer != numPhotos - 1) {
			photoPointer++;
			changePhoto(photoPointer);
		}
	} else {
		checkEffectTimer = window.setTimeout("nextPhoto();", 10);
	}
}

function gotoPhoto (offset) {
	if (effectComplete == 1 && photoShowing == 1) {
		photoPointer = offset;
		changePhoto(photoPointer);
	} else {
		checkEffectTimer = window.setTimeout("gotoPhoto(" + offset + ");", 10);
	}
}

function getComments(dummy, isResponse) {
	if (isResponse == 1) {
		var numComments = response.getElementsByTagName('numComments')[0].firstChild.data;
		var comments = '';
		if (numComments > 0) {
			for (var x=0; x < numComments; x++) {
				if (response.getElementsByTagName('name')[x].firstChild != undefined) {
					var name = response.getElementsByTagName('name')[x].firstChild.data;
				}
				if (response.getElementsByTagName('date')[x].firstChild != undefined) {
					var date = response.getElementsByTagName('date')[x].firstChild.data;
				}
				if (response.getElementsByTagName('email')[x].firstChild != undefined) {
					var email = response.getElementsByTagName('email')[x].firstChild.data;
				}
				if (response.getElementsByTagName('web')[x].firstChild != undefined) {
					var web = response.getElementsByTagName('web')[x].firstChild.data;
				}
				if (response.getElementsByTagName('text')[x].firstChild != undefined) {
					var text = response.getElementsByTagName('text')[x].firstChild.data;
				}
				comments += '<div class="comments">';
				comments += '<div class="commentsInfo1">';
				if (name != '' && name != undefined)	{
					comments += '<span class="commentsName">' + name + '</span>';
				}
				if (email != '' && email != undefined) {
					comments += '<span class="commentsEmail"> : <a class="commentsLink" href="mailto:' + email + '">email</a></span>';
				}
				if (web != '' && web != undefined) {
					comments += '<span class="commentsWeb"> : <a class="commentsLink" href="' + web + '" target="_blank">web</a></span>';
				}
				comments += '</div>';
				comments += '<div class="commentsInfo2">'
				if (date != '')	{
					comments += '<span class="commentsDate">' + date + '</span>';
				}
				comments += '</div>';
				comments += '<div class="clear" style="margin-top: 3px; margin-bottom: 3px; border-bottom: 1px dotted #949494;">&nbsp;</div>';
				if (text != '') {
					comments += '<span class="commentsText">' + text + '</span><br/>';
				}
				comments += '</div>'
			}
		} else {
			comments = 'no comments';
		}
		document.getElementById("comments").innerHTML = comments;
	} else {
		url = 'functions.php?method=getComments&photoID='+photoid;
		loadXMLDoc(url);
	}
}

function addComment(dummy, isResponse) {
	if (isResponse == 1) {
		getComments();
		document.commentForm.name.value = '<name>';
		document.commentForm.email.value = '<email>';
		document.commentForm.web.value = '<web>';
		document.commentForm.comment.value = '<comment>';
	} else {
		var name = document.commentForm.name.value;
		var email = document.commentForm.email.value;
		var web = document.commentForm.web.value;
		var comment = document.commentForm.comment.value;
		if (email != '<e-mail>' && email != '') {
			var invalidChars = "34,38,39,60,61,62,63,96";
			for (i=0; i<email.length; i++) {
				var character = email.charCodeAt(i);
				if ( ( invalidChars.indexOf(character) != -1 ) || ( email.charCodeAt(i) > 126 ) ) {
					alert("if you're going to enter an e-mail address, at least enter a real one.");
					return false;
				}
			}
		}
		if (comment == '' || comment == '<comment>') {
			alert("you have to at least put a comment in.");
			return false;
		}
		url = 'functions.php?method=addComment&photoID='+photoid+'&name='+name+'&email='+email+'&web='+web+'&comment='+comment;
		loadXMLDoc(url);
		return false;
	}
}

function ratePhoto(action, isResponse) {
	if (isResponse == 1) {
		var rating = response.getElementsByTagName('rating')[0].firstChild.data;
		document.getElementById("photoRatingNumber").innerHTML = rating;
	} else {
		if (action == 'plus') { 
			url = 'functions.php?method=ratePhoto&plusRating='+photoid;
		}
		if (action == 'minus') {
			url = 'functions.php?method=ratePhoto&minusRating='+photoid;
		}
		loadXMLDoc(url);
	}
}

function getThumbnails(dummy, isResponse) {
	if (gotThumbs != 1)	{
		if (isResponse == 1) {
			var numThumbnails = response.getElementsByTagName('numThumbnails')[0].firstChild.data;
			var thumbnails = '<br/>';
			if (numThumbnails > 0) {
				for (var x=0; x < numThumbnails; x++) {
					if (response.getElementsByTagName('filename')[x].firstChild != undefined) {
						var filename = response.getElementsByTagName('filename')[x].firstChild.data;
					}
					if (response.getElementsByTagName('title')[x].firstChild != undefined) {
						var title = response.getElementsByTagName('title')[x].firstChild.data;
					}
					if (filename != '' && filename != undefined)	{
						thumbnails += '<div class="thumbnailContainer"><a href="javascript:void(0);" onclick="gotoPhoto(' + x + ');"><img class="thumbnail" src="images/gallery/thumbnails/' + filename + '" border="0" alt="' + title + '"/></a></div>';
					}
				}
			} else {
				thumbnails = 'no thumbnails';
			}
			document.getElementById("thumbnailViewerContent").innerHTML = thumbnails;
			gotThumbs = 1;
		} else {
			url = 'functions.php?method=getThumbnails&albumID='+albumID;
			loadXMLDoc(url);
		}
	}
}


function getKey(e) {
	// listen for keypress
	if (e) { 
		keyCode = e.which;
	} else if (event) {
		keyCode = event.keyCode;
	} else {
		return;
	}
	// if key is either right or left arrow, go to photo
	if (keyCode == 39) {
		nextPhoto();
	} else if (keyCode == 37) {
		prevPhoto();
	}
	return false;
}