$(function() {
	var getViewPortDimensions = function() {
		return {
			w	: self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth),
			h	: self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight)
		}
	};
	
	var myParseInt = function(str) {
		if (isNaN(parseInt(str, 10)))
			return 0;
		else
			return parseInt(str, 10);
	}
	
	var getTotalHeight = function(elt) {
		elt = $(elt);
		
		if (elt.size())
			return elt.height() 
				+ myParseInt(elt.css('paddingTop')) 
				+ myParseInt(elt.css('paddingBottom'))
				+ myParseInt(elt.css('borderTopWidth'))
				+ myParseInt(elt.css('borderBottomWidth'));
		else
			return 0;
	}
	
	// Redimensionem la capa central per ocupar el maxim espai vertical possible.
	var redimensionar = function() {
		var headerHeight = $('#header').height();
		var footerHeight = $('#footer').height();
		var clientViewPortHeight = getViewPortDimensions().h;
		
		var mainHeight = clientViewPortHeight - (headerHeight + footerHeight + 5); // Els 5 pixels més són per si de cas
		var contingutHeight = mainHeight - (getTotalHeight($('.submenu').eq(0)) + getTotalHeight($('#breadcrumb')));
		
		$('#main').height(mainHeight);
		$('#content').height(contingutHeight);
		
		// Accions especifiques per algunes pŕgines
		
		// En la pŕgina /intranet/principal.asp, redimensionem el contenidor central
		var tableLayout = $('#table_layout');
		
		if (tableLayout.size()) {
			tableLayoutHeight = contingutHeight - myParseInt(tableLayout.css('top'));
			tableLayout.height(tableLayoutHeight);
		}
	}
	
	window.onload = function() {
		$(window)
			.resize(redimensionar)
			.resize();
	}
	
	
});

