/////////////////////////////////////////////////////////////////////////////
// Think of this function as the constructor for this class
////////////////////////////////////////////////////////////////////////////
function GWL_LeftHandNavigation(strShowHome, 
																strStartLevel, 
																strNumLevels,
																strArrowOnImageName,
																strArrowOffImageName,
																strArrowOnHighlightedImageName,
																strArrowOffHighlightedImageName,
																strSubArrowOnImageName,
																strSubArrowOffImageName,
																strSubArrowOnHighlightedImageName,
																strSubArrowOffHighlightedImageName,
																strImagesDirectory) {
	
	
	this.m_ShowHome   = false;
	
	this.m_NumLevels  = 10;
	this.m_EndLevel   = 11;
	
	this.m_ArrowOnImageName = strImagesDirectory +  strArrowOnImageName;
	this.m_ArrowOffImageName = strImagesDirectory +  strArrowOffImageName;
	this.m_ArrowOnHighlightedImageName = strImagesDirectory + strArrowOnHighlightedImageName;
	this.m_ArrowOffHighlightedImageName = strImagesDirectory + strArrowOffHighlightedImageName;
	
	this.m_SubArrowOnImageName = strImagesDirectory + strSubArrowOnImageName;
	this.m_SubArrowOffImageName = strImagesDirectory + strSubArrowOffImageName;
	this.m_SubArrowOnHighlightedImageName = strImagesDirectory + strSubArrowOnHighlightedImageName;
	this.m_SubArrowOffHighlightedImageName = strImagesDirectory + strSubArrowOffHighlightedImageName;
	
	this.m_ImagesDirectory = strImagesDirectory;
		
	this.m_NavPath    = g_navNode_Path;
			
	GWL_LeftHandNavigation.prototype.Display = GWL_LeftHandNavigation_Display;
	GWL_LeftHandNavigation.prototype.DisplayNode = GWL_LeftHandNavigation_displayNode;

	if (strShowHome == 'true') {
		this.m_ShowHome = true;
	}
		
	if (strStartLevel != '') {
		var value = parseInt(strStartLevel);
		if (value != NaN) {
			this.m_StartLevel = value;
		} else {
			this.m_StartLevel = 0;
		}
	}
	
	if (strNumLevels != '') {
		var value = parseInt(strNumLevels);
		if (value != NaN)	{
			this.m_NumLevels = value;
		}
	}

	this.m_EndLevel = this.m_StartLevel + this.m_NumLevels - 1 ;
		
}

/////////////////////////////////////////////////////////////////////////////
// Think of this as the public method
/////////////////////////////////////////////////////////////////////////////
function GWL_LeftHandNavigation_Display (node) {
	GWL_LeftHandNavigation_displayNode(node, true, true, this);
}


