/*
Smart columns method modified from
http://www.sohtanaka.com/web-design/smart-columns-w-css-jquery/
*/
$(document).ready(function(){

  function smartColumns() { //Create a function that calculates the smart columns

    //Reset column size to a 100% once view port has been adjusted
    $("ul.column").css({ 'width' : "100%"});

    var colWrap = $("ul.column").width(); //Get the width of row
    var colNum = Math.floor(colWrap / 300); //Find how many columns of X px can fit per row / then round it down to a whole number
    var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column

    $("ul.column").css({ 'width' : colWrap}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
    $("ul.column li").css({ 'width' : colFixed}); //Set exact width of the re-adjusted column

    $("span.panel-inner").css ("height", "");
    $("ul.column li.clearer").remove();
    if (colNum > 1) {
      $("ul.column li").each (function(index) {
	if (index>0 && index%colNum==0) {
	  $(this).before ("<li class=\"clearer\"><!-- --></li>");
	}
      });
      var innerHeight = 0;
      $("span.panel-inner").each(function(index) {
	innerHeight = Math.max (innerHeight, $(this).height());
      });
      $("span.panel-inner").css ("height", innerHeight);
    }
  }

  smartColumns();//Execute the function when page loads

  $(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
    smartColumns();
  });

});
