/**
 * 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 = 215;
	var GAP = 0; 
	var GAPL = 30; 
	
	var offx, offy, topY = 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(); } );
	

	function arrange() {
		// how many columns fits here?
		var columns = Math.max(MIN_COLS, parseInt($('#allposts').innerWidth() / (COL_WIDTH+GAP)));
		
		
		$('.eachpost').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
		$('.eachpost').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+GAPL)).css('top',altura);
				
				if (altura+topY > maxHeight) {
				  maxHeight=maxy[pos+w-1];
				}
				

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

				$(this).css('left', cursor*(COL_WIDTH+GAPL)).css('top',maxy[cursor]);

				maxy[cursor] += $(this).outerHeight() + GAP;
				if (maxy[cursor] > maxHeight) {maxHeight=maxy[cursor];}
				
				
			}
		});

		//判断position=absolute的高度，赋值给div;

		var eachpostLen = $(".eachpost").length;

		$(".Bcon-hidden").height(parseInt($(".eachpost")[eachpostLen-1].offsetHeight)+parseInt($(".eachpost")[eachpostLen-1].style.top)+'px');
	/*	var eachpostLen = $(".eachpost").length;
		var Maxsum = [];
		var lenyu = eachpostLen%4;
		if(eachpostLen<=4){
			for(var h=0;h<eachpostLen;h++){
				Maxsum.push(parseInt($(".eachpost")[h].offsetHeight));
			}
			Maxsum.sort(function(a,b){
				if(a-b<0){
					return 1;
				}else{
					return -1;
				}
			});
			$(".Bcon-hidden").height(parseInt(Maxsum[0])+'px');
		}

		if(eachpostLen>4){

			if(lenyu == 0){
				Maxsum.push(parseInt($(".eachpost")[eachpostLen-1].offsetHeight));
				Maxsum.push(parseInt($(".eachpost")[eachpostLen-2].offsetHeight));
				Maxsum.push(parseInt($(".eachpost")[eachpostLen-3].offsetHeight));
				Maxsum.push(parseInt($(".eachpost")[eachpostLen-4].offsetHeight));
			Maxsum.sort(function(a,b){
				if(a-b<0){
					return 1;
				}else{
					return -1;
				}
			});
			$(".Bcon-hidden").height(parseInt(Maxsum[0])+'px');
		}
	*/	
	}
	
	
	

	
