$(document).ready(function(){

	// Work
	var 
		$wall = $('#content').find('.wrap'),
		masonryOptions = {
			singleMode: true,
			itemSelector: '.box:not(.invis)',
			animate: false
		}
	;
	$(window).load(function(){
		$wall.masonry(masonryOptions);
		if ( window.location.hash ) {
			var possibleFilterClass = window.location.hash.replace('#', '');
			switch (possibleFilterClass) {
				case 'all' : case 'web' : case 'illustration' : case 'shirts' : case 'paintings' : 
					$wall.children().not('.'+possibleFilterClass).toggleClass('invis').hide();
					$('#work_nav a.'+possibleFilterClass).addClass('active');
					$wall.masonry(masonryOptions);
				break;
			}
		} else {
			$('#work_nav a.all').addClass('active');
		}
	});
	
	// Work Nav
	$('#work_nav a').click(function(){
		var 
			type = $(this).attr('class'),
			filterClass = '.' + type;
		;
		if (filterClass == '.all') {
			$wall.children('.invis')
			.toggleClass('invis').show();
		} else {
			$wall.children().not(filterClass).not('.invis').toggleClass('invis').hide();
			$wall.children(filterClass+'.invis').toggleClass('invis').show();
		}
		$wall.masonry();
		window.location.hash = type;
		return false;
	});
	
	$('.box').hover(function() {
		$(this).stop().animate({'opacity': 1}); 
	},function() { 
		$(this).stop().animate({'opacity': .8}); 
	});
	
	// Toggle Active Link
	$('#work_nav a').click(function() {
		var active = $('.active');
		$(active).removeClass('active');
		$(this).addClass('active');
	});
	
	// Create Box Bleed
	function setBleed() {
		if ($('#work_page #right_inner').length) {
			var elm = $('#work_page #right_inner');
			var offset = $(elm).offset();
			var offsetRight = offset.left + $(elm).outerWidth();
			var windowWidth = $(window).width();
			var bleedWidth = windowWidth - offsetRight;
			var boxHeight = $(elm).height() + 40;
			$('#work_page #right .box_bleed').css({'height':boxHeight,'width':bleedWidth});
		}
	}
	$(document).ready(function() { setBleed(); });
	$(window).resize(function() { setBleed(); });	

	// Open All Comics Links
	$('#open_comics').click(function() {
		$('.comics a').each(function() {
			var linkLoc = $(this).attr('href');
			window.open(linkLoc, '_blank');
		});
	});

});
