var firsttime = true;

$(document).ready(function(){

	// Set tooltip
	$('.right ul li img').tooltip({
		showURL: false,
		delay: 0,
		track: true,
		fixPNG: true,
		showBody: " - ",
		extraClass: "nopngfix",
		top: -100,
		backgroundReverse: true
	});
	
	// Visiting a category URL
	if (document.location.href.indexOf('show=') !== -1) {
		var show = document.location.href.split('show=')[1];
		showGrid(show);
	}

	// Change projects when clicking on top links
	$('.top a').click(function(){

		// Remove any selected class
		$('.top a').each(function() {
			$(this).removeClass('selected');
		});

		// Add selected class
		$(this).addClass('selected');
		
		var url = $(this).attr('href');
		var show = url.split('show=')[1];
		
		showGrid(show);
		return false;
	});
});

function showGrid(show) {
	var total = $('.right ul li img').length - 1;
	
	// Bring all images into focus
	if (!firsttime) {
		$('.right ul li img').each(function(i) {
			$(this).animate({opacity: 1}, 500, function(){
				if (i == total) {
					
					// Hide all other images if not all
					if (show !== "all") {
						$('.right ul li img').each(function() {
							if (!$(this).parent().parent().hasClass(show)) {
								$(this).animate({opacity: 0.2}, 500);
							}
						});
					}
				}
			});
		});
	}
	else {
		firsttime = false;
		if (show !== "all") {
			$('.right ul li img').each(function() {
				if (!$(this).parent().parent().hasClass(show)) {
					$(this).animate({opacity: 0.2}, 500);
				}
			});
		}
	}
}