function getHash() {
  var hash = window.location.hash;
  return hash.substring(1); // remove #
}


$(function(){

	var 
		speed = 1000,   // animation speed
		$wall = $("#portfolio_primary")
	;

	$wall.masonry({
		columnWidth: 247, 
		// only apply masonry layout to visible elements
		itemSelector: '.box:not(.invis)',
		animate: true,
		animationOptions: {
			duration: speed,
			queue: false
		}
	});

	$('#portfolio_filtering_nav a').click(function(){
		$('#portfolio_filtering_nav a').each(function(index){
			if($(this).hasClass('active')) $(this).removeClass('active');
		});
		var categoryClass = '.' + $(this).attr('class');
		$(this).addClass('active')
		/* use fadeIn()/fadeOut() as opposed to fadeTo() to show/hide bricks */
		
		if(categoryClass=='.all') {
			// show all hidden boxes
			$wall.children('.invis')
				.toggleClass('invis').fadeIn();
		} else {    
			// hide visible boxes 
			$wall.children().not(categoryClass).not('.invis')
				.toggleClass('invis').fadeOut();
			// show hidden boxes
			$wall.children(categoryClass+'.invis')
				.toggleClass('invis').fadeIn();
		}
		$wall.masonry();

		return false;
	});
	
	$('a.portfolio_modal_box').click(function(e) {
		if($(this).parent().hasClass('invis')) {
			$(this).removeClass('portfolio_modal_box');
			e.preventDefault();
		}
	}); 
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
	    return '<div id="portfolio_content_title"><b>Portfolio Content: '+title+'</b></div>';
	}

	
	$("a.portfolio_modal_box").fancybox({
				'titleShow'		: false,
				'scrolling' : 'no',
				'titleFormat'	: formatTitle
			});
			
	var itemName = getHash();		
	if(itemName.length > 0 && $("a[name="+itemName+"]").length){
        $("a[name="+itemName+"]").fancybox().trigger('click');
	}
	
	window.scrollTo(0,0);
});
