	var MIN_COLS = 2;
	var COL_WIDTH = 170;
	var GAP = 14; 
	
	var offx, offy = 0;
	maxy = new Array();
	
	// on site load (DOM READY)
	$(function() { 
	
		offy = $('#foliogrid').offset().top;
		offx = $('#foliogrid').offset().left;
		
		arrange(); 

		$('.front-page #footer-block').animate({bottom: '-7px'},1500).animate({bottom: '-70px'},400);
		$('#footer-block').css('bottom','-70px');
		$('#footer-block').hover(
			function(){ $(this).animate({bottom: '-7px'},200)},
			function(){ $(this).animate({bottom: '-70px'},200)}
		);
		
		$('.front-page img').attr('title','');
		$('.pages li:first').css('border',0);
		
/*
		$('.front-page .post').hover(
			function(){ $(this).find('p').slideToggle(100)},
			function(){ $(this).find('p').css('display','none')}
		);
*/

		
	});
	
	// on window resize, call again
	$(window).resize( function() { arrange(); } );
	
	arrange();
	
	function arrange() {
	
		// how many columns fits here?
		var columns = Math.max(MIN_COLS, parseInt($('body').innerWidth() / (COL_WIDTH+GAP)));
		$('.post').css('width',COL_WIDTH  + 'px');

		for (x=0; x < columns; x++) {
			maxy[x] = 0;
		}
		
		// lets iterate over all posts
		$('.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;
				}

				$(this).css('left', cursor*(COL_WIDTH+GAP) + offx).css('top',maxy[cursor] + offy);
				maxy[cursor] += $(this).outerHeight() + GAP;
			}
		});
	
	}
