(function() {
  var delta = 10;
  var animation;

  // This function simply iterates through the immediate child elements of .flow_layout
  // adding each just to the right of the previous (unless outside right page border), and just
  // below element above
  var layout = function(noAnimate){
    var layedout      = [];
    var boundingBox   = {};
    var maxRight      = 0;
    var maxBottom     = 0;
    var destination, previousRight;
    var bottoms, above, previousTop;
    

    // Calculate bounding box
    boundingBox.width = $('#sidebar').width();
    jQuery.each($('.flow_layout > *'), function(i, el){
      var done = false;
      if(((boundingBox.width + $(el).outerWidth()) < $(window).width()) && !done) {
        boundingBox.width += $(el).outerWidth();
      } else {
        done = true;
      }
    });
    boundingBox.left = ($(window).width() - boundingBox.width) / 2;
    boundingBox.right = $(window).width() + boundingBox.left;
    previousRight = boundingBox.left - 1;

    jQuery.each($('.flow_layout > *'), function(i, el) {
      var position = {};

      // Calculate left
      position.left = previousRight + 1; 
      position.right = position.left + $(el).outerWidth();
      if(position.right > boundingBox.right - $('#sidebar').width()) {
        position.left = boundingBox.left - 1;
        position.right = position.left + $(el).outerWidth();
      }

      // Calculate top
      above = jQuery.grep(layedout, function (a) { 
        return !(a.right < position.left || a.left > position.right); 
      });
      bottoms = jQuery.map(above, function (a){ return a.bottom });
      bottoms.push($('#sidebar').position().top);
      position.top = Math.max.apply(null, bottoms) + 1;
      position.bottom = position.top + $(el).outerHeight();
      layedout.push(position);
      previousRight = position.right;

      // Assign to element
      if(($(el).css('top') != position.top + 'px') || ($(el).css('left') != position.left + 'px')){
        destination = {
          left: position.left + 'px',
          top: position.top + 'px'
        };

        $(el).css('position', 'absolute');

        if(noAnimate) {
          $(el).css(destination);
        } else {
          $(el).animate(destination);
        }
      }

      maxBottom = Math.max(position.bottom, maxBottom);
      maxRight = Math.max(maxRight, position.right);
    });
    
    // ie6 fix for super small widths
    if ($.browser.msie && jQuery.browser.version == '6.0') {
      if ($(window).width() < 960)
        $('body').css('width', "1100px");
      else
        $('body').css('width', "100%");
    }
      
    // Set containing div height
    // Header height (css) needs to be set correctly here, or these calc won't work
    maxBottom = Math.max.apply(null, [maxBottom, $('#sidebar').position().top + $('#sidebar').height()]);
    $('#main').css('height', (maxBottom - $('#header').height()) + 'px')

    // Place sidebar
    if(noAnimate) {
      $('#sidebar').css({'left': maxRight + 'px'});
    } else {
      $('#sidebar').animate({'left': maxRight + 'px'});
    }
  };

  $(window).load(function(){layout(true)});
  $(window).resize(function(){
    clearTimeout(animation);
    animation = setTimeout(layout, 500);
  });
}());
