/**
 * 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.
 */
 
 	 jQuery.noConflict();


	var MIN_COLS = 4;
	var COL_WIDTH = 88;
	var GAP = 5; 
	
	var offx, offy, topY = 0;
	maxy = new Array();
	
	// on site load (DOM READY)
	jQuery(function() { 
		offy = 25;
		offx = 25;
	
		arrange();
	});
	
	// on window resize, call again
	jQuery(window).resize( function() { arrange(); } );
	

	function arrange() {
		// how many columns fits here?
		var columns = Math.max(MIN_COLS, parseInt(jQuery('body').innerWidth() / (COL_WIDTH+GAP)));
		
		
		jQuery('.eachpost').css('width',COL_WIDTH*2 + GAP*1  + 'px');
		jQuery('.twocols').css('width', COL_WIDTH*4 + GAP*3  + 'px' );
		jQuery('.HighLight').css('width', COL_WIDTH*4 + GAP*3  + 'px' );
		
		if(columns < 8)
		{
		jQuery('#myGallery').css('width',COL_WIDTH*4 - GAP*2  + 'px');
		jQuery('.fourcols').css('width',COL_WIDTH*4 + GAP*3  + 'px');
		}
		else
		{
		jQuery('#myGallery').css('width',COL_WIDTH*8 + GAP*3  + 'px');
		jQuery('.fourcols').css('width',COL_WIDTH*8 + GAP*7  + 'px');
		}
		
		if(columns > 16)
		{
		jQuery('.twocolsLiquid').css('width', COL_WIDTH*0 + GAP*0  + 'px' );
		}
		else
		{
		jQuery('.twocolsLiquid').css('width', COL_WIDTH*4 + GAP*3  + 'px' );
		}
		
		
		for (x=0; x < columns; x++) {
			maxy[x] = 0;
		}
		var maxHeight = 0;
		var boxHeight = window.innerHeight;
		// lets iterate over all posts
		jQuery('.eachpost').each(function(i) {

			var pos, cursor, w , altura= 0;
	
			w = (Math.floor(jQuery(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(jQuery(this).outerHeight()) + GAP + altura;
					
				jQuery(this).css('left', pos*(COL_WIDTH+GAP) + offx).css('top',altura + offy);
				
				if (altura+topY > maxHeight) {
				  maxHeight=maxy[pos+w-1];
				}
				
				if (maxHeight+parseInt(jQuery(this).outerHeight()) > boxHeight)
				{
					
					boxHeight = maxHeight+parseInt(jQuery(this).outerHeight())+70;
				}


			}
			else {
			
				for (x=0; x < columns; x++) {
					cursor = maxy[x] < maxy[cursor] ? x : cursor;
				}

				jQuery(this).css('left', cursor*(COL_WIDTH+GAP) + offx).css('top',maxy[cursor] + offy);

				maxy[cursor] += jQuery(this).outerHeight() + GAP;
				if (maxy[cursor] > maxHeight) {maxHeight=maxy[cursor];}
				
				if (maxHeight+parseInt(jQuery(this).outerHeight()) > boxHeight)
				{
			
					boxHeight = parseInt(jQuery(this).outerHeight())+70;
				}		
			}
			
		});
		
		
		var _docHeight = (boxHeight !== undefined) ? boxHeight : document.body.offsetHeight;
		jQuery('#map_canvas').css('height', _docHeight + 'px');
		
		var _docWidth = (document.width !== undefined) ? document.width : document.body.offsetWidth;
		jQuery('#map_canvas').css('width',document.documentElement.clientWidth + 'px');
		
		//jQuery('.test').css('top', maxHeight);
		
		
	}
	
	
	

	