
var sizeDependentPropertyStore = [];
function setSizeDependentProperty(obj, styleProperty, valueMethod, extraInfo) {
	var definition = extraInfo || {};
	definition.object = obj;
	definition.styleProperty = styleProperty;
	definition.valueMethod = valueMethod;

	sizeDependentPropertyStore.push(definition);
	recalculateSizeDependentProperty(definition);
}
function recalculateSizeDependentPropertiesOfNode(theNode) {
	for (var i = 0; i < sizeDependentPropertyStore.length; i++)
		if (sizeDependentPropertyStore[i].object == theNode) {
			recalculateSizeDependentProperty(sizeDependentPropertyStore[i]);
		}
}
function fullSizeTable(theTable) {
	var headingTable = theTable.cloneNode(false);
	var theadElements = theTable.getElementsByTagName('thead');
	for (var i = 0; i < theadElements.length; i++) {
		var theNode = theadElements[i];
		if (theNode.parentNode == theTable) {
			theTable.removeChild(theNode);
			headingTable.appendChild(theNode);
		}
	}
	var containerDiv = document.createElement('div');
	containerDiv.className = 'autoBodyContainer';
	theTable.parentNode.replaceChild(containerDiv, theTable);
	containerDiv.appendChild(theTable);
	containerDiv.parentNode.insertBefore(headingTable, containerDiv);
	fullSizeSplitTable(headingTable, theTable, containerDiv);
}
function fullSizeSplitTable(headingTable, contentTable, contentContainer) {
	// If provided, we set the height on the content container. This
	// means that the table will become whatever size it needs in order
	// to fit, and the container will be able to scroll.
	if (contentContainer)
		setSizeDependentProperty(contentContainer, 'width', function() { return Math.max(0, windowWidth() - 1) + 'px'; });
	setSizeDependentProperty(contentContainer || contentTable, 'height', function() { return Math.max(0, windowHeight() - calculateOffsetBottom(headingTable) - 1) + 'px'; });

	var windowWidthMinusScrollBarGutter = function() { return Math.max(0, windowWidth() - 20) + 'px'; };
	setSizeDependentProperty(headingTable, 'width', windowWidthMinusScrollBarGutter);
	setSizeDependentProperty(contentTable, 'width', windowWidthMinusScrollBarGutter);
}

function recalculateSizeDependentProperty(definition) {
	definition.object.style[definition.styleProperty] = definition.valueMethod.call(definition);
}
function fixSizeDependentProperties() {
	for (var i = 0; i < sizeDependentPropertyStore.length; i++)
		recalculateSizeDependentProperty(sizeDependentPropertyStore[i]);
}
addEvent(window, 'resize', fixSizeDependentProperties);

function initFullSize() {
	var list = document.getElementsByTagName('table');
	for (var i = 0; i < list.length; i++) {
		if (hasClass(list[i], 'fullSize')) {
			removeClass(list[i], 'fullSize');
			fullSizeTable(list[i]);
		}
	}
}

if (window.addEvent)
	addEvent(window, 'load', initFullSize);

