// Global Variables
var currentImage = 1;

$(document).ready(function() {
	// Preload all images for the menus
	jQuery.each($("#menu ul li"), function() {
		var menu_num = $(this).attr("id").split('_')[1];
		jQuery ('<img src="/images/menuicon_' + menu_num + '.jpg" />');
	});

	// Set menu rollovers
	$("#menu ul li:not(.current)").hover(
		function() { // the over state
			var menu_num = $(this).attr("id").split('_')[1];
			$('#menuicon_' + menu_num).css('background', 'url(/images/menuicon_' + menu_num + '.jpg) left top no-repeat');
		},
		function() {	// the out state
			var menu_num = $(this).attr("id").split('_')[1];
			$('#menuicon_' + menu_num).css('background', 'none');
	});
	// Set the current menu item
	jQuery.each($("#menu ul li.current"), function() {
		var menu_num = $(this).attr("id").split('_')[1];
		$('#menuicon_' + menu_num).css('background', 'url(/images/menuicon_' + menu_num + '.jpg) left top no-repeat');
	});

	// Preload all rollovers
	jQuery.each($("a.imgrollover img"), function() {
		jQuery ('<img src="' + $(this).attr('src').replace(/(\.[^.]+)$/, '_over$1') + '" />');
	});

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

	// Set other rollovers
	$("a.imgrollover img").hover(
		function() { // the over state
			$(this).attr('src', $(this).attr('src').replace(/(\.[^.]+)$/, '_over$1'));
		},
		function() {	// the out state
			$(this).attr('src', $(this).attr('src').replace(/_over(\.[^.]+)$/, '$1'));
	});


	// 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.
			$("#image_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 Next Button to move to the next image when clicked.
	$("#next_button").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.
		$("#image_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;
	});

	//Set the Previous Button to move to the next image when clicked.
	$("#previous_button").bind("click", function() {
		$(".selected").toggleClass("selected");
		if (currentImage <= 1) { // if the current image is the first, start from the end.
			currentImage = $(".portfolio_thumb").size();
		} 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.
		$("#image_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;
	});

});