function findPostWithDeeplink(deeplink)
{
	
	if(deeplink == "/")
	{
		//closeCurrentPostAnimation();
		//jQuery('.post').slideDown(300);
		
		//alert('width: '+jQuery('.post .thumb').css('width'));
		jQuery('.post').animate({
			height: jQuery('.post .thumb').css('width'),
			margin: '0 auto 6%',
		},600, function(){
			jQuery(this).removeAttr('style');
		});


		lastCategory = "";
		menuHighlight('all');
	}
	
	else if(deeplink.indexOf("cat") != -1)
	{
		var cid = deeplink.replace("/cat/", "");
		showCat(cid);
		menuHighlight(cid);
	}
	
	else
	{
		jQuery('.post[deeplink='+deeplink+']:first .thumb .fancybox').trigger('click');
  }
  
}

function menuHighlight(selected)
{
	if(selected == 'all')
	{
		jQuery('#mainmenu .catbtn[cid=all]').animate({
			opacity: 1
		}, 300, function() {
			// Animation complete.
		});
		jQuery('#mainmenu .catbtn[cid!=all]').animate({
			opacity: 0.4
		}, 300, function() {
			// Animation complete.
		});
	}
	
	else
	{
		var cid = selected;
		jQuery('#mainmenu .catbtn[cid='+cid+']').animate({
			opacity: 1
		}, 300, function() {
			// Animation complete.
		});
		jQuery('#mainmenu .catbtn[cid!='+cid+']').animate({
			opacity: 0.4
		}, 300, function() {
			// Animation complete.
		});
		
	}
}

var currentPost;
var lastCategory;


function showCat(btncid)
{	
	jQuery.fancybox.close();
	
	//hide all post not in this cid/ show all that are
	//jQuery('.post:not([cats*="\;'+btncid+'\;"])').slideUp(500);
	//jQuery('.post[cats*="\;'+btncid+'\;"]').slideDown(300);
	
	
	
	
	jQuery('.post:not([cats*="\;'+btncid+'\;"])').animate({
		height: '0',
		margin: '0 auto 0',
	},600, function(){
		jQuery(this).css({
			padding: '0 6%'
		});
	});
	
	
	jQuery('.post[cats*="\;'+btncid+'\;"]').animate({
		height: jQuery('.post .thumb').css('width'),
		margin: '0 auto 6%',
	},600, function(){
		jQuery(this).removeAttr('style');
	});
	
	
	
	
	/*
	jQuery('.post[cats*="\;'+btncid+'\;"]').parent().animate({
		width: 'auto'
	}, 300, function() {
		// Animation complete.
	});
	*/
	
	//if current post is not in selected category then collapse
	//if(currentPost && currentPost.attr('cats').indexOf(";"+btncid+";") == -1) closeCurrentPostAnimation();
}

/*
function closeCurrentPostAnimation()
{
	if(currentPost)
	{
		//jQuery('#column-container').animate({width: '83.33%'}, 3000);
		//jQuery('.yearcolumn').animate({width: '9%'}, 3000);
		
		currentPost.find(".content").fadeOut(1000);
		currentPost.find(".thumb").fadeIn(3000);
		
		//currentPost.parent().find('.post').css({width: 'auto'});
		//currentPost.parent().find('.post').css({padding:'6%', margin:'0 auto 6%'});
		
	}
}
*/

function setupUserInterface()
{

	//category tooltips
	jQuery('.catbtn[title]').tooltip({ position:"bottom center", opacity:1, events: {tooltip: "mouseout"} });
	
	//post hovers
	jQuery(".post").hover(function(){
		jQuery(this).find(".meta").not(":animated").slideDown('slow');
	},function(){
		jQuery(this).find(".meta").slideUp('slow');
	});
	
	//post open button
	jQuery(".post .thumb").click(function(){jQuery.address.value(jQuery(this).parent().attr('deeplink'));});
	
	//category clicks
	jQuery('.catbtn[cid!="all"]').click(function(){jQuery.address.value('/cat/'+jQuery(this).attr('cid'));});
	//jQuery('.post .catbtn').click(function(){lastCategory = '/cat/'+jQuery(this).attr('cid'); jQuery.address.value(lastCategory);});
	
	//reset button
	jQuery('#mainmenu .catbtn[cid="all"]').click(function(){jQuery.address.value('/');});
	
	//address change listener
	jQuery.address.change(function(event) {findPostWithDeeplink(event.value);});
	
	
	jQuery('.fancybox').fancybox();
	
}

var jq = jQuery.noConflict();
jq(document).ready(function(){setupUserInterface();});
