$(function(){
	//Custom fonts
  Cufon.replace('#access');
	
	var 
  $wall = $('#content'),
  speed = 800;  // animation speed
  
  $(".entry-fulltext").hide();
  $(".entry-utility-show-less").hide();
  
  //Masonry setup
  $wall.imagesLoaded(function(){
	  $wall.masonry({
		  columnWidth: 237,
		  itemSelector: '.post:not(.invis)',
		  isFitWidth: true,
		  isAnimated: true,
		  animationOptions: {
			duration: speed,
			easing: 'swing',
			queue: false}   
		});
  });

  //Initialization  
  
 $('#filtering-nav a').click(function(){
	 var colorClass = '.' + $(this).attr('class').split(' ').slice(0,1);
	 filter(colorClass);
 });
  //SWF-adress setup
  SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
  
  //Address change handler
  function handleChange(event) {
    filter(formatTitle(event.path));
  }
  
	  
  //Filter functionality
  function filter(colorClass) {
	 var allElements = $('.post').length;
	
	 if ($('.invis').length == 0 && $('#access').find('.selected').length <= 1) {
		 $wall.children().not(colorClass).not('.invis').toggleClass('invis').fadeOut(speed);
		 $wall.children().find('#highlight').not(colorClass).not('.invis').toggleClass('invis').fadeOut(speed);
		 $('#access').find(colorClass).toggleClass('selected');
	 }
	 else if($wall.children(colorClass).hasClass('invis')) {
		  $wall.find(colorClass).toggleClass('invis').fadeIn(speed);
		  $('#access').find(colorClass).toggleClass('selected');
	 }
	 else {
		  if(($('.invis').length + $wall.children(colorClass).length)  == allElements) {
			  $wall.find('.invis').toggleClass('invis').fadeIn(speed);
			  $('#access').find(colorClass).removeClass('selected');
		  } 
		  else {
			  $wall.find(colorClass).toggleClass('invis').fadeOut(speed);
			  $('#access').find(colorClass).removeClass('selected');
		  }
     }
	 
	 
	 $wall.masonry('reload');
	
	 return false;
  }
  
  // Custom utility functions
  function formatTitle(title) {
	return title = "." + title.substr(1, title.length - 2);
  }
  
  
  //Show more/less functionality
  $(".entry-utility-show-more").click(function()
  {
	$(".entry-fulltext").hide();
	$(".entry-utility-show-more").show();
	$(".entry-utility-show-less").hide();
    $(this).parent().parent().parent().find(".entry-fulltext").show();
	$(this).parent().find(".entry-utility-show-more").hide();
	$(this).parent().find(".entry-utility-show-less").show();
	$wall.masonry();
  });
  $(".entry-utility-show-less").click(function()
  {
    $(this).parent().parent().parent().find(".entry-fulltext").hide();
	$(this).parent().find(".entry-utility-show-more").show();
	$(this).parent().find(".entry-utility-show-less").hide();
	$wall.masonry('reload');
  });
});
