/**
 * dynamically compute and set the visibility and margins of the news in the active boxes
 * @author pedro@brainsum.com
 **/

  $(document).ready(function(){

    var availableHeight = $(".box-content").innerHeight();
    var liSum = 0;
    var liCount = 0;
    $("li").each(function(i) {
      var lastLiHeight = $(this).outerHeight();
      liSum += lastLiHeight;
      liCount++;
    });
    if (liCount > 0) {
      /* If the last li is overflowing the visibl area remove it */ 
      if (liSum > availableHeight && liCount>1) {
        liSum -= $("li:last").outerHeight(); 
        $("li:last").remove();
        liCount--;
        /* And again: If the last li is overflowing the visibl area remove it */ 
        if (liSum > availableHeight && liCount>1) {
          liSum -= $("li:last").outerHeight(); 
          $("li:last").remove();
          liCount--;
          /* And again: If the last li is overflowing the visibl area remove it */ 
          if (liSum > availableHeight && liCount>1) {
            liSum -= $("li:last").outerHeight(); 
            $("li:last").remove();
            liCount--;
            /* And again: If the last li is overflowing the visibl area remove it */ 
            if (liSum > availableHeight && liCount>1) {
              liSum -= $("li:last").outerHeight(); 
              $("li:last").remove();
              liCount--;
            }
          }
        }
      }
      /* Calculate and set proper margin-top / bottom for li elements to fill the available space */
      var increaseMargin = Math.round( ( availableHeight - liSum ) / liCount / 2 );
      $("li").each(function(i) {
        $(this).css('margin-top', increaseMargin);
      });
    }

  });

