/**
 * 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 = 2;
	var COL_WIDTH = 210;
	var GAP = 10; 
	var leftposstart=0;
	
	var offx=0, offy=0, topY = 0;
	maxy = new Array();
	
	// on site load (DOM READY)
	$(function() { 
		if(document.getElementById("grid"))
		{
		offy = $('#grid').offset().top;
		offx = $('#grid').offset().left;
		}
		arrange(); 
	});
	
	// on window resize, call again
	$(window).resize( function() { arrange(); } );
	
	function arrange() {
	
		// how many columns fits here?
		var columns = 4;
		
		
		$('.grid-item').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;
		// lets iterate over all posts
		$('.grid-item').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);
				
				if (altura+topY > maxHeight) {
				  maxHeight=maxy[pos+w-1];
				}

			}
			else {
			
				for (x=0; x < columns; x++) {
					cursor = maxy[x] < maxy[cursor] ? x : cursor;
				}
				if(navigator.userAgent.indexOf('Macintosh') != -1)
				{
					if(navigator.userAgent.indexOf('Safari') != -1)
					{
						ptop=140;
						pleft=197;	
					}
					if(navigator.userAgent.indexOf('Firefox') != -1)
					{
						ptop=175;
						pleft=195;	
					}
				}
				else if(navigator.userAgent.indexOf('MSIE') != -1)
				{
					ptop=175;
					pleft=194;
				}
				else if(navigator.userAgent.indexOf('Chrome') != -1)
				{
					ptop=140;
					pleft=196;	
				}
				else if(navigator.userAgent.indexOf('Safari') != -1)
				{
					ptop=145;
					pleft=196;	
				}
				else if(navigator.userAgent.indexOf('Opera') != -1)
				{
					ptop=175;
					pleft=193;
				}
				else
				{
					ptop=175;
					pleft=196;	
				}
				$(this).css('left', (cursor*(COL_WIDTH+GAP))).css('top',(maxy[cursor]-ptop+83 + offy));
				maxy[cursor] += $(this).outerHeight() + GAP;
				if (maxy[cursor] > maxHeight) {maxHeight=maxy[cursor];}
			}
		});

		$('.test').css('top', maxHeight);
		$.getDocHeight = function(){
			 var D = document;
			 return Math.max(Math.max(D.body.scrollHeight,    D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
		};
		$('#infscr-loading').css('top', $.getDocHeight()-200);

	}
	
	
	

	
