/**
 * 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 = 270;
	var GAP = 10; 
	
	var offx, offy = 0;
	maxy = new Array();
	
//	var MAX_HEIGHT = 0;
//	var COLUMNS = 0;
	
	// on site load (DOM READY)
	$(function() { 
		offy = $('#allposts').offset().top;
		offx = $('#allposts').offset().left;
		arrange();
//		arrangePosts();
//		arrangeFooter();
	});
	
	// on window resize, call again
//	$(window).resize( function() { arrangePosts(); arrangeFooter();} );
	$(window).resize( function() { arrange(); } );
	
// was fuer ein schwachsinn… das muss man doch nicht alles 2 mal berechnen!!	
//	arrange();
	
//	function arrangePosts() {
	function arrange() {	
		var MAX_HEIGHT = 0;
		// how many COLUMNS fits here?
		//var 
		var COLUMNS = Math.max(MIN_COLS, parseInt($('body').innerWidth() / (COL_WIDTH+GAP)));
		$('.eachpost').css('width',COL_WIDTH  + 'px');
		$('.footer').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;
		}

		// lets iterate over all posts
		$('.eachpost').each(function(i) {

//			alert( $(this) );			
//			if( $(this):has('#menuItem')){
//				alert('header element!');
//				return true;
//			}
			if( $(this).is(":hidden")){
				//			for(i = 0; i < COLUMNS; ++i){
//				maxy[i] = 0;
//				}
				return false; // break
			}
			
			var pos, cursor, w , altura= 0;
	
			w = (Math.floor($(this).outerWidth() / COL_WIDTH));
			cursor = 0;

			// if post spans more than one column
			if (w > 1) {
				for (x = 0; x < COLUMNS - (w - 1); x++) {
					if(maxy[x] < maxy[cursor]){
						cursor = x;
					}
				}
				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 post spans only one column
			else {
			
				for (x = 0; x < COLUMNS; x++) {
					if(maxy[x] < maxy[cursor]){
						cursor = x;
					}
				}

				$(this).css('left', cursor*(COL_WIDTH+GAP) + offx).css('top',maxy[cursor] + offy);
				maxy[cursor] += $(this).outerHeight() + GAP;
			}	
		});
//	}
//		$('#container').css('height', MAX_HEIGHT);
//		$('#container').css('width', COLUMNS * (COL_WIDTH + GAP) + offx);
	
		/////////////////////// arranging footer //////////////////////////////
//	function arrangeFooter(){
		
		for(i = 0; i < COLUMNS; ++i){
			MAX_HEIGHT = Math.max(MAX_HEIGHT, maxy[i]);
		}

//		$('#container').css('height', MAX_HEIGHT);
		var col = 0;
		var row = 0;
		var notFirstRun = false;
		
		var footerHeight = parseInt($('.footer').css('height'));
		
		// puts as many footer divs in a row as there are columns
		$('.footer').each(function(i){

			col = col % COLUMNS;

			if(col == 0 && notFirstRun){
				++row;
			}
			$(this).css('top', MAX_HEIGHT + row * (footerHeight + GAP) + offy);
			$(this).css('left', col * (COL_WIDTH + GAP) + offx);

			++col;
			// daemlich leider
//			for(i = 0; i < COLUMNS; ++i){
//				maxy[i] = 0;
//			}
//			MAX_HEIGHT = 0;
			notFirstRun = true;
		});
	}
	
	function hidePosts(){
//		if ($("#container").is(":hidden")) {
//			$("#container").slideDown("slow");
//			$("#container").slideToggle("slow");
//		} else {
	//		$("div.eachpost").hide();
//			$("#container").slideToggle("slow");
//			$("#container").slideUp("slow");
//			MAX_HEIGHT = 0;
			//arrangePosts()
//		$('.eachpost').slideToggle('slow');
//		$('#container').slideToggle('slow');
//		}
//		arrange();
		//arrangeFooter();
		return false;
	}
