/*
      ___           ___           ___           ___           ___     
     /\__\         /\  \         /\__\         /\  \         /\  \    
    /:/  /        /::\  \       /:/  /        /::\  \       /::\  \   
   /:/__/        /:/\:\  \     /:/  /        /:/\:\  \     /:/\:\  \  
  /::\  \ ___   /:/  \:\  \   /:/__/  ___   /::\~\:\  \   /::\~\:\  \ 
 /:/\:\  /\__\ /:/__/ \:\__\  |:|  | /\__\ /:/\:\ \:\__\ /:/\:\ \:\__\
 \/__\:\/:/  / \:\  \ /:/  /  |:|  |/:/  / \:\~\:\ \/__/ \/_|::\/:/  /
      \::/  /   \:\  /:/  /   |:|__/:/  /   \:\ \:\__\      |:|::/  / 
      /:/  /     \:\/:/  /     \::::/__/     \:\ \/__/      |:|\/__/  
     /:/  /       \::/  /       ~~~~          \:\__\        |:|  |    
     \/__/         \/__/                       \/__/         \|__|
	      ___           ___           ___           ___                   
	     /\__\         /\  \         /\  \         /\__\                  
	    /:/  /        /::\  \       /::\  \       /:/ _/_         ___     
	   /:/  /        /:/\:\__\     /:/\:\  \     /:/ /\__\       /\__\    
	  /:/  /  ___   /:/ /:/  /    /:/ /::\  \   /:/ /:/  /      /:/  /    
	 /:/__/  /\__\ /:/_/:/__/___ /:/_/:/\:\__\ /:/_/:/  /      /:/__/     
	 \:\  \ /:/  / \:\/:::::/  / \:\/:/  \/__/ \:\/:/  /      /::\  \     
	  \:\  /:/  /   \::/~~/~~~~   \::/__/       \::/__/      /:/\:\  \    
	   \:\/:/  /     \:\~~\        \:\  \        \:\  \      \/__\:\  \   
	    \::/  /       \:\__\        \:\__\        \:\__\          \:\__\  
	     \/__/         \/__/         \/__/         \/__/           \/__/

///////////////////////////////////////////////////////////////////////////////////////
	
	Live Grid* by POWELL (R) MAY (powell.may@gmail.com || powell@iamhovercraft.com)
	Examples and documentation will never be found. this is
	beta for life hit me up for instructions (email above)
	Copyright (c) 2009-2010 P.R.May
	Version: .666 (06.06.09)
	Dual licensed under the MIT and GPL licenses:
	http://www.opensource.org/licenses/mit-license.php
	http://www.gnu.org/licenses/gpl.html
	Requires: jQuery v1.2.6 or later

///////////////////////////////////////////////////////////////////////////////////////

BASED ON THE EXTREMELY HARD WORK OF:

	1) Andreas Pihlstr0m (http://www.suprb.com/)
	2) David DeSandro (http://desandro.com/)

*/

(function($){ 

	$.fn.liveGrid = function(options) {
		
		var defaults = {  
			minCol: 2,  
			colWidth: 206,  
			colGutter: 10,
			container: '#content_main',
			item: '.grid_4_cell'
		};  

		var offx, offy = 0;
		var options = $.extend(defaults, options);
		var columns = Math.max(options.minCol, parseInt($(options.container).innerWidth() / (options.colWidth+options.colGutter)));

		

		maxy = new Array();
		for (x=0; x < columns; x++) {
			maxy[x] = 0;
		}

		

		this.each(function() {


			$(options.item).each(function(){
				var $this = $(this);
				$this.css('position', 'absolute');

				var pos, colID, unitWidth, colHeight = 0;
				var trueWidth = ( options.colWidth + options.colGutter );

				// Calculate individual unit widths
				unitWidth = (Math.floor($this.outerWidth() / options.colWidth));

				colID = 0;

				////////////////////////////////////////////////////////  
				// If the unit width is greater 
				// than the default column width
				// adjust layout accordingly ()
				////////////////////////////////////////////////////////  
				if (unitWidth > 1) {
					for ( x = 0; x < columns - (unitWidth - 1); x++) {
						colID = maxy[x] < maxy[colID] ? x : colID;
					}
					pos = colID;
					for (var x=0; x < unitWidth; x++)  {
						colHeight = Math.max(colHeight, maxy[pos+x]);
						maxy[pos+x] = parseInt($this.outerHeight()) + options.colGutter + colHeight;
					}
					// Set Position
					$this.css({
						'left' : (pos * trueWidth),
						'top': colHeight
					});
				} else {
					for (x=0; x < columns; x++) {
						colID = maxy[x] < maxy[colID] ? x : colID;
					}
					// Set Position
					$this.css({
						'left' : (colID*trueWidth),
						'top': maxy[colID]
					});

					maxy[colID] += $this.outerHeight() + options.colGutter;
				}
			});
			return this; 
		});
	};
})(jQuery);

