
var tabletop, tablebottom, divheadref, headref, tableref;
var tablecount;
var stubref;


function initFloatingHeaders() {
	if ((ie5 && !is_mac) || ns7) {
		tabletop = new Array();
		tablebottom = new Array();
		divheadref = new Array();
		divstubref = new Array();
		divcornerref = new Array();
		headref = new Array();
		stubref = new Array();
		tableref = new Array();
		
		// find all the tables with ID of 'tableXX'
		var tablecollection = document.getElementsByTagName("TABLE");

		for (i=0; i<tablecollection.length; i++) {
			// if the table id begins with 'table'...
			if (tablecollection[i].id.indexOf('table') == 0) {
				tablecount = tablecollection[i].id.substr(5);
				// save references to the div, head, and table
				
				// these 3 are DIVs, not TABLE references
				divheadref[tablecount] = document.getElementById("divhead" + tablecount)	
				divstubref[tablecount] = document.getElementById("divstub" + tablecount)	
				divcornerref[tablecount] = document.getElementById("divcorner" + tablecount)	
								
				headref[tablecount] = document.getElementById("head" + tablecount)		
				stubref[tablecount] = document.getElementById("stub" + tablecount)	
				tableref[tablecount] = tablecollection[i];
				
				divheadref[tablecount].style.visibility = "visible";					
				divheadref[tablecount].style.left = tableref[tablecount].offsetLeft		
			}
		}

		windowResize();
	}
}

function windowResize() {
	var elem; 
	var corner;
	
	if ((ie5 && !is_mac) || ns7) {
		for(i=1; i<=tablecount; i++) {
			if(divheadref[i]) {
				headref[i].width = tableref[i].offsetWidth	// set the width of the column header table to the width of the data table
				tabletop[i] = tableref[i].offsetTop;
				tablebottom[i] = (tabletop[i] + tableref[i].offsetHeight) - divheadref[i].offsetHeight - tableref[i].rows[tableref[i].rows.length-1].offsetHeight;			
				divheadref[i].style.top = tabletop[i];
				
				elem = tableref[i].rows[0].cells[0];
				if (elem.currentStyle) {
					padding = parseInt(elem.currentStyle.paddingLeft) + 
						parseInt(elem.currentStyle.paddingRight) + 1;
				} else if (window.getComputedStyle) {
					var compStyle = window.getComputedStyle(elem, "");
					padding = parseInt(compStyle.getPropertyValue("padding-left")) + 
						parseInt(compStyle.getPropertyValue("padding-right")) + 1;			
				}
					
				// set the header table columns widths to the content table's column widths	
				for(j=0; j<headref[i].rows.length; j++) {
					for(k=0;k<headref[i].rows[j].cells.length;k++) {
						headref[i].rows[j].cells[k].width = tableref[i].rows[j].cells[k].offsetWidth-padding;
	
					}
				} // end for each row
				
				// set width of stub table to width of first column of table
				stubref[i].width = tableref[i].rows[0].cells[0].offsetWidth + 1;
				
				corner = document.getElementById("corner" + i)
				corner.width = tableref[i].rows[0].cells[0].offsetWidth + 1;
				corner.style.height = (tableref[i].rows[0].cells[0].offsetHeight + 1) + "px";
				
				// this assumes the header row is only one row high (no spanners)
				divstubref[i].style.top = tabletop[i] + tableref[i].rows[0].cells[0].offsetHeight;
				divstubref[i].style.left = tableref[i].offsetLeft;
				
				divcornerref[i].style.top = tabletop[i];
				divcornerref[i].style.left = tableref[i].offsetLeft;
				
				divstubref[i].style.zIndex = 4;
				divheadref[i].style.zIndex = 5;
				divcornerref[i].style.zIndex = 6;
			}	
		} // end for each table
	}
}

function windowScroll() {
	if ((ie5 && !is_mac) || ns7) {
		scrolltop = document.body.scrollTop;
		scrollleft = document.body.scrollLeft;
			
		for (i=1; i<=tablecount; i++) {
			if(divheadref[i]) {
				if (scrolltop > tablebottom[i]) {
					divheadref[i].style.top = tablebottom[i];
					divcornerref[i].style.top = tablebottom[i];
				} else if (scrolltop > tabletop[i]) {
					divheadref[i].style.top = scrolltop;
					divcornerref[i].style.top = scrolltop;
				} else {
					divheadref[i].style.top = tabletop[i];
					divcornerref[i].style.top = tabletop[i];
				}
					
				left = parseInt(divheadref[i].style.left);
				
				// window.status = (scrollleft > left) + ", " + scrollleft + ", " + left;
				if (scrollleft > left) {
					divstubref[i].style.visibility = divcornerref[i].style.visibility = "visible";
					divstubref[i].style.left = divcornerref[i].style.left = scrollleft;
				} else
					divstubref[i].style.visibility = divcornerref[i].style.visibility = "hidden";
			}
		}
	}
}
