//R. Andrew Lamonica
//Copyright 2005
//Interface Library for http://lamonica.info/

//Load Handler and Attach Command
AttachEvent(window, "load", InterfaceOnDocumentLoad);
function InterfaceOnDocumentLoad()
{

}

//Generic Event Handler Functions
function AttachEvent(element, eventName, handlerName)
{
	if(element.addEventListener)
		element.addEventListener(eventName, handlerName, false);
	else if(element.attachEvent)
		element.attachEvent("on"+eventName, handlerName);
	else
		alert("Event Handlers Not Supported !?!");
}

function DetachEvent(element, eventName, handlerName)
{
	if(element.removeEventListener)
		element.removeEventListener(eventName, handlerName, false);
	else if(element.detachEvent)
		element.detachEvent("on"+eventName, handlerName);
	else
		alert("Event Handlers Not Supported !?!");
}

//Interface for hide/unhide table rows
function CollapsibleCellEventHandler(e)
{
	if(!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;
	
	var parentOfCollapsedCell = target.CollapseInfo.currentCell.parentNode;
	
	var newCell = (target.CollapseInfo.isCollapsed ? 
		target.CollapseInfo.fullCell : 
		target.CollapseInfo.emptyCell);
	
	parentOfCollapsedCell.replaceChild(newCell, target.CollapseInfo.currentCell);
	target.CollapseInfo.currentCell = newCell;
	target.CollapseInfo.isCollapsed = !target.CollapseInfo.isCollapsed;
			
	if(e.preventDefault)
		e.preventDefault();
	return false;
}

function CreateCollapsibleTableCell(titleCell, cellToCollapse, startCollapsed)
{
	if(titleCell.CollapseInfo == null)
	{
		var collapsed = cellToCollapse.cloneNode(true);
		
		var theCurrentCell = cellToCollapse;
		var anEmptyCell = document.createElement("TD");
		anEmptyCell.style.display = "none";
		
		if(startCollapsed)
		{
			theCurrentCell = anEmptyCell;
			cellToCollapse.parentNode.replaceChild(theCurrentCell, cellToCollapse);
		}
		titleCell.CollapseInfo = 
		{
			fullCell: collapsed,
			emptyCell: anEmptyCell,
			currentCell: theCurrentCell,
			callback: CollapsibleCellEventHandler,
			isCollapsed: startCollapsed
		}
		AttachEvent(titleCell, "click", titleCell.CollapseInfo.callback);
	}
}



