var treeNodes = new Array();

/***************************************************************************\
* Function:     detatchLiveChat                                             *
* Author:       Terry Tice                                                  *
* Created:      09/25/07                                                    *
* Purpose:      This will detatch the chat window.                          *
\***************************************************************************/
function detachLiveChat()
{
    window.open(
        "/statusmonitor/site/index.aspx",
        "ChatWindow",
        "height = 500, width = 400, location = no, menubar = no, scrollbars = no, " +
        "status = no, toolbar = no, resizable = yes"
    );
}

/***************************************************************************\
* Function:		initMenus													*
* Author:		Terry Tice													*
* Created:		09/21/07													*
* Purpose:		This function will loop through all divs with the class		*
*				"treeNode" and make them actual tree nodes.					*
\***************************************************************************/
function initMenus()
{
	var divElements;
	
	// Grab a list of all the div elements on the page.
	divElements = document.getElementsByTagName('div');
	
	// Loop through all the div elements.
	for (var i = 0; i < divElements.length; i++)
	{
		// Find all the tree nodes that have a parent with class 'leftNavigation'
		if (divElements[i].className == 'treeNode')
	    {
    	    // Find the title of the tree node, which should be in a 'u' tag.
			// Also, make the image clickable.
			for (var j = 0; j < divElements[i].childNodes.length; j++)
			{
				if (divElements[i].childNodes[j].tagName == 'U' ||
				    divElements[i].childNodes[j].tagName == 'IMG')
			        divElements[i].childNodes[j].onclick = titleClick;
			}
    				

			// If the node is set to non collapsable then don't allow collapsing.
			if(divElements[i].getAttributeNode('collapsable') == null ||
				divElements[i].getAttributeNode('collapsable').value != 'false')
			{
    			if (divElements[i].parentNode.className == 'leftNavigation')
    		    {
    				divElements[i].onclick = nodeClick;
    		    }
    		    
    			treeNodes.push(divElements[i]);
    		}
		}
	}
}

/***************************************************************************\
* Function:		nodeClick													*
* Author:		Terry Tice													*
* Created:		09/21/07													*
* Purpose:		This function is called when one of the sidebar nodes is	*
*				clicked.													*
\***************************************************************************/
function nodeClick(e)
{
	var treeNode;
	
	// If no arguments were passed then grab the event arguments from
	// window.event
	if (e == null)
		var e = window.event;
				
	// Grab the tree node that was clicked
	treeNode = e.srcElement || e.target;
	
	collapseAllNodes(treeNode);
		
	toggleNode(treeNode);
}

/***************************************************************************\
* Function:		titleClick													*
* Author:		Terry Tice													*
* Created:		10/01/07													*
* Purpose:		This function is called when a tree title is clicked.       *
\***************************************************************************/
function titleClick(e)
{
    var treeNode;
	
	// If no arguments were passed then grab the event arguments from
	// window.event
	if (e == null)
		var e = window.event;
				
	// Grab the tree node that was clicked
	treeNode = (e.srcElement || e.target).parentNode;
	
    collapseAllNodes(treeNode);
		
	toggleNode(treeNode);
}

/***************************************************************************\
* Function:		toggleNode													*
* Author:		Terry Tice													*
* Created:		09/21/07													*
* Purpose:		This function will toggle a tree node's collapsed state.	*
\***************************************************************************/
function toggleNode(treeNode)
{
	var treeChildren;
	var treeNodeCollapsed;
	var treeNodeCollapsable;
	var treeNodeDestination;
	
	// Grab the children of the tree
	treeChildren = treeNode.childNodes;
	
	// If there are no children then don't do anything
	if (treeChildren == null)
		return;
	
	try { treeNodeCollapsed = treeNode.getAttributeNode('collapsed');       } catch (e) { return; }
	try { treeNodeCollapsable = treeNode.getAttributeNode('collapsable');   } catch (e) { return; }
	
	// If there are no properties of treeNodeCollapsed, just exit
	if (treeNodeCollapsed == null || (treeNodeCollapsable != null && treeNodeCollapsable.value == 'false')) return;
	
	treeNodeCollapsed.value = treeNodeCollapsed.value == 'true' ? 'false' : 'true';
	
	treeNodeDestination = getDestination(treeNode);
	
	if (treeNodeDestination != null && treeNodeCollapsed.value == 'false')
	{
	    location.href = treeNodeDestination;
	    return;
    }
	
	// Grab the child div elements.
	for (var i = 0; i < treeChildren.length; i++)
	{
		if (treeNode.childNodes[i].className == 'treeChildren')
		{
			// Decide whether to display or hide it.
			if (treeNodeCollapsed.value == 'true')
				treeNode.childNodes[i].style.display = 'none';
			else
				treeNode.childNodes[i].style.display = 'block';
		}
		else if (treeNode.childNodes[i].nodeName == 'IMG')
		{
			if (treeNodeCollapsed.value == 'true')
				treeNode.childNodes[i].src = '/images/right-arrow2.gif';
			else
				treeNode.childNodes[i].src = '/images/down-arrow2.gif'
		}
	}
}

/***************************************************************************\
* Function:		collapseAllNodes											*
* Author:		Terry Tice													*
* Created:		09/21/07													*
* Purpose:		This function collapses all open tree nodes					*
\***************************************************************************/
function collapseAllNodes(treeNode)
{
    var nodeDepth;
    
    nodeDepth = getDepth(treeNode);
    
	for (var i = 0; i < treeNodes.length; i++)
	{
		if (treeNodes[i].getAttributeNode('collapsed').value == 'false' &&
			treeNodes[i] != treeNode && getDepth(treeNodes[i]) == nodeDepth)
			
			toggleNode(treeNodes[i]);
	}
}

/***************************************************************************\
* Function:		getDepth        											*
* Author:		Terry Tice													*
* Created:		10/09/07													*
* Purpose:		This function determins the tree node depth for collapsing. *
\***************************************************************************/
function getDepth(treeNode)
{
    try { 
        return treeNode.getAttributeNode('depth').value;
    } 
    catch (e) 
    { 
        return 0; 
    }
}

/***************************************************************************\
* Function:		getDestination    											*
* Author:		Terry Tice													*
* Created:		10/09/07													*
* Purpose:		This function determines the detination of a node that is   *
*               not expandable.                                             *
\***************************************************************************/
function getDestination(treeNode)
{
    try { 
        return treeNode.getAttributeNode('destination').value;
    } 
    catch (e) 
    { 
        return null; 
    }
}