$(function() {
	var pg = (window.location.search.indexOf('&pg') > -1) ? window.location.search.substring(window.location.search.indexOf('&pg')+4) : 0;
	var newpg = (window.location.hash == '') ? 0 : window.location.hash.substring(1);
	if(window.location.hash != '' && pg != newpg)
		window.location.href = ((window.location.search.indexOf('&pg') > -1) ? window.location.search.substring(0, window.location.search.indexOf('&pg')+4) : window.location.search+'&pg=')+newpg;
	$(document).bind('keydown.switchimg', 
		function(e) {
			e = (e == null) ? event : e; // ie
			keycode = (e.which) ? e.which : e.keyCode;
			if(keycode == 80 || keycode == 37)
				$('#prevpg').click();
			else if(keycode == 78 || keycode == 39)
				$('#nextpg').click();
		});
	setClicks();
});

function setClicks() {
	$('#pgnav a').click(handleClicks);
}

function handleClicks() {
	$.getJSON('getalbum.php'+$(this).attr('href'), function(data) {
		$('#photocont').html(data.html);
		document.title = data.title;
		window.location.hash = data.pg;
		setClicks();
	});
	return false;
}
