var _cols = 2;
var _width = 332;
var _gap = 8;

var _topY, maxHeight = 0;
_max = new Array();

jQuery(function() {
	makeGrid();
});
jQuery(window).resize( function() { makeGrid(); } );
function makeGrid() {
	var cols = Math.max(_cols, parseInt(jQuery('body').innerWidth() / (_width+_gap)));
	jQuery('.box').css('position','absolute');
	for (x=0; x < cols; x++) {
		_max[x] = 0;
	}
	var make = 0;
	jQuery('.box').each(function(i) {
		var pos, cursor, w , altura= 0;
		w = (Math.floor(jQuery(this).outerWidth() / _width));
		cursor = 0;
		if (w>1) {
			for (x=0; x < cols-(w-1); x++) {
				cursor = _max[x] < _max[cursor] ? x : cursor;
			}
			pos = cursor;
			for (var x=0; x<w; x++) {
				altura = Math.max(altura, _max[pos+x]);
			}
			for (var x=0; x<w; x++) {
				_max[pos+x] = parseInt(jQuery(this).outerHeight()) + _gap + altura;
			}
			jQuery(this).css('left', pos*(_width+_gap)).css('top',altura);
			if (altura+topY > maxHeight) {
				maxHeight=_max[pos+w-1];
			}
	}
	else {
		for (x=0; x < cols; x++) {
			cursor = _max[x] < _max[cursor] ? x : cursor;
		}
		jQuery(this).css('left', cursor*(_width+_gap)).css('top',_max[cursor]);
		_max[cursor] += jQuery(this).outerHeight() + _gap;
		if (_max[cursor] > maxHeight) {
			maxHeight=_max[cursor];
		}
	}
	_cols = cols;
});
jQuery('#listing-holder').css('height', maxHeight);
jQuery('#listing-holder').css('width', (_width+_gap)*_cols + 8);
}
