/////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2007, 2008, Oracle. All rights reserved.
// Function : TS_GE_VNav
// Comments : 
/////////////////////////////////////////////////////////////////////////////

var uRoles ="";

function TS_GE_VNav(strTextColor, strHoverColor, strFocusColor, strClassName, 
							strShowHome, strStartLevel, strNumLevels)
{
	this.m_TextColor  = '';
	this.m_HoverColor = '';
	this.m_FocusColor = '';
	this.m_ClassName  = 'nav-lev';
	
	this.m_ShowHome   = false;
	
	this.m_StartLevel = 2;
	this.m_NumLevels  = 10;
	this.m_EndLevel   = 11;
	
	this.m_NavPath    = g_navNode_Path;
			
	TS_GE_VNav.prototype.Display = TS_GE_VNav_Display;
	TS_GE_VNav.prototype.DisplayNode = TS_GE_VNav_DisplayNode;
		
	if (strTextColor != '')
		this.m_TextColor = strTextColor;
		
	if (strHoverColor != '')
		this.m_HoverColor = strHoverColor;

	if (strFocusColor != '')
		this.m_FocusColor = strFocusColor;

	if (strClassName != '')
		this.m_ClassName = strClassName;

	if (strShowHome == 'true')
		this.m_ShowHome = true;
		
	if (strStartLevel != '')
	{
		var value = parseInt(strStartLevel);
		if (value != NaN)
			this.m_StartLevel = value;
	}
	
	if (strNumLevels != '')
	{
		var value = parseInt(strNumLevels);
		if (value != NaN)
			this.m_NumLevels = value;
	}

	this.m_EndLevel = this.m_StartLevel + this.m_NumLevels - 1 ;
}

function TS_GE_VNav_Display (node)
{
	//alert("name:"+node.m_label+", level:"+node.m_level);
	document.write('<div class="left-nav">');
	this.DisplayNode(node);	
	document.write('</div>');
}

function TS_GE_VNav_DisplayNode(node)
{

	var bSelected = false;
	var nodeColor = this.m_TextColor;
	var nodeClass = 'nav-lev';

	var nodeLevel = node.m_level;
	
	if (nodeLevel > 6)
		nodeLevel = 6;
	
	if (nodeLevel > 0)
		nodeClass += '-' + nodeLevel;
		
	if (this.m_NavPath.length > 0 && node.m_level < this.m_NavPath.length)
	{
		if (this.m_NavPath[node.m_level] == node.m_id)
		{
			if (node.m_level > 0 || (node.m_level == 0 && this.m_NavPath.length == 1))
			{
				bSelected = true;
				//nodeColor = this.m_FocusColor;
				nodeClass += '-selected';
			}
		}
	}

	if ( (node.m_level == 0 && this.m_ShowHome) || 
     	 (node.m_level >= this.m_StartLevel && node.m_level <= this.m_EndLevel)
	   )
	{
		var ds = new Array();
		var di = 0;
		
		//ds[di++] = (nodeLevel <= 2) ? '<dt' : '<dd';
		//ds[di++] = ' class="' + nodeClass + '"';
		//ds[di++] = '>';
		if (node.cp_OpenInNewWindow){
			ds[di++] = '<a target="_blank" href="' + node.m_href + '">';
			//alert("Nytt fönster " + node.m_href + " (" + node.cp_OpenInNewWindow + ")");
		}else{
			ds[di++] = '<a href="' + node.m_href + '">';
			//alert("Inget nytt fönster, tyvärr...");
		}
		//ds[di++] = '<a href="' + node.m_href + '">';
		ds[di++] = '<div class="' + nodeClass + '">';
		ds[di++] = node.m_label;
		ds[di++] = '</div></a>';
		if (node.m_level > 3){
			ds[di++] = '<div class="vertical-space-4px"></div>';
		}
		document.write(ds.join(''));
	}
	
	/**if (bSelected || node.m_level == 0)
	{	// expand sub-levels (if any)
		for (var i = 0; i < node.m_subNodes.length; i++)
		{
			if (node.m_level == 3){
				document.write('<div class="vertical-space-4px"></div>');
			}
			this.DisplayNode(node.m_subNodes[i]);
		}
	}**/
	if (bSelected || node.m_level == 0)
	{
		// expand sub-levels (if any)
		for (var i = 0; i < node.m_subNodes.length; i++)
		{
			//Check if user role has access to node
			if(node.m_subNodes[i].cp_Roles != null)
			{
				if (!checkRigths(node.m_subNodes[i].cp_Roles))
				{
					continue;
				}
			}
			else
			{
				if (!checkRigths('guest'))
				{
					continue;
				}
			}
			
			this.DisplayNode(node.m_subNodes[i]);
		}
		
		
	}
	
	if (node.m_level == 3){
		document.write('<div class="hr"></div>');
	}
}
function checkRigths(cp_Roles)
{
	var strArray = new Array();
	var bReturn = false;
	
	//check if cp_Roles is a longer string than uRoles
	if (cp_Roles.length > uRoles.length)
	{	
		//splits the string to array
		strArray = splitStrings(cp_Roles);
		for (var i = 0; i < strArray.length ; i++)
		{
			//checks if the user has a matching cridential
			if (uRoles.match(strArray[i]))
			{
				bReturn = true;
				break;
			}
		}
	}
	else
	{
		//splits the string to array
		strArray = splitStrings(uRoles);
		for (var i = 0; i < strArray.length ; i++)
		{
			//checks if the page has a matching cridential
			if (cp_Roles.match(strArray[i]))
			{
				bReturn = true;
				break;
			}
		}
	}
	return bReturn; //Returns true if user has access.
	
}

function splitStrings(strString2Split)
{

	var strArray = new Array();
	//Splits the string into an array by the seperator " | "
	strArray = strString2Split.split("|");
	return strArray;
}

