/* Color stuff  */

$(document).ready(function() {
	var range = 180;
	var offset = 255 - range;
	var linkrange = 100;
	var linkoffset = 255 - linkrange;
	var hue = 'rgb(' + (Math.floor(range*Math.random()+offset)) + ',' + (Math.floor(range*Math.random()+offset)) + ',' + (Math.floor(range*Math.random()+offset)) + ')';
	var linkhue = 'rgb(' + (Math.floor(linkrange*Math.random()+linkoffset)) + ',' + (Math.floor(linkrange*Math.random()+linkoffset)) + ',' + (Math.floor(linkrange*Math.random()+linkoffset)) + ')';

	
	/* loop posts and make random colors */
	$('.post').each(function(){
		var color = 'rgb(' + (Math.floor(range*Math.random()+offset)) + ',' + (Math.floor(range*Math.random()+offset)) + ',' + (Math.floor(range*Math.random()+offset)) + ')';
		$(this).css("background-color", color);
	 });
	 
	 			
	$('body').css("background-color", hue);
	$('#footer a').css("color", linkhue);
	$('#footer a:hover').css("color", hue);
	$('.sf-menu li li').css("color", hue);	
	
	/* Superfish */
	$('ul.sf-menu').superfish();
 });
/* Isotope stuff */
	
	var $container = $('#postcontainer'),
	 	$body = $('body'),
	 	$menues = $('.dynamicwidth'),
	  	colW = 163,
	  	columns = null,
	  	currentColumns = null;
	
	$container.imagesLoaded(function(){
		$container.isotope({
		  // options
		  itemSelector : '.item',
		  masonry : {
		    columnWidth : colW,
		  }
		});
	});

// Filter stuff for Isotope
$('ul.catfilters li a').click(function(){
var $this = $(this);
	// don't proceed if already selected
	if ( $this.hasClass('active') ) {
	  return false;
	}
	var $optionSet = $this.parents('#mainnav');
	$optionSet.find('.active').removeClass('active');
	$this.addClass('active');

	var selector = $(this).attr('data-filter');
	$('#postcontainer').isotope({ filter: selector });
	return false;
});

// Resizes the main container when the width is divisible by the column width
$(window).smartresize(function(){
    // check if columns has changed
    currentColumns = Math.floor( ( $body.width() ) / colW );
    if ( currentColumns !== columns ) {
      // set new column count
      columns = currentColumns;
      // apply width to container manually, then trigger relayout
      $container.width( columns * colW )		      
    }  
    alert("resized");
  }).smartresize(); // trigger resize to set container width
	
// Resizes the menu and footer to match the post container. Could be smoother...
$("#postcontainer").resize(function(e){
	var $dynwidth = $('#postcontainer').width();
	if ($dynwidth>580)
		{
			$(".dynamicwidth").width($dynwidth);
			$("#footerinner").width($dynwidth);
			$("#rightfoot").width($dynwidth-480);
		}
		else
		{
			$(".dynamicwidth").width(580);
			$("#footerinner").width(580);
		}
});