/////////////////////////////////////////////////////////////////////////////
// This function receives one node and produces one html row definition. Depending if this node is the
// first and/or last node within an array of nodes will determine if the beginning or ending html table
// definitions should appear.
/////////////////////////////////////////////////////////////////////////////
function GWL_LeftHandNavigation_displayNode(node, isFirstNode, isLastNode, obj) {

	var subNode = null;
	var parentNode = null;
	var startNodeLevel = 0;
	var isFirstSubNode = false;
	var isLastSubNode = false;
	var selectedNodes = obj.m_NavPath;
	var relNodeLevel = 0;
	var nodes = null;
	var startLevel = obj.m_StartLevel;
	var startLevelNodeID = null;
	var subNode = null;
	
	if ( startLevel <= selectedNodes.length - 1) {
		startLevelNodeID = selectedNodes[startLevel-1];	
	}
	
	if ( node.m_parent != null ) {
		parentNode = node.m_parent;
	}
	
	
	
	
	// Determines if the specified node should be shown, based upon if one of the following is true;
	// 1. The level of the node (0 = Home Page) is >= the level specified by the user AND 
	//    the node is selected.
	//		(This condition is used to display section nodes)
	//
	// 2. The node has a parent AND the parent's node id is the last selected node in the tree.
	//    (This condition is used to display document nodes)
	//
	// 3. The level of the node (0 = Home Page) is >= the level specified by the user AND 
	//    the parent's node id is selected.
	//    (This condition is used to display subsection nodes)
	
	if ( 	( node.m_level >= startLevel && nodeSelected(node, selectedNodes) )
		||  ( parentNode != null && parentNode.m_id == startLevelNodeID ) 
		||  ( node.m_level >= startLevel && nodeSelected(parentNode, selectedNodes ) ) ) { 
		
		relNodeLevel = node.m_level - startLevel + 1;
		
		if ( isFirstNode ) {
			document.write(tableStartDefinition(relNodeLevel, obj));
		}
		
		var ds = new Array();
		var di = 0;
		var isSelected = false;
		var isHighlighted = false;
		var subNode = null;
		var nodeClassNameOn = null;
		var nodeClassNameOff = null;
		var nodeImageOn = null;
		var nodeImageOff = null;
		var nodeImageOnHighlighted = null;
		var nodeImageOffHighlighted = null;
		var nodeImageCellWidth = 0;
		var nodeImageOnWidth =   0;
		var nodeImageOnHeight =  0;
		var nodeImageOnVSpace =  0;
		var nodeImageOffWidth =  0;
		var nodeImageOffHeight = 0;
		var nodeImageOffVSpace = 0;
				
				
		if ( relNodeLevel == 1 ) {
			nodeClassNameOn = "levelOneOn";
			nodeClassNameOff = "levelOne";
			nodeImageOn = obj.m_ArrowOnImageName;
			nodeImageOff = obj.m_ArrowOffImageName;
			nodeImageOnHighlighted = obj.m_ArrowOnHighlightedImageName;
			nodeImageOffHighlighted = obj.m_ArrowOffHighlightedImageName;
			nodeImageCellWidth = 7;
			nodeImageOnWidth =   7;
			nodeImageOnHeight =  4;
			nodeImageOnVSpace =  6;
			nodeImageOffWidth =  4;
			nodeImageOffHeight = 7;
			nodeImageOffVSpace = 4;
		} else {
			nodeClassNameOn = "levelTwoOn";
			nodeClassNameOff = "levelTwo";
			nodeImageOn = obj.m_SubArrowOnImageName;
			nodeImageOff = obj.m_SubArrowOffImageName;
			nodeImageOnHighlighted = obj.m_SubArrowOnHighlightedImageName;
			nodeImageOffHighlighted = obj.m_SubArrowOffHighlightedImageName;
			nodeImageCellWidth = 6;
			nodeImageOnWidth = 	 6;
			nodeImageOnHeight =	 3;
			nodeImageOnVSpace =  6;
			nodeImageOffWidth =  3;
			nodeImageOffHeight = 5;
			nodeImageOffVSpace = 4;
			
			
		}
			
		isSelected = nodeSelected(node, selectedNodes);
		isHighlighted = nodeHighlighted(node, selectedNodes);
		isNodeHasChildren = nodeHasChildren(node);
			
			
			
			
		ds[di++] = 			'<tr valign="top">';
		ds[di++] = 			'<td width=\"3\"></td>';
		
		
		// Write the cell html that contains the image (the arrows)
		ds[di++] = 			'	<td width=\"' + nodeImageCellWidth + '\">';
		
		if (isSelected) {
			if (isHighlighted) {
				if (isNodeHasChildren) {
					ds[di++] = 		'		<img src=\"' + nodeImageOnHighlighted + '\" alt=\"\" width=\"' + nodeImageOnWidth + '\" height=\"' + nodeImageOnHeight + '\" vspace=\"' + nodeImageOnVSpace + '\">';
				} else {
					ds[di++] = 		'		<img src=\"' + nodeImageOffHighlighted + '\" alt=\"\" width=\"' + nodeImageOffWidth + '\" height=\"' + nodeImageOffHeight + '\" vspace=\"' + nodeImageOffVSpace + '\">';
				}
			} else {
				ds[di++] = 		'		<img src=\"' + nodeImageOn + '\" alt=\"\" width=\"' + nodeImageOnWidth + '\" height=\"' + nodeImageOnHeight + '\" vspace=\"' + nodeImageOnVSpace + '\">';
			}
		} else {
			ds[di++] = 		'		<img src=\"' + nodeImageOff + '\" alt=\"\" width=\"' + nodeImageOffWidth + '\" height=\"' + nodeImageOffHeight + '\" vspace=\"' + nodeImageOffVSpace + '\">';
		}
		
		ds[di++] = 			'	</td>';
		ds[di++] = 			'<td width=\"3\"></td>';
		
		
		
		// Write the cell html that contains the href link
		ds[di++] = 			'	<td>';
			
		if (isHighlighted) {
			ds[di++] = 			'		<a href=\"' + node.m_href + '\" id=\"' + nodeClassNameOn + '\">' + node.m_label + '</a>';
		} else {
			ds[di++] = 			'		<a href=\"' + node.m_href + '\" id=\"' + nodeClassNameOff + '\">' + node.m_label + '</a>';
		}
		
		
		
	
		
		document.write(ds.join(''));
		
		
		// If the current node has subnodes, drill down each node
		isFirstSubNode = true;
		
		for (var i = 0; i < node.m_subNodes.length; i++) {
			isLastSubNode = (i == node.m_subNodes.length - 1);
			subNode = node.m_subNodes[i];
			GWL_LeftHandNavigation_displayNode(subNode, isFirstSubNode, isLastSubNode, obj);
			isFirstSubNode = false;
		}
		
		
		// Write the closing row html
		ds = new Array();
		di = 0;
		ds[di++] = 		'	</td>';
		ds[di++] = 		'</tr>';
		
		if ( !nodeSelected(node, selectedNodes) ) {
			ds[di++] = 		'<tr><td height="3"></td></tr>';
		}
		
		document.write(ds.join(''));
	
	
		// If this is the last node, write the ending table definition html
		if ( isLastNode ) {
			document.write(tableEndDefinition(relNodeLevel, obj));
		}	

	// If the current node has subnodes, drill down each node
	} else {
	
		isFirstSubNode = true;
		for (var i = 0; i < node.m_subNodes.length; i++) {
			isLastSubNode = (i == node.m_subNodes.length - 1);
			subNode = node.m_subNodes[i];
			GWL_LeftHandNavigation_displayNode(subNode, isFirstSubNode, isLastSubNode, obj);
			isFirstSubNode = false;
		}
	}
}


