$(document).ready( function() {
	var totalImages = $("#mini-gallery div").length - 1;	
	var currentImageNum = 0;
	var allowClick = "yes";
	var animating = false;


	//set the thickbox link for first image
	setLightboxLink(totalImages,"startup");


	// when you rollover image show tools (prev + next only if more than 1
	$(".pic-tools").hide();

	if (!totalImages) {
		$("#mini-gallery .pt-prev").hide();
		$("#mini-gallery .pt-next").hide();
	}

	$(".pic-holder").hover(function() {
		if(!animating) {
			animating = true;

			$(this).children('ul').fadeIn("normal", function() {
				animating = false;
			});
		}
	},function() {		
		$(this).children('ul').hide();
	});


	
	// rotate images backwards
	function getPrevImage(x) {
		allowClick = "no"

		if (x == 0) {		
			$("#mini-gallery div:visible").fadeOut("fast", function() {
				$("#mini-gallery div:eq("+totalImages+")").fadeIn("fast");
				
				allowClick = "yes";
			});
		}

		else {
			$("#mini-gallery div:visible").fadeOut("fast", function() {
				$("#mini-gallery div:eq("+(x - 1)+")").fadeIn("fast");
				
				allowClick = "yes";
			});
		}

	}


	// rotate images forwards
	function getNextImage(x) {
		allowClick = "no"

		if (x == totalImages) {		
			$("#mini-gallery div:visible").fadeOut("fast", function() {
				$("#mini-gallery div:eq("+0+")").fadeIn("fast");
				
				allowClick = "yes";
			});
		}

		else {
			$("#mini-gallery div:visible").fadeOut("fast", function() {
				$("#mini-gallery div:eq("+(x + 1)+")").fadeIn("fast");
				
				allowClick = "yes";
			});
		}
	}


	// sets the new link + title to the lightbox
	function setLightboxLink(s,direction) {
		if(direction == "backwards") {
			if(s == 0) {
				var newLargeImageLink = $("#mini-gallery div:eq("+totalImages+") img").attr("rel");
				var newLargeImageTitle = $("#mini-gallery div:eq("+totalImages+") img").attr("alt");
			}

			else {
				var newLargeImageLink = $("#mini-gallery div:eq("+(s - 1)+") img").attr("rel");
				var newLargeImageTitle = $("#mini-gallery div:eq("+(s - 1)+") img").attr("alt");
			}			
		}

		else {
			if(s == totalImages) {
				var newLargeImageLink = $("#mini-gallery div:eq("+0+") img").attr("rel");
				var newLargeImageTitle = $("#mini-gallery div:eq("+0+") img").attr("alt");
			}

			else {
				var newLargeImageLink = $("#mini-gallery div:eq("+(s + 1)+") img").attr("rel");
				var newLargeImageTitle = $("#mini-gallery div:eq("+(s + 1)+") img").attr("alt");
			}
		}		
		
		$("#mini-gallery .thickbox").attr("href",newLargeImageLink);
		if(newLargeImageTitle) {
			$("#mini-gallery .thickbox").attr("title",newLargeImageTitle);
		}
		else {
			$("#mini-gallery .thickbox").attr("title","");
		}
	}


	// updates the current number of image
	function updateNum(s,direction) {
		if(direction == "backwards") {
			if(s == 0) {
			currentImageNum = totalImages;
			}

			else {
				currentImageNum = s - 1;
			}
		}

		else {
			if(s == totalImages) {
				currentImageNum = 0;
			}

			else {
				currentImageNum = s + 1;
			}
		}
	}

	
	// bind click event to 'previous image' button
	$("#mini-gallery .pt-prev").click(function() {
		if(allowClick == "yes") {
			getPrevImage(currentImageNum,totalImages);
			setLightboxLink(currentImageNum,"backwards");		
			updateNum(currentImageNum,"backwards");
		}

		else {
			return false;
		}
	});


	// bind click event to 'next image' button
	$("#mini-gallery .pt-next").click(function() {
		if(allowClick == "yes") {
			getNextImage(currentImageNum,totalImages);
			setLightboxLink(currentImageNum,"fowards");		
			updateNum(currentImageNum,"fowards");
		}

		else {
			return false;
		}
	});
});