// 
//	TYGR Productions Interactive Statistics
//	
//	This file contains JavaScript support functions for TYGR Productions' Interactive Statistics.  The code contained herein
//  is NOT in the public domain.  This file is provided for download to personal computers and is only authorized for personal 
//  use in conjuction with TYGR Productions' Interactive Statistics.  Copying or reposting this file in whole or in part on 
//  any web server or other network server, or reusing any portion of the code is not allowed without express written consent 
//  of the author.  Any commercial use is strictly prohibited.
//	
//	Copyright (c) 2005, 2006 by Jon T. Camp.  All rights reserved.
// 
function buildHeader(tableData) 
{
	for (var i = 0; i < tableData.DataColCount; i++) 
	{
		var nodeElement = tableData.Node.getElementsByTagName(tableData.Elements[i]).item(0);
		tableData.IDs[i] = nodeElement.attributes.getNamedItem("id").value;
		tableData.IDHeaderRows[i] = dataColToTableHeaderRow(tableData, i);
		tableData.IDHeaderRowSpans[i] = dataColToTableHeaderRowSpan(tableData, i);
		tableData.Captions[i] = nodeElement.attributes.getNamedItem("caption").value;
		tableData.ColWidths[i] = nodeElement.attributes.getNamedItem("colWidth").value;
		tableData.Status[i] = nodeElement.attributes.getNamedItem("status").value;
		tableData.LeftAlign[i] = Number(nodeElement.attributes.getNamedItem("leftAlign").value);
		tableData.Descending[i] = Number(nodeElement.attributes.getNamedItem("descending").value);
		tableData.ByValue[i] = Number(nodeElement.attributes.getNamedItem("byValue").value);
	}

	var tr;
	var th;
	var nSubRow;
	var nSubRowStart;
	var nSubRowStop;
	
	if (bDefaultFormat) 
	{
		tr = appendRow(tableData.Table.tHead);
		th = appendHeaderCell(tr, tableData.Caption.toUpperCase(), "th2")
		th.colSpan = tableData.TableColCount;
	
		// special case
		nSubRowStart = 1;
		nSubRowStop = (tableData.Table.id == "tableTotalId" ? 2 : 1);
		
		for (nSubRow = nSubRowStart; nSubRow <= nSubRowStop; nSubRow++)
		{
			tr = appendRow(tableData.Table.tHead);
			
			for (var i = 0; i < tableData.DataColCount; i++) 
			{
				if (tableData.IDHeaderRows[i] == nSubRow)
				{
					if (tableData.LeftAlign[i]) th = appendHeaderCell(tr, tableData.Captions[i], "th1")
					else th = appendHeaderCellHTML(tr, tableData.Captions[i]);
					
					th.rowSpan = tableData.IDHeaderRowSpans[i];
					setClickHandler(th, cellClick);
					setEnterHandler(th, cellEnter);
					setLeaveHandler(th, cellLeave);
					setMoveHandler(th, cellMove);
				}
				
				// special case
				if ((tableData.Table.id == "tableTotalId") && (nSubRow == nSubRowStart) && (i == 0))
				{
					th = appendHeaderCell(tr, "Offense", "th2");
					th.colSpan = 3;
					th = appendHeaderCell(tr, "Defense", "th2");
					th.colSpan = 3;
				}	
			}
		}
		
		tr = appendRow(tableData.Table.tBodies[1]);
		appendHeaderCell(tr, "Ranked:", "th1a");
		tableData.Rank[0] = false;
		
		for (var i = 1; i < tableData.DataColCount - 1; i++) 
		{
			th = appendHeaderCell(tr, "", "th1b");
			setClickHandler(th, cellRankClick);
			setRank(tableData, i, true);
		}
		
		appendHeaderCell(tr, "", "th1a");
		setRank(tableData, tableData.DataColCount - 1, false);
	}
	else 
	{
		tr = appendRow(tableData.Table.tHead, "border", 1);
		th = appendHeaderCell(tr, "", "border");
		th.colSpan = tableData.TableColCount;
	
		tr = appendRow(tableData.Table.tHead);
		appendHeaderCell(tr, "", "border", 1);
		appendHeaderCell(tr, tableData.Caption.toUpperCase(), "th2", tableData.Table.width - 2,
					tableData.TableColCount - 2);
		appendHeaderCell(tr, "", "border", 1);
	
		tr = appendRow(tableData.Table.tHead, null, 1);
		appendHeaderCell(tr, "", "border", 1);
		th = appendHeaderCell(tr, "", "hborder");
		th.colSpan = tableData.TableColCount - 2;
		appendHeaderCell(tr, "", "border", 1);
	
		// special case
		nSubRowStart = 3;
		nSubRowStop = (tableData.Table.id == "tableTotalId" ? 5 : 3);
	
		for (nSubRow = nSubRowStart; nSubRow <= nSubRowStop; nSubRow += 2)
		{
			tr = appendRow(tableData.Table.tHead);
			if (nSubRow == nSubRowStart) appendHeaderCell(tr, "", "border", 1, null, tableData.IDHeaderRowSpans[0]);
			
			for (var i = 0; i < tableData.DataColCount; i++) 
			{
				if (tableData.IDHeaderRows[i] == nSubRow)
				{
					if (tableData.LeftAlign[i]) th = appendHeaderCellHTML(tr, "&nbsp;" + tableData.Captions[i], "th1")
					else th = appendHeaderCellHTML(tr, tableData.Captions[i] + "&nbsp;");
					
					if (tableData.ColWidths[i] > 0) th.width = tableData.ColWidths[i];
					th.rowSpan = tableData.IDHeaderRowSpans[i];
					setClickHandler(th, cellClick);
					setEnterHandler(th, cellEnter);
					setLeaveHandler(th, cellLeave);
					setMoveHandler(th, cellMove);
					
					// special case
					if (tableData.Table.id == "tableTotalId")
					{
						if ((nSubRow == nSubRowStop) && (i < 6)) appendHeaderCell(tr, "", "hborder", 1, null, tableData.IDHeaderRowSpans[i]);
						else if ((nSubRow != nSubRowStop) && (i < tableData.DataColCount - 1)) appendHeaderCell(tr, "", "hborder", 1, null, tableData.IDHeaderRowSpans[i]);
					}
					else if (i < tableData.DataColCount - 1) appendHeaderCell(tr, "", "hborder", 1, null, tableData.IDHeaderRowSpans[i]);
				}

				// special case
				if ((tableData.Table.id == "tableTotalId") && (nSubRow == nSubRowStart) && (i == 0))
				{
					th = appendHeaderCell(tr, "Offense", "th2");
					th.colSpan = 5;
					appendHeaderCell(tr, "", "hborder", 1);
					th = appendHeaderCell(tr, "Defense", "th2");
					th.colSpan = 5;
					appendHeaderCell(tr, "", "hborder", 1, null, tableData.IDHeaderRowSpans[i]);
				}	
				if ((tableData.Table.id == "tableTotalId") && (nSubRow == nSubRowStart) && (i == tableData.DataColCount - 1))
				{
					appendHeaderCell(tr, "", "border", 1, null, tableData.IDHeaderRowSpans[i]);	
					tr = appendRow(tableData.Table.tHead, null, 1);
					th = appendHeaderCell(tr, "", "hborder");
					th.colSpan = 11;
				}
			}
		}

		// special case
		if (tableData.Table.id != "tableTotalId") appendHeaderCell(tr, "", "border", 1);	

		tr = appendRow(tableData.Table.tBodies[1]);
		appendHeaderCell(tr, "", "border", 1);	
		appendHeaderCellHTML(tr, "&nbsp;Ranked:", "th1a");
		tableData.Rank[0] = false;
		appendHeaderCell(tr, "", "hborder", 1);
		
		for (var i = 1; i < tableData.DataColCount - 1; i++) 
		{
			th = appendHeaderCell(tr, "", "th1b");
			setClickHandler(th, cellRankClick);
			setRank(tableData, i, true);
			appendHeaderCell(tr, "", "hborder", 1);
		}
		
		appendHeaderCell(tr, "", "th1a");
		setRank(tableData, tableData.DataColCount - 1, false);
		appendHeaderCell(tr, "", "border", 1);
	}
		
	if (tableData.Table.id == "tableTotalId") setRank(tableData, tableData.DataColCount - 2, false);
}
