/***************************

	fixSubPageFooter.js

	Fixes the footer on subpages by calculating the 
	heights of various columns and moving the footer
	accordingly.
	
	Necessary due to the particular positioning used
	to avoid tables.

***************************/

var replacementThreshold = 60;	// Threshold beyond which we'll convert the width (in %)
var fixWidth = 500;				// The value to set the new table to
var tableLabel = '_table';		// Used to create an ID for a table that doesn't have one assigned
var tableCount = 0;				// Initialize the counter for on the fly table IDs 				
var sideBarWidth = 178;			// Width of the related links sidebar (easier just to hard code it)


// Write the calls to the X library into the page as a js check
if (document.getElementById || document.all) { // minimum dhtml support required
  document.write("<"+"script type='text/javascript' src='../JavaScript/x_core.js'><"+"/script>");
  document.write("<"+"script type='text/javascript' src='../JavaScript/x_event.js'><"+"/script>");
  document.write("<"+"style type='text/css'>#footer{visibility:hidden;}<"+"/style>");
  window.onload = winOnLoad;
}



// Grab all tables on the page and run the replacement function
function getTables(){
	if(document.getElementsByTagName && document.createElement){
		// Determine the size to apply to the table
			
		// If there's no sidebar on the page, change the size allocated to it
		if(!document.getElementById('sideBar')) {
			sideBarWidth = 15;
		}
		fixWidth = xWidth('breadcrumbs') - sideBarWidth;
		
		// Find and replace the table(s)		
		myTables=document.getElementsByTagName('table');// Generate a list of tables
		scanandreplace(myTables);// Resize the tables
	}
}
//Re-size tables that are too big for the page
function scanandreplace(myTables){
	for(i=0;i<myTables.length;i++){
		// Assign an ID value to tables that don't have one
		if(!myTables[i].id) {
			myTables[i].id = tableLabel+tableCount;
			tableCount++;
		}
		// Alert string for testing
		var msgString = "";
		msgString += myTables[i].id + ":";
	
		var myWidth = myTables[i].width;
		// if this is a percentage width
		if(myWidth.indexOf('%') > 0) {
			myWidth = myWidth.substr(0,myWidth.indexOf('%'));
			var myWidth = parseInt(myWidth); // convert to an integer
			// Test to see if this table is too wide
			if(myWidth >= replacementThreshold) {
				xWidth(myTables[i].id, fixWidth);
				msgString += " I am a percentage width table, my width is ";
				msgString += myWidth + "%";
			}
		} else {
			msgString += " I am not a percentage width table";
		}
		//alert(msgString);
	}
}


function winOnLoad() {
  var ele = xGetElementById('leftColumn');
  if (ele && xDef(ele.style, ele.offsetHeight)) { // another compatibility check
    getTables();
	adjustLayout();
    xAddEventListener(window, 'resize', winOnResize, false);
  }
   
}
function winOnResize() {
	getTables();
	adjustLayout();
}
function adjustLayout() {
	// Get content heights and work out the bottom points of the three columns
	var headerHeight = xHeight('header');
	var splashHeight = xHeight('topContent');
	// If there's a sitemap
	var siteMapHeight = Math.max(xHeight('sitemapLeft'),xHeight('sitemapRight'));
//	var internalContent = xHeight('sbsdetail') + 80;
	
	var leftColumnBottom = xHeight('leftColumn') + headerHeight;
	var rightColumnBottom = xHeight('mainContentWrapper') + headerHeight + splashHeight + siteMapHeight + 60;
	var sideBarBottom = xHeight('sideBar') + xPageY('sideBar');

	// Figure out which one is bigger
	var farthestBottom = Math.max(sideBarBottom, Math.max(leftColumnBottom, rightColumnBottom));
	
	//Reposition the footer and show it 
	xMoveTo('footer',0,farthestBottom+1);
	xShow('footer');
	
	// Change the height of the left column
	var leftColumnTop = xPageY('leftColumn');
	var leftColumnHeight = farthestBottom-leftColumnTop;
	xHeight('leftColumn', leftColumnHeight);
}