/**
 * Grid-A-Licious(tm)
 * Copyright (c) 2008 Suprb - info(at)suprb(dot)com
 *
 * License Agreement: By downloading Grid-A-Licious(tm),
 * you agree to the following: The copyright information 
 * must remain intact in the product.
 *
 * The product may be used for personal use only, no 
 * commercial projects. You are not free to remove the 
 * copyright information (anywhere).
 * 
 * You are not free to use or copy any of the 
 * "grid-a-licious.js" (this file) code on your own products
 * without asking for permission.
 * 
 * Thanks for understanding.
 */

	var MIN_COLS = 4;
	var COL_WIDTH = 210;
	var GAP = 9; 
	
	var offx, offy = 0;
	maxy = new Array();
	
	// on site load (DOM READY)
	//$(function() { 
	//	offy = $('#allposts').offset().top;
	//	offx = $('#allposts').offset().left;
	//	arrange(); 
	//});
	
	
	// on window resize, call again
	//$(window).resize( function() { arrange(); } );
	$(document).ready(function () {
		//offy = $('#allposts').offset().top;
		//offx = $('#allposts').offset().left;
		$('#allposts  .post').css("position","absolute");
		offy = 0;
		offx = 0;
		arrange();
	});
	
	
	function arrange() {
	
		// how many columns fits here?
		//var columns = Math.max(MIN_COLS, parseInt($('body').innerWidth() / (COL_WIDTH+GAP)));
		var columns = 4;
		$('#allposts .post').css('width',COL_WIDTH  + 'px');


		for (x=0; x < columns; x++) {
			maxy[x] = 0;
		}
		
		// lets iterate over all posts
		$('#allposts  .post').each(function(i) {

			var pos, cursor, w , altura= 0;
	
			w = (Math.floor($(this).outerWidth() / COL_WIDTH));
			cursor = 0;

			if (w>1) {
				for (x=0; x < columns-(w-1); x++) {
					cursor = maxy[x] < maxy[cursor] ? x : cursor;
				}
				pos = cursor;
				
				for (var x=0; x<w; x++) {
					altura = Math.max(altura, maxy[pos+x]);
				}
				for (var x=0; x<w; x++) 
					maxy[pos+x] = parseInt($(this).outerHeight()) + GAP + altura;
					
				$(this).css('left', pos*(COL_WIDTH+GAP) + offx).css('top',altura + offy);

			}
			else {
			
				for (x=0; x < columns; x++) {
					cursor = maxy[x] < maxy[cursor] ? x : cursor;
				}
				
				var margin = 20 * cursor;
				
				$(this).css('left', cursor*(COL_WIDTH+GAP) + offx + margin).css('top',maxy[cursor] + offy);
				maxy[cursor] += $(this).outerHeight() + GAP;
	
	
			}
		});

		var max_values = [];
		max_values.push(Math.max(maxy[0]),Math.max(maxy[1]),Math.max(maxy[2]),Math.max(maxy[3]));
		Array.max = function( array ){
			return Math.max.apply( Math, array );
		};

		$("#allposts").css("height",Array.max(max_values));
	}

