var cpTO; //For timeout

function cyclePhotos(i, maxPhotos) {
	if (i >= 3) $("#small-photos img:visible:first").hide();
	if (i == 0) $("#small-photos img:hidden").show();
	
	$("#small-photos img.thumb").removeClass("highlighted");
	$("#small-photos img.thumb:eq("+i+")").addClass("highlighted");
				
	$("#large-photos div.fphoto").hide();
	$("#large-photos div.fphoto:eq("+i+")").show();
	var nextphoto = (i+1) % maxPhotos;
	cpTO = setTimeout("cyclePhotos(" + nextphoto + ", " + maxPhotos + ");", 4000);
}

$(document).ready(function() {
	$("#small-photos img").each(function(i) {
		$(this).click(function(e) {
			e.preventDefault();
			clearTimeout(cpTO); //Stop auto-cycling slide show
			$("#small-photos img.thumb").removeClass("highlighted");
			$(this).addClass("highlighted");
			$("#large-photos div.fphoto").hide();
			$("#large-photos div.fphoto:eq("+i+")").show();
		});
	});
	$("#thumb-scroller .photo-viewer-arrow-left").click(function(e) {
		e.preventDefault();
		if ($("#small-photos img:hidden").size() > 0) {
			$("#small-photos img:hidden:last").show();
		}
	});
	$("#thumb-scroller .photo-viewer-arrow-right").click(function(e) {
		e.preventDefault();
		if ($("#small-photos img:visible").size() > 3) {
			$("#small-photos img:visible:first").hide();
		}
	});
	
	// Start slide show
	cyclePhotos(0, $("#small-photos img").length);
});