/////////////////////////////////////////////////////////////////////////////
// This function creates html that builds the starting table definition
/////////////////////////////////////////////////////////////////////////////
function tableStartDefinition(relNodeLevel, obj) {

	var ds = new Array();
	var di = 0;
	
	if ( relNodeLevel == 1 ) {
		ds[di++] = '<table width=\"175\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">';
		ds[di++] = '<tr valign="top">';
		ds[di++] = '<td class=\"keyline\">';
		ds[di++] = '<img src=\"' + obj.m_ImagesDirectory + 'S4_002218.gif' + '\" alt=\"\" width=\"10\" height=\"1\">';
		ds[di++] = '</td>';
		ds[di++] = '</tr>';
		ds[di++] = '<tr valign=\"top\">';
		ds[di++] = '<td class=\"bodyHighlight\">';
		ds[di++] = '<table width=\"175\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">';
		ds[di++] = '<tr><td height=\"3\"></td></tr>';
		
	} else if ( relNodeLevel == 2 ) {
		ds[di++] = '<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">';
		ds[di++] = '<tr><td height=\"3\"></td></tr>';
	} else if ( relNodeLevel == 3 ) {
		ds[di++] = '<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">';
		ds[di++] = '<tr><td height=\"3\"></td></tr>';
	} else if ( relNodeLevel == 4 ) {
		ds[di++] = '<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">';
		ds[di++] = '<tr><td height=\"3\"></td></tr>';
	} else if ( relNodeLevel == 5 ) {
		ds[di++] = '<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">';
		ds[di++] = '<tr><td height=\"3\"></td></tr>';
	} else if ( relNodeLevel == 6 ) {
		ds[di++] = '<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">';
		ds[di++] = '<tr><td height=\"3\"></td></tr>';
	} else if ( relNodeLevel == 7 ) {
		ds[di++] = '<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">';
		ds[di++] = '<tr><td height=\"3\"></td></tr>';
	} else {
		ds[di++] = '<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">';
		ds[di++] = '<tr><td height=\"3\"></td></tr>';
	}
	return ds.join('');
}

