Event.observe(window, 'load', function() {
	var cont = $('content');
	var tables = cont.getElementsBySelector('table');
	if(tables.length == 0) {
		return;
	}
	
	var tablesindex = [];
	
	// sort the widest table to be first
	// in this way, tables that are able to shrink will do so
	var sorter = function(tablea, tableb) {
		if(Object.isUndefined(tablea.widthCached)) {
			tablea.widthCached = tablea.getWidth();
		}
		if(Object.isUndefined(tableb.widthCached)) {
			tableb.widthCached = tableb.getWidth();
		}
		
		if(tablea.widthCached > tableb.widthCached) {
			return -1;
		} else if(tablea.widthCached < tableb.widthCached) {
			return 1;
		}
		return 0;
	}
	
	tables.sort(sorter);
	
	for(var i = 0; i < tables.length; i++) {
		var table = $(tables[i]);
		if(table.widthCached > bodyWidth && table.descendantOf(cont)) {
			var tablecontainer = document.createElement('div');
			var height = table.getHeight() + 2;
			Element.extend(tablecontainer);
			table.setStyle({
				marginTop: '1px',
				marginBottom: '1px'
			});
			height += 16;
			
			tablecontainer.setStyle({
				width : bodyWidth+'px',
				overflowX : 'auto',
				overflowY : 'hidden',
				height : height+'px'
			});
			table.parentNode.insertBefore(tablecontainer, table);
			tablecontainer.appendChild(table);
		}
	}
});
