$(document).ready(function() {
	// Setup tabs for projects page
	$("#project-nav ul").tabs("div#project-gallery > div.post");
	// Show the description for the first project
	$("#project-nav li span:first").show();
	// Show and hide appropriate descriptions
	$("#project-nav li").click(function() {
		$('span').hide();
		$(this).find('span').show();
		// Fix a bug that keeps hiding my captions
		$('.image-wrap span').show();
		// Update large image wrap
		if ($('.post:visible .gallery-item').is('.active')) {
			var url = $('.post:visible .active').find("a").attr("href");
			var title = $('.post:visible .active').find("a img").attr("alt");
			// Get handle to large image container/wrapper
			var wrap = $(".image-wrap");
			// The large image
			var img = new Image();
			// Call function after it loads
			img.onload = function() {
				// Change the image and title
				wrap.find("span").html(title);
				wrap.find("img").attr("src", url);
			};
			// Begin loading the image
			img.src = url;
		} else {
			$('.post:visible .gallery .gallery-item').filter(':first').click();
		}
		
	});
	
	// Remove line breaks from WordPress generated image gallery
	$(".gallery br").remove();
	
	// Setup scrollable and set .gallery to default items container/wrapper
	$(".scrollable").scrollable({items:".gallery"});
	
	// Activate scrollable navigation
	$(".gallery-item").click(function(event) {
		// Deactivate the link to prevent leaving the page
		event.preventDefault();
		// Show that awesome apng preloader
		 $('#loader .loading').show();
		// Get large image URL and title from thumbnail link
		var url = $(this).find("a").attr("href");
		var title = $(this).find("a img").attr("alt");
		// Get handle to large image container/wrapper
		var wrap = $(".image-wrap");
		// The large image
		var img = new Image();
		// Call function after it loads
		img.onload = function() {
			// Hide that awesome apng preloader
			$('#loader .loading').fadeOut();
			// Change the image and title
			wrap.find("span").html(title);
			wrap.find("img").attr("src", url);	
		};
		// Begin loading the image
		img.src = url;
	// Activate and load the first item
	}).filter(":first").click();
});
