$(document).ready(function(){
	
	$('.collection-item > a').click(function(e) {
		e.preventDefault();
		if ($(this).parents('.collection-item').hasClass('open')) {
			gallery_close(this);
		}
		else {
			gallery_open(this);
		}
	});
	
	if (window.location.hash != '') {
		var hash = window.location.hash.substring(1, window.location.hash.length);
		var anchor = $('a[name='+hash+']');
		gallery_close();
		$(anchor).click();
	}
});

function gallery_open(item) {
	var details = $('.item-details', $(item).parents('.collection-item'));
	
	if ($('.collection-item.open').length > 0) {
		$('.collection-item.open .item-details').stop().slideUp(800, 'easeOutQuad', function() {
			$('.collection-item.open .item-spacer').stop().slideUp(800, 'easeOutQuad');
			$(details).stop().slideDown(800, 'easeOutQuad');
			$('.item-spacer', $(item).parents('.collection-item')).slideDown(800, 'easeOutQuad');
			$(this).parents('.collection-item').removeClass('open');
		});
	}
	else {
		$(details).stop().slideDown(800, 'easeOutQuad');
		$('.item-spacer', $(item).parents('.collection-item')).slideDown(800, 'easeOutQuad');
	}
	
	window.location.hash = $(item).attr('name');
	$(item).parents('.collection-item').addClass('open');
}

function gallery_close(item) {
	var details = $('.item-details', $(item).parents('.collection-item'));
	$(details).stop().slideUp(800, 'easeOutQuad');
	$('.item-spacer', $(item).parents('.collection-item')).slideUp(800, 'easeOutQuad');
	$(item).parents('.collection-item').removeClass('open');
	window.location.hash = '';
}
