var first_image = 0;
var last_image = 0;
var prev_image = 0;
var next_image = 0;

var thumbs = null;

$(document).ready(function(){
	thumbs = $("a[rel='photo-thumbs']");
	
	thumbs.bind("click", function(event){
		var obj = $(this);

		loadPhoto(obj);
		return false;
	});

	first_image = next_image = $(".thumbnails a:first()");
	last_image = prev_image = $(".thumbnails a:last()");

	loadPhoto(first_image);

	$("span.left a").bind("click", function(){
		loadPrevPhoto();
		return false;
	});
	
	$("span.right a").bind("click", function(){
		loadNextPhoto();
		return false;
	});
});

var loadPhoto = function(obj){
	thumbs.fadeTo("fast", 1);
	obj.fadeTo("normal", 0.5).blur();

	$("#photo-caption").text(obj.attr('title') ? '> ' + obj.attr('title') : '');
	$(".photoholder").css('background', 'transparent none no-repeat scroll center center');
	$("#photo-loading").attr('src', 'images/loading.gif');

	$("<img src='" + obj.attr('href') + "' />").load(function(){
		$("#photo-loading").attr('src', 'images/blank.gif');
		$(".photoholder").css('background', 'transparent url(' + obj.attr('href') + ') no-repeat scroll center center');
	});

	prev_image = obj.prev("a");
	if(typeof(prev_image.attr('href')) == 'undefined'){
		prev_image = last_image;
	}

	next_image = obj.next("a");
	if(typeof(next_image.attr('href')) == 'undefined')
	{
		next_image = first_image;
	}
}

var loadPrevPhoto = function(){
	loadPhoto(prev_image);
}

var loadNextPhoto = function(){
	loadPhoto(next_image);
}