/**
 * 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 = 3;
	var COL_WIDTH = 326;
	var GAP = 0; 
	
	var offx, offy = 0;
	maxy = new Array();
	
	// on site load (DOM READY)
	$(function() {		
		offy = 0;
		offx = 0;		
		arrange(); 
	});
	
	// on window resize, call again
	$(window).resize( function() { arrange(); } );
	
	arrange();
	
	function arrange() {
	
		// how many columns fits here?
		var columns = Math.max(MIN_COLS, parseInt($('.gridBox').innerWidth() / (COL_WIDTH+GAP)));
		$('.NewsSummary').css('width',COL_WIDTH  + 'px');
		$('.twocols').css('width', COL_WIDTH*2 + GAP  );
		$('.threecols').css('width', COL_WIDTH*3 + GAP*2);
		$('.allcols').css('width', parseInt($('.gridBox').innerWidth()) );

		for (x=0; x < columns; x++) {
			maxy[x] = 0;
		}
		
		// lets iterate over all posts
		$('.NewsSummary').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]);
				}

				// modificato per la gestione delle altezze in homepage
				var aTmp = xGetElementsByClassName("NewsSummary", this, "div");
				
				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);
			}
			else {
				
				var parente = $(this).parent().attr("class");
				
				if (parente != "NewsSummary threecols") {
					for (x=0; x < columns; x++) {
						cursor = maxy[x] < maxy[cursor] ? x : cursor;
					}
					$(this).css('left', cursor*(COL_WIDTH+GAP) + offx).css('top',maxy[cursor] + offy);
					maxy[cursor] += $(this).outerHeight() + GAP;
				}
			}
		});
		
		
		
		
		cursor = 0;
		for (var x=0; x<maxy.length; x++) { 
			cursor = maxy[x] > maxy[cursor] ? x : cursor;
		}
		$('#footer').css('top', maxy[cursor]+'px');
	
		
	}
	
	// aggiunto per la gestione delle altezze in homepage
	function xGetElementsByClassName(clsName, parentEle, tagName) {
		var elements = null;
		var found = new Array();
		var re = new RegExp('\\b'+clsName+'\\b');
		if (!parentEle) parentEle = document;
		if (!tagName) tagName = '*';
		if (parentEle.getElementsByTagName) {elements = parentEle.getElementsByTagName(tagName);}
		else if (document.all) {elements = document.all.tags(tagName);}
		if (elements) {
			for (var i = 0; i < elements.length; ++i) {
				if (elements[i].className.search(re) != -1) {
					found[found.length] = elements[i];
				}
			}
		}
		return found;
	}

