/**
 * 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 = 3;
	var COL_WIDTH = 230;
	var GAP = 15; 
	
	var offx, offy = 0;
	maxy = new Array();
	
	// on site load (DOM READY)
	$(function() { 
		offy = $('#allposts').offset().top +30;
		if($("body").hasClass("singletemplate")){
			offx = $('#allposts').offset().left +245;
			// offset comments panel
			$("div.leftlowernav").css("top",$("div.leftnav").height() + 70);
		} else {
			offx = $('#allposts').offset().left;
		}
		$('.eachpost').hide();
		setTimeout(function() { arrange(); }, 1000);
		
		// Button rollovers
		$("img[src*='btn-'],input[src*='btn-']").each(function(i,btn){
			var oldSrc = $(btn).attr('src');
			if(oldSrc.match('.png')){
				var newSrc = oldSrc.replace(/.png/,'-o.png');
			} else if(oldSrc.match('.gif')){
				var newSrc = oldSrc.replace(/.gif/,'-o.gif');
			}
			
			$(btn).mouseover(function(evt){ $(btn).attr('src',newSrc) });
			$(btn).mouseout(function(evt){ $(btn).attr('src',oldSrc) });

		});
	});
	
	// on window resize, call again
	$(window).resize( function() { arrange(); } );
	
	function arrange() {
	
		// how many columns fits here?
		if($("body").hasClass("singletemplate")){
			var columns = Math.max(MIN_COLS, parseInt(($('body').innerWidth()-245) / (COL_WIDTH+GAP)));
		} else {
			var columns = Math.max(MIN_COLS, parseInt($('body').innerWidth() / (COL_WIDTH+GAP)));
		}
		
		$('.eachpost').css('width',COL_WIDTH + 'px');
		$('.twocols').css('width', (COL_WIDTH*2 + GAP) + 'px');
		$('.threecols').css('width', (COL_WIDTH*3 + GAP*2) + 'px');

		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;
					//maxy[pos+x] = parseInt($(this).outerHeight()) + GAP + altura;
					
				$(this).css('position','absolute').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('position','absolute').css('left', cursor*(COL_WIDTH+GAP) + offx).css('top',maxy[cursor] + offy);
				maxy[cursor] += $(this).outerHeight() + GAP;
				//maxy[cursor] += $(this).outerHeight() + GAP;
				if($('body').innerWidth() < 996){ $(this).css('width','230px'); }
			}
			$('.eachpost').fadeIn(500);
			var biggest = 10;
for (x=0;x<maxy.length;x++) {
if (maxy[x] > biggest) {
biggest = parseInt(maxy[x]); //right here
}
};
var leftnavh = $('.leftnav').height() + $('.leftlowernav').height() + 60;
if (leftnavh > biggest) {
biggest = leftnavh ; //right here
};
$('.eachpostcontainer').css('height', biggest );
$('.footer').css("display","block");
		});
	}