	
/*function initGrid(){
	alert('hello');*/
	var MIN_COLS = 3;
	var COL_WIDTH = 267;
	var GAP = 8; 
	
	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(); } );
	
	if($('#allPosts')){
		var intID = setInterval('arrange()',1000);
	}
	//arrange();
/*}*/
	
	function arrange() {
		
		var images = $('#allPosts img');
		//alert(images.length);
		
		var loaded=true;
		
		for (i=0;i<images.length;i++){
			if(!images[i].complete)	
				loaded=false;
		}
		
		//if(loaded)
		//	clearInterval(intID);
		
		// how many columns fits here?
		var columns = Math.max(MIN_COLS, parseInt($('#allPosts').innerWidth() / (COL_WIDTH+GAP)));
		//alert(columns);
		$('.eachPost').css('width',COL_WIDTH  + 'px');
		$('.twocols').css('width', COL_WIDTH*2 + GAP  );
		$('.threecols').css('width', COL_WIDTH*3 + GAP*2);

		for (x=0; x < columns; x++) {
			maxy[x] = 0;
		}
		
		// lets iterate over all posts
		$('.eachPost').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;
				}
				
				//alert( cursor*(COL_WIDTH+GAP) + offx);

				$(this).css('left', cursor*(COL_WIDTH+GAP)).css('top',maxy[cursor]);
				maxy[cursor] += $(this).outerHeight() + GAP;
				
				
			}
		});
		
		//Set final height of absolute container;
		var height=0;
		for(i=0;i< maxy.length; i++){
			if(	maxy[i] > height)
				height=maxy[i];
		}
		$('#allPosts').css("height",height+"px");
		
	}
