/**
 * 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 = 192;
	var COL_HEIGHT = 79;
	var GAP = 10; 
	var first_load = 0;
	var anime_delay = 500;
	var position_delay = 1;
	
	var offx = 0;
	var 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(); 
			} );
	


	$(window).load( function() { arrange(); 
		setTimeout(function()
		{
			$('#memo_wrap').css("visibility","visible");
			$("div#memo_wrap").slideToggle('middle');
			offy = 125;
		},100);
		
		setTimeout(function()
		{
					$('#header').css("visibility","visible");
					
					$('#main_container').css("visibility","visible");
					$('#footer').css("visibility","visible");
					$.unblockUI();
		},750);
	});
	arrange();
	
	function arrange() {
	
		// how many columns fits here?
		var columns = Math.max(MIN_COLS, parseInt(($('body').innerWidth() -20) / (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' );

        var max_width = columns * COL_WIDTH + (columns - 1) * GAP;
		offx = (($('body').innerWidth() - max_width) / 2);
		

			
			$('#header .headerbox').animate({"backgroundPosition":offx + ' top',"paddingLeft":offx},position_delay);

			$('#header .hrbox').animate({"marginRight":offx - 11},position_delay);

			$('#footer .footerbox').animate({"paddingLeft": offx, "paddingRight": offx},position_delay);
		
			
		//window.alert('offy' + offy);
		for (x=0; x < columns; x++) {
			maxy[x] = 0;
		}
	
		// lets iterate over all posts
		$('.eachpost').each(function(i) {

			// height adjust
//			var height = $(this).innerHeight();
//			height = (Math.ceil((height-COL_HEIGHT) / (COL_HEIGHT+GAP))) * (COL_HEIGHT+GAP) + COL_HEIGHT - 2;
//			$(this).css('height', height);

			var pos, cursor, w , altura= 0;
	
			w = (Math.floor($(this).outerWidth() / COL_WIDTH));
			//cursor = 0;
			cursor = 1;

			if (w>1) {
				if($(this).hasClass('column1')) pos = 0;
				else if($(this).hasClass('column2')) pos = 1;
				else if($(this).hasClass('column3')) pos = 2;
				else if($(this).hasClass('column4')) pos = 3;
				else if($(this).hasClass('column5')) pos = 4;		
				else 
				{
				    for (x=0; x < columns-(w-1); x++) {
				    //for (x=1; 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;
					
				if (first_load < 2)
				{
					$(this).css('left', pos*(COL_WIDTH+GAP) + offx).css('top',altura + offy);
				}
				else
				{
					//$(this).animate({'left' :  pos*(COL_WIDTH+GAP) + offx}, anime_delay);
					//$(this).animate({'top' : altura + offy}, anime_delay);

					$(this).animate({'left' :  pos*(COL_WIDTH+GAP) + offx, 'top' : altura + offy}, anime_delay);
				}
				
			}
			else {
				if($(this).hasClass('column1')) cursor = 0;
				else if($(this).hasClass('column2')) cursor = 1;
				else if($(this).hasClass('column3')) cursor = 2;
				else if($(this).hasClass('column4')) cursor = 3;
				else if($(this).hasClass('column5')) cursor = 4;
				
				//banner
				else if($(this).hasClass('columnR')) cursor = columns - 1;
				
				else
				{				
				    for (x=0; x < columns; x++) {
				    //for (x=1; x < columns; x++) {
			     		cursor = maxy[x] < maxy[cursor] ? x : cursor;
				    }
				}

				if (first_load < 2)
				{
					$(this).css('left', cursor*(COL_WIDTH+GAP) + offx);
					$(this).css('top',maxy[cursor] + offy);								
				}
				else
				{
					//$(this).animate({'left' :  cursor*(COL_WIDTH+GAP) + offx}, anime_delay);
					//$(this).animate({'top' : maxy[cursor] + offy}, anime_delay);

					$(this).animate({'left' :  cursor*(COL_WIDTH+GAP) + offx, 'top' : maxy[cursor] + offy}, anime_delay);
				}

				maxy[cursor] += $(this).outerHeight() + GAP;
			}
		});
		var max_height = 200;
		for (x=0;x<columns;x++)
		{
			max_height = Math.max(max_height, maxy[x]);
		}
		$("#main_container").css('height', max_height);
	
		if (first_load < 2) first_load++;
	}
