// Global Variables
var currentImage = 1;

$(document).ready(function() {
	// Preload all images for the portfolio
	jQuery.each($(".portfolio_thumb"), function() {
		jQuery ('<img src="' + $(this).attr("href") + '" />');
	});


	// Set portfolio thumbnails to swap with the main image.
	$(".portfolio_thumb").bind("click", function() {
		$(".selected").toggleClass("selected");
		if ($(this).attr("href") != '') {
			currentImage = Number($(this).attr("id").split('_')[1]); // Set the currentImage variable to the current ID
			$("#mainimage").attr("src",$(this).attr("href")); // Set the main image to the new source
			var thisWork = $(this).attr("title"); // Get the name of the work.
			$("#caption").html($("#thumb_"+currentImage+"_content").html()); // Set the caption to the descriptive text.
			$(this).toggleClass("selected");
			pageTracker._trackPageview("/portfolio/" + thisWork); // Log the switch to Google Analytics
		}
		return false;
	});
	
	
	//Set the main image to move to the next image when clicked.
	$("#portfolio_image").bind("click", function() {
		$(".selected").toggleClass("selected");
		if (currentImage >= $(".portfolio_thumb").size()) { // if the current image is the last, start from the beginning.
			currentImage = 1;
		} else {
			currentImage = currentImage + 1;
		}
		$("#mainimage").attr("src",$("#thumb_"+currentImage).attr("href")); // Set the main image to the new source
		var thisWork = $("#thumb_"+currentImage).attr("title"); // Get the name of the work.
		$("#caption").html($("#thumb_"+currentImage+"_content").html()); // Set the caption to the descriptive text.
		$("#thumb_"+currentImage).toggleClass("selected");
		pageTracker._trackPageview("/portfolio/" + thisWork); // Log the switch to Google Analytics
		return false;
	});
});