
function statusCheck(item, tocall)
{

	//change style
	if(item.className=="up")
	{
	item.className="down";
	tocall(item.firstChild.nodeValue, "table-row;");
	} else {
	item.className="up";
	tocall(item.firstChild.nodeValue, "none;");
	}
	
}

function changeStatus(status, newDisp)
{


var items = document.evaluate("//table[@id='rostertable']/tbody/tr/td[@class='status']", 
	document, null, 6, null);
	var item, cnode, cbutton, trnode;
	for(var i = 0;i<items.snapshotLength;i++)
	{
		item = items.snapshotItem(i);
		if(status == item.firstChild.nodeValue)
		{	
			//is the class down?
			cnode = item.parentNode.childNodes[2];
			cbutton = document.getElementById('cl'+cnode.firstChild.nodeValue);
			if(cbutton.className == "down")
			{
				trnode = item.parentNode;
				trnode.style.display=newDisp;	
			}				
		}
	}
}

function changeClass(status, newDisp)
{
var items = document.evaluate("//table[@id='rostertable']/tbody/tr/td[@class='pclass']", 
	document, null, 6, null);
	for(var i = 0;i<items.snapshotLength;i++)
	{
		item = items.snapshotItem(i);
	
		if(status == item.firstChild.nodeValue)
		{	
			//is the status down?
			cnode = item.parentNode.childNodes[6];
			
			cbutton = document.getElementById('st'+cnode.firstChild.nodeValue);
			//DEBUG
			if(cbutton == null)
			{
				alert(cnode.firstChild.nodeValue);
			}
			
			if(cbutton.className == "down")
			{
				trnode = item.parentNode;
				trnode.style.display=newDisp;	
			}				
		}		
	}
}

function flipClass(caller)
{
caller.className="down";
var items = document.evaluate("//ul[@id='pclass']/li[@id[starts-with(., 'cl')]]",
document, null, 6, null);
var item;
//alert(items.snapshotLength);
for(var i=0;i<items.snapshotLength;i++)
{

	item = items.snapshotItem(i);
	statusCheck(item, changeClass);
}

caller.className="up";
}

function resizeHead(mod)
{
	var head = document.getElementById("header");
	var h;
	if(head.style.height == null || head.style.height == "")
	{
		h = 200;
	} else {
	 h = parseInt(head.style.height.substring(0, head.style.height.length-2));
	}
	if(h == null || h == NaN)
	{
		h = 150;
	}
	var newHeight = h+50*mod;
	head.style.height = newHeight;
	if(newHeight < 50)
		head.style.height = 50;
	
}

function toggleHead(caller)
{
	if(caller.className == "up")
	{
		caller.className="down";
		setHead("block");
	} else {
		caller.className="up";
		setHead("none");
	}
}

function setHead(display)
{
	document.getElementById("header").style.display=display;
	if(display == 'none')
	{
		document.getElementById('feStats').className="up";
	}
}

function changeSummary(caller, doSummary)
{
	var items = document.evaluate("//div[@id='header']/div[@id='sumControls']/div[@id[starts-with(., 'sum')]]",
	document, null, 6, null);
	var item;
	
	for(var i=0;i<items.snapshotLength;i++)
	{
		item = items.snapshotItem(i);
		if(item.id != caller.id)
		{
			item.className="headcontrol up";
		}
		else
		{
			item.className="headcontrol down";
		}
	}
	doSummary();
}

function toggleCollapse(lName)
{
	var items = document.evaluate("//ul[@id='"+lName+"']/li",
	document, null, 6, null);
	var item;
	var newDisp = "none";
	if(items.snapshotItem(0).style.display == "none")
	{
		newDisp = "block";
	}
	
	for(var i=0;i<items.snapshotLength;i++)
	{
		item = items.snapshotItem(i);
		item.style.display=newDisp;
	}
}

function sumByClass()
{
	sumClear();
	
	//create the by-class summary data
	var sumTable = document.createElement("table");
	document.getElementById("header").appendChild(sumTable);
	//document.body.appendChild(sumTable);
	sumTable.id="statTable";
	sumTable.className="sortable";
	sumTable.style.width="90%";
	var row = sumTable.insertRow(0);
	
	//header elements
	var cell = document.createElement("th");
	row.appendChild(cell);	
	cell.appendChild(document.createTextNode("Class"));
	
	cell = document.createElement("th");
	row.appendChild(cell);
	cell.appendChild(document.createTextNode("Members"));
	
	cell = document.createElement("th");
	row.appendChild(cell);
	cell.appendChild(document.createTextNode("Total Points"));
	
	cell = document.createElement("th");
	row.appendChild(cell);
	cell.appendChild(document.createTextNode("Avg Points"));
	//end header
	
	//gather up some data yes?
	var data = gatherClassData();
	var entry;
	
	for(entry in data)
	{
		row = sumTable.insertRow(sumTable.rows.length);
		
		//class name
		cell = row.insertCell(0);
		cell.appendChild(document.createTextNode(data[entry].name));
		cell = row.insertCell(1);
		cell.appendChild(document.createTextNode(data[entry].count));
		cell = row.insertCell(2);
		cell.appendChild(document.createTextNode(Math.round(data[entry].points)));
		cell = row.insertCell(3);
		cell.appendChild(document.createTextNode(Math.round(data[entry].points/data[entry].count)));
		
	}	
	ts_makeSortable(sumTable);// <-- making this work is hard... a href in a floating div bad?
}

function gatherClassData()
{
	var items = document.evaluate("//table[@id='rostertable']/tbody/tr",
	document, null, 6, null);
	var item;
	var itemClass;
	var data = new Object;
	//alert(items.snapshotLength);
	
	for(var i=0;i<items.snapshotLength;i++)
	{
		
		item = items.snapshotItem(i);
		if(item.style.display != 'none')
		{
			itemClass = item.childNodes[2].firstChild.nodeValue;
			if(itemClass == null)
				continue;
			if(data[itemClass] == null)
			{
				
				data[itemClass] = new Object;
				data[itemClass].count = 0;
				data[itemClass].points = 0;
				data[itemClass].name = itemClass;
			}
			data[itemClass].count += 1;	
			data[itemClass].points += (parseFloat(item.childNodes[1].firstChild.firstChild.nodeValue));
		}			
	}
		
	return data;
}

function sumClear()
{
	var table = document.getElementById("statTable");
	if(table != null)
	{
	var parent = table.parentNode;
	parent.removeChild(table);
	}
}
