/////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2007, 2008, Oracle. All rights reserved.
// Function : ls_tree_navigation
// Comments : 
/////////////////////////////////////////////////////////////////////////////


function ls_tree_navigation (strInstanceName, strCgiPath, strAssetsPath, strShowHome, strStartingNodeId, strEndNodeId) 
{
	this.m_InstanceName 	= strInstanceName;
	this.m_CgiPath		= strCgiPath;
	this.m_ShowHome	    	= false;
	
	if (strShowHome != null && strShowHome == 'true')
		this.m_ShowHome = true;
	
	this.m_AssetsPath	= strAssetsPath;


	this.m_NavPath      = g_navNode_Path;

	this.m_StartingNodeId = strStartingNodeId;

	if (strEndNodeId == '')
		this.m_EndNodeId = -1;
	else
		this.m_EndNodeId = strEndNodeId;
	

	if (this.m_StartingNodeId == '')
		this.m_StartDisplayingNodes = true;
	else	
		this.m_StartDisplayingNodes = false;


	this.m_ds = new Array();
	this.m_di = 0;

	ls_tree_navigation.prototype.Display = ls_tree_navigation_Display;
	ls_tree_navigation.prototype.DisplayNode = ls_tree_navigation_DisplayNode;
	
	ls_tree_navigation.prototype.Display = ls_tree_navigation_Display;

	ls_tree_navigation.prototype.addHtmlForChild = ls_tree_navigation_addHtmlForChild;
	ls_tree_navigation.prototype.hasNextSiblingElement = ls_tree_navigation_hasNextSiblingElement;
	ls_tree_navigation.prototype.hasPreviousSiblingElement = ls_tree_navigation_hasPreviousSiblingElement;

}

// ========================= document generation code ====================
 
function ls_tree_navigation_Display (node)
{
	/* Call the function that builds the navigation code. Note: this function contains
		a recursive looping mechanism that causes it to drill down through all the clildren
		beneath the passed-in element; this process builds the html code by appending markup
		for each level to the m_ds[m_di++] variable. */
	this.m_ds[this.m_di++] = '<div id="accordeon_menu">';
	this.DisplayNode(node);
	this.m_ds[this.m_di++] = '</div>';		
	document.write(this.m_ds.join(''));	
	//alert(this.m_ds.join(''));

}

function ls_tree_navigation_DisplayNode(parentElement)
{
	/* Create a variable containing all children of the passed-in element. */
	if(parentElement.m_id != this.m_EndNodeId){
 	if (!this.m_StartDisplayingNodes && parentElement.m_id == this.m_StartingNodeId)
		this.m_StartDisplayingNodes = true;
		
	var childList = parentElement.m_subNodes;
	
	/* Generate the opening <div> tag for the current collection. As it's currently designed,
		all the collections are closed (e.g. only the top level collections are displayed initially. */	

	var bExpand = false;
			
	if (this.m_NavPath[parentElement.m_level] == parentElement.m_id)
	{
		bExpand = true;
	}
	
	if (this.m_StartDisplayingNodes){
		if (bExpand || parentElement.m_level == 0)
		{
			this.m_ds[this.m_di++] = '<ul class="level' + parentElement.m_level + '">' + '\n';
		}
		else
		{
			this.m_ds[this.m_di++] = '<ul class="level' + parentElement.m_level + '">' + '\n';
		}

		if (parentElement.m_level == 0 && this.m_ShowHome)
			this.addHtmlForChild(parentElement);
	}
	/* Loop through the children of the passed-in element. For each child generate the html code
		for that child, and if that child has children of its own, recursively call the function.
		Note: since the standards-based browsers don't ignore whitespace the nodeType value of 
		each node must be checked. */
		
	for (var i=0; i<childList.length; i++)
	{
		var childNode = childList[i];
		if (this.m_StartDisplayingNodes && childNode.m_id != this.m_EndNodeId){
			this.addHtmlForChild(childNode);
		}
		if (childNode.m_subNodes.length > 0)
		{
			if(childNode.m_id != this.m_EndNodeId){
				this.DisplayNode(childNode);
			}
			else{
				this.m_StartDisplayingNodes = false;
				return;
			}
		}
	}
	
	/* Generate closing <div> tag code for current element. */
	if (this.m_StartDisplayingNodes)
		this.m_ds[this.m_di++] = '</ul>\n';	
 }
 else{
	return;
 }
}

/* This function is called by the createChildrenForCollection function above. It
	generates the html table code for a single display item on the nav tree. */
function ls_tree_navigation_addHtmlForChild(childElement)
{
	var labelTextHtml = "";		
	var labelTextHtmlChild = "";
	var labelTextHtmlParent = "";

	/* Generate the html code for the label text. A link will be included if the xml data contains 
		a value for the "url" atrribute. */
	if (childElement.m_href != null && childElement.m_href != "")
	{
		/*show the selected element - start*/
			if (this.m_NavPath[childElement.m_level] == childElement.m_id)
			{
			labelTextHtml = '<a href="' + childElement.m_href + '" class="open">' + childElement.m_label + '</a>';
			}
			else
		    {
  		    labelTextHtml = '<a href="' + childElement.m_href + '">' + childElement.m_label + '</a>';
			}
	}
	else
	{
		labelTextHtml = childElement.m_label;
	}	

/*	if (childElement.m_level == this.m_NavLevel)
	{
		this.m_ds[this.m_di++] = '<h2>'+ labelTextHtml + '</h2>\n';
	}
*/	
	/* Append the final html table code for the item to the global html string variable. */

	if (childElement.m_level == this.m_NavLevel){
		this.m_ds[this.m_di++] = '<h2>'+ labelTextHtml + '</h2>\n';
		//this.m_ds[this.m_di++] = '<li>'+ labelTextHtml + '</li>\n';
	}	
	else {	
		if (childElement.m_subNodes.length == 0){
			this.m_ds[this.m_di++] = '<li>'+ labelTextHtml + '</li>\n';
		}	
		else {	
			this.m_ds[this.m_di++] = '<li>'+ labelTextHtml;
		}	

		//this.m_ds[this.m_di++] = '<li>'+ labelTextHtml + '</li>\n';
		//this.m_ds[this.m_di++] = '<li>'+ labelTextHtml;
	}
}

/* Standard function for generating html image tag code. */
function ls_tree_navigation_imageHtml(src, w, h)
{
	var imgHtml = '<img src="' + src + '" width="' + w + '" height="' + h + '" border="0" align="absmiddle">';
	return imgHtml
}

/* This function had to be used to find the next element-type node in a collection of children 
	since the standards-based browsers interpret whitespace as additional text nodes in the hierarchy. */
function ls_tree_navigation_hasNextSiblingElement(element)
{
	var parent = element.m_parent;		

	if (parent == null)
	{
		if (this.m_ShowHome && element.m_subNodes.length)
			return true;
			
		return false;
	}
	
	var len = parent.m_subNodes.length;
	
	if (parent.m_subNodes[len-1] == element)
		return false;
		
	return true;
}

/* This function had to be used to find the previous element-type node in a collection of children 
	since the standards-based browsers interpret whitespace as additional text nodes in the hierarchy. */	
function ls_tree_navigation_hasPreviousSiblingElement(element)
{
	var parent = element.m_parent;		
	
	if (parent == null)
		return false;
		
	if (parent.m_subNodes[0] == element)
	{
		if (parent.m_level == 0 && this.m_ShowHome)
			return true;
			
		return false;
	}	
	return true;
}	