/////////////////////////////////////////////////////////////////////////////
// This function creates html that builds the ending table definition
/////////////////////////////////////////////////////////////////////////////
function tableEndDefinition(relNodeLevel, obj) {

	var ds = new Array();
	var di = 0;
	
	if ( relNodeLevel == 1 ) {
		ds[di++] = '</table>';
		ds[di++] = '</td>';
		ds[di++] = '</tr>';
		ds[di++] = '<tr>';
		ds[di++] = '<td class=\"keyline\">';
		ds[di++] = '<img src=\"' + obj.m_ImagesDirectory + 'S4_002218.gif' + '\" alt=\"\" width=\"10\" height=\"1\">';
		ds[di++] = '</td>';
		ds[di++] = '</tr>';	
		ds[di++] = '</table>';
	} else if ( relNodeLevel == 2 ) {
		ds[di++] = '</table>';
	} else {
		ds[di++] = '</table>';
	}
	return ds.join('');
}

/////////////////////////////////////////////////////////////////////////////
// This function determines if the specified node is selected. The term selected
// means that a node is expanded and is displaying its subnodes or is the current
// node that the user selected. This is determined by the array called selectedNodes.
// The selectedNodes array is a copy of the g_navNode_Path, which is automatically
// generated by the application. If the id value for the specified node is contained
// in the array, then it is considered to be selected.
/////////////////////////////////////////////////////////////////////////////
function nodeSelected(node, selectedNodes){
	
	if ( node == null ) {
		return false;
	}
	
	for (var i = 0; i < selectedNodes.length; i++) {
		if ( (node.m_id == selectedNodes[i]) || nodeHighlighted(node, selectedNodes) ) {
			return true;
		}
	}
	return false;
}



/////////////////////////////////////////////////////////////////////////////
// This function determines if the specified node is the node that the user 
// is currently viewing. 
//
// For sections, the parameter "&nodeId" will be in the url with a value
// of the section id that the user is viewing. 
//
// For documents, the parameter "&ssDocName" will be in the url with a value 
// of the contentID that corresponds to the document the user is viewing. 
/////////////////////////////////////////////////////////////////////////////
function nodeHighlighted(node, selectedNodes){
	
	var selectedNode = 0;
	var parentNode = null;
	var lastSelectedNode = null;
	
	if ( node == null ) {
		return false;
	}
	
	if ( selectedNodes == null ) {
			return false;
	}

	if (!g_ssIsSecondaryPage) {
		selectedNode = g_ssNodeId;
	}
	else {
		parentNode = node.m_parent;
		lastSelectedNode = g_ssNodeId;
		
		// This check is here in case a one document appears in two sections that 
		// are expanded and visible. This will ensure that only the document in the
		// last expanded section will be highlighted.
		if ( parentNode.m_id == lastSelectedNode ) {
			selectedNode = g_ssDocName;
		}
	}
	
	if ( node.m_id == selectedNode ) {
		return true;
	}
	return false;
}

function nodeHasChildren(node) {
	
	return (node.m_subNodes.length > 0);

}