function paginate(){
	// check that there is a thumbnail displayed or else hide the next/prev link
	var nextlink = $('#nextprev a.next');
	var prevlink = $('#nextprev a.prev');
	var nexturl = $(nextlink).attr('href');
	var prevurl = $(prevlink).attr('href');
	// fix for ie, http://www.glennjones.net/Post/809/getAttributehrefbug.htm
	var domain = 'http://'+window.location.hostname;
	if (prevurl) {
		prevurl = prevurl.replace(domain,'');
	}
	if (nexturl) {
		nexturl = nexturl.replace(domain,'');
	}
			
	if($('#gallery li').find('a[@href="' + nexturl + '"]').get(0)==null) {
		$(nextlink).replaceWith('<img src="/img/go-next.png" width="36" height="36" alt="Next" title="No more photos" class="photonav none" />');
	}
	if($('#gallery li').find('a[@href="' + prevurl + '"]').get(0)==null) {
		$(prevlink).replaceWith('<img src="/img/go-previous.png" width="36" height="36" alt="Previous" title="No previous photo" class="photonav none" />');
	}
	
	$('#nextprev a').click(function(event){  
		var link = $(this).attr('href');
		var domain = 'http://'+window.location.hostname;
		link = link.replace(domain,'');
		// highlight thumbnail
		$("#gallery img").removeClass('selected');	
		$("#gallery a[@href='"+link+"'] img").addClass('selected');
		showPhoto(link);
		return false;
	});
}

function showPhoto(link){	
	var imgheight = $("#largeimage").attr('height');
	$("#img_placeholder").replaceWith('<div id="img_placeholder"><img src="/images/loading.gif" width="32" height="32" alt="Loading" class="loading icon" /></div>');
	if ($.browser.msie) {
		var url = link +'?' + new Date().getTime();
	} else {
		var url = link;
	}
	$("#img_placeholder").load(url,null,paginate);
}	
	
	
$(document).ready(function() {
	$("#imgwrap").show();
	$("#imgwrap").append('<div id="img_placeholder"><img src="/images/loading.gif" width="32" height="32" alt="Loading" class="loading icon" /></div>');
	// find first image
	var first = $("#gallery #thumb1 a").attr('href');
	if ($.browser.msie) {
		var url = first +'?' + new Date().getTime();
	} else {
		var url = first;
	}
	$("#gallery #thumb1 img").addClass('selected');
	$("#img_placeholder").load(url,null,paginate);
	$('#gallery').click(function(event){  
		if ($(event.target).is("img.thumb")) {
			var link = $(event.target).parent().attr('href');
			// highlight thumbnail
			$("#gallery img").removeClass('selected');
			$(event.target).addClass('selected');
			showPhoto(link);
			return false;
		}				
	});
});