var MIN_COLS = 2;
var COL_WIDTH = 245;
var GAP = 1;//30; 

var offx = 0;
var offy = 0;
var topY = 0;
maxy = new Array();	

$(window).resize( function() { arrange(); } );


var isTest = false;


function arrange() {

	var bodyWidth = $('body').innerWidth();
	var columns = 0;
	if(bodyWidth <= 1280)
		columns = 4;
	else
		columns = Math.max(MIN_COLS, parseInt($('body').innerWidth() / (COL_WIDTH+GAP)));
		
	var rw = "auto";
	var isSingle = document.getElementById('singleContent');
	
	if (isSingle) {
		columns = 4;
		
		$('#leftColumn').css('opacity', 1);
		$('#middleColumn').css('opacity', 1);
		$('#rightColumn').css('opacity', 1);
		
		if (isTagOpen) {
			rw = '1280px';
		}
		else if ($(window).width() <= 1024) {
			rw = '1024px';
		}
		
	}
	
	$('#rap').css('width',rw);
	$('#rightColumn').css('float','right');
	$('#rightColumn').css('float','left');
	
	$('.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;
	}
	var maxHeight = 0;
	var maxLastHeight = 0;
	// lets iterate over all posts
	var cc = 0;
	var rows = 0;
	
	$('.eachpost').each(function(i) {

		var pos, cursor, w , altura= 0;
		
		$(this).css('opacity', 1);

		w = (Math.floor($(this).outerWidth() / COL_WIDTH));
		cursor = 0;
		cursor = cc%columns;
		if (cursor == 0) {
			rows++;
		}
		
		if ($(this).attr("id") == "pagenavi" && rows > 2) {
			// put pagination div to last
			$(this).css('left', (columns-1)*(COL_WIDTH+GAP) + offx).css('top',maxy[columns-1]);
		}
		else {
			$(this).css('left', cursor*(COL_WIDTH+GAP) + offx).css('top',maxy[cursor] + offy);
		}
		
		if (w>1) {
			for (var x=0; x<w; x++) {
				maxy[cursor+x] = parseInt($(this).outerHeight()) + GAP + altura -2;
			}
		}
		else {
			maxy[cursor] += $(this).outerHeight() + GAP - 2;
		}
		
		if (maxy[cursor] > maxHeight) {
			maxHeight=maxy[cursor];
		}
		
		cc += w;
		
	});
	
	
	var w = 0;
	w = ((bodyWidth -(columns * COL_WIDTH)) / 2);
	
	if (!isTagOpen) {
		if (w < 20) {
			w = 20;	
		}
	}
	else {
		w -= 130;
		if (w < 0) {
			w = 0;	
		}
	}
	$('#content').css('left',w+'px');
   
	if (maxHeight == 0) {
		maxHeight = $('body').innerHeight();
	}
	maxHeight += 160;
	
	if (maxHeight < $(window).height()) {
		maxHeight = $(window).height();
	}
	
	var h = 0;
	h = maxHeight - 26;
	$('#footer').css('top',h+'px');
	
	
	w = 0;
	if (isTagOpen) {
		w = $('#sideTags').innerWidth();
	}
	w += Number($("#content").css("left").replace(/px/, ""))
	bodyWidth = Math.max($('body').innerWidth(), columns * COL_WIDTH + w + 20);
	
	
	if (bodyWidth < 1024) {
		bodyWidth = 1024;
	}
	$("#navigationBar").css('width',bodyWidth + "px");
	$("#footer").css("width",bodyWidth + "px");
	
	
	var ws = 52;
	if (isSingle) {
		ws = 56;	
	}
	adjustNavPos(w, columns * COL_WIDTH + w - ws);
	
	
}

