$(document).ready(function() {
	// Change the caption of the starting image (if one exists)
	var image_caption = $("#image_gallery li.selected a").attr("title"); // Grab title text from active gallery li
	$(".portfolio h2").append(image_caption); // Append caption text to h2
	
	// Gallery images
    $("#image_gallery a").click(function(event) {
		// Prevent normal link and switch out placeholder image
    	event.preventDefault();
    	var image = $(this).attr("href");
    	$("#starting_image img").attr("src", image);

		// Add/remove class to the clicked link (to highlight with color)
		$("li.selected").removeClass("selected");
		$(this).parent().addClass("selected");
		
		// Change the caption of the selected image (if one exists)
		var image_caption = $("#image_gallery li.selected a").attr("title"); // Grab title text from active gallery li
		$(".portfolio h2").empty().append(image_caption); // Empty h2 text in portfolio DIV and append caption text to h2
    });

	// Add Prev / Next to gallery links
	$("#image_gallery").prepend("<li><a href=\"#\" class=\"previous\">&lt;</a></li>").append("<li><a href=\"#\" class=\"next\">&gt;</a></li>");
	// Function for Next link
	$("#image_gallery a.next").click(function(event) {
		// Prevent normal link
    	event.preventDefault();
		// Add/remove class to the clicked link (to highlight with color)
		if ($("#image_gallery li.selected").next().children("a").attr("href") != '#') {
			$("#image_gallery li.selected").removeClass("selected").next().addClass("selected");
			// Switch out placeholder image
			var image = $("#image_gallery li.selected a").attr("href");
			$("#starting_image img").attr("src", image);

			// Change the caption of the selected image (if one exists)
			var image_caption = $("#image_gallery li.selected a").attr("title"); // Grab title text from active gallery li
			$(".portfolio h2").empty().append(image_caption); // Empty h2 text in portfolio DIV and append caption text to h2
		}
    });

	// Function for Previous link
	$("#image_gallery a.previous").click(function(event) {
		// Prevent normal link
    	event.preventDefault();
		// Add/remove class to the clicked link (to highlight with color)
		if ($("#image_gallery li.selected").prev().children("a").attr("href") != '#') {
			$("#image_gallery li.selected").removeClass("selected").prev().addClass("selected");
			// Switch out placeholder image
			var image = $("#image_gallery li.selected a").attr("href");
			$("#starting_image img").attr("src", image);

			// Change the caption of the selected image (if one exists)
			var image_caption = $("#image_gallery li.selected a").attr("title"); // Grab title text from active gallery li
			$(".portfolio h2").empty().append(image_caption); // Empty h2 text in portfolio DIV and append caption text to h2
		}
    });
});