
// main function to display left navigatoin on SEDI web site
// 
function DisplayLeftMenu()
{
	// retrieves name (text) of the menu item to show
	var MenuNameToShow = GetSelectedMenuName();
	// retrieves name (text) of the sub menu item to show
	var SubMenuNameToShow = GetSelectedSubMenuName();
	

	// check for IE5 and up,NS6 and up browsers
	if (document.getElementsByTagName)
	{
		HideAllMenuItemsMost();
		if (document.getElementById('LeftMenu'))
			document.getElementById('LeftMenu').style.visibility = 'visible';
		showItemMost(MenuNameToShow,SubMenuNameToShow);
	}
	// IE4 browser
	else if (document.all) 
	{
		
		//HideAllMenuItemsIE4();
		if (document.all['LeftMenu'])
			document.all['LeftMenu'].style.visibility = 'visible';
		//showItemIE4(MenuNameToShow,SubMenuNameToShow);
	}
	
	return false;				
}


// this function changes the style of selected item in navigation menu
// and opens up (shows) sub navigation for the selected item (if exists)
// for browsers IE5 and up,NS6 and up
function showItemMost(ItemName,SubItemName)
{
	// getting collection of all divs on the page
	var allDivsColl = document.getElementsByTagName("div");
	var subDivID;	// will hold span object with subnavigation
	//var subItemID;	// will hold span object with subnavigation Item
	var spanID;		// will hold span object with style
	var arrDivID;	// will hold div object of arrow placement
	
	// looping through all collecton of divs and checking only those with
	// style 'NavLevel1' (wich is our navigation item) and compare text inside
	// with ItemName variable passed to this function. if the item has the same text inside
	// as ItemName variable then we show sub navigation for this item and set it's style
	// to "selected", also moving div with arrow image beside it.
	for (var i=0;i<allDivsColl.length;i++)
	{
		if (allDivsColl[i].className == 'NavLevel1')
		{
			// subnavigation div has the same id as the navigation item div but with letter 'b'
			// prefix. the span that holds style of this navigation has the same id but with
			// letter 'c' prefix and the div with absolute location (where the arrow suppose to go)
			// has the same id as the navigation item but with 'a'' prefix.
			// the submenu item style span has the same id as the navigation item div but with
			// letter 'd' prefix.
			
			subDivID = 'b' + allDivsColl[i].id;
			spanID = 'c' + allDivsColl[i].id;
			arrDivID = 'arr' + allDivsColl[i].id;
			//subItemID = 'd' + allDivsColl[i].id;
			
			// item found
			if (TokenFromHTMLElement(allDivsColl[i].childNodes[1]) == ItemName)
			{
				// if this navigation item has style SPAN around it then
				// change it to "selected" style
				if (document.getElementById(spanID))
				{
					document.getElementById(spanID).className = 'MenuLinkSelected';
					// putting the arrow in place
					if (document.getElementById('im_arrow'))
						document.getElementById(arrDivID).innerHTML = document.getElementById('im_arrow').innerHTML;
				}
				
				// if this item has subnavigation (div) then show it
				if (document.getElementById(subDivID))
				{
					document.getElementById(subDivID).style.visibility = 'visible';
					document.getElementById(subDivID).style.position = 'static';
				
					//selecting subnavigation
					if (document.getElementById(subDivID).childNodes)
					{
						var SubItemNodes = document.getElementById(subDivID).childNodes;
						//alert(SubItemNodes);
						for (var cnt=0;cnt < SubItemNodes.length;cnt++)
						{
							//alert(SubItemNodes[cnt].childNodes);
							if (SubItemNodes[cnt].childNodes)
							{
								if(SubItemNodes[cnt].childNodes.length > 1)
									if (SubItemNodes[cnt].childNodes[1].childNodes.length > 1)
										if (TokenFromHTMLElement(SubItemNodes[cnt].childNodes[1].childNodes[1])==SubItemName)
											SubItemNodes[cnt].childNodes[1].attributes['class'].nodeValue = "SubMenuLinkSelected";
							}
						}
					}
				}
				
				// if item found exit loop ...
				break;
			}
		}
	}
	
	return false;
}


// this function changes the style of selected item in navigation menu
// and opens up (shows) sub navigation for the selected item (if exists)
// for browsers IE4
function showItemIE4(ItemName,SubItemName)
{
	// getting collection of all divs on the page
	var allDivsColl = document.all.tags('div');
	var subDivID;	// will hold span object with subnavigation
	var spanID;		// will hold span object with style
	var arrDivID;	// will hold div object of arrow placement
	
	// looping through all collecton of divs and checking only those with
	// style 'NavLevel1' (wich is our navigation item) and compare text inside
	// with ItemName variable passed to this function. if the item has the same text inside
	// as ItemName variable then we show sub navigation for this item and set it's style
	// to "selected", also moving div with arrow image beside it.
	for (var i=0;i<allDivsColl.length;i++)
	{
		if (allDivsColl[i].className == 'NavLevel1')
		{	
			// subnavigation div has the same id as the navigation item div but with letter 'b'
			// prefix. the span that holds style of this navigation has the same id but with
			// letter 'c' prefix and the div with absolute location (where the arrow suppose to go)
			// has the same id as the navigation item but with 'a'' prefix
			
			subDivID = 'b' + allDivsColl[i].id;
			spanID = 'c' + allDivsColl[i].id;
			arrDivID = 'arr' + allDivsColl[i].id;
			
			// item found
			if (TokenFromHTMLElement(allDivsColl[i]) == ItemName)
			{
				// if this navigation item has style SPAN around it then
				// change it to "selected" style
				if (document.all[spanID])
				{
					//first span inside this div is the "style" span
					document.all[spanID].tags('span')[0].className = 'MenuLinkSelected';
				}
				
				// putting the arrow in place
				if (document.all.im_arrow)
					document.getElementById(arrDivID).innerHTML = document.getElementById('im_arrow').innerHTML;
					
				// if this item has subnavigation (div) then show it
				if (document.all[subDivID])
				{
					document.all[subDivID].style.visibility = 'visible';
					document.all[subDivID].style.position = 'relative';
				
					//selecting subnavigation
					if (document.getElementById(subDivID).childNodes)
					{
						var SubItemNodes = document.getElementById(subDivID).childNodes;
						for (var cnt=0;cnt < SubItemNodes.length;cnt++)
						{
							
							if (SubItemNodes[cnt].childNodes)
								if(SubItemNodes[cnt].childNodes[1])
									if (SubItemNodes[cnt].childNodes[1].childNodes[1])
										if (TokenFromHTMLElement(SubItemNodes[cnt].childNodes[1].childNodes[1])==SubItemName)
											SubItemNodes[cnt].childNodes[1].attributes['class'].nodeValue = "SubMenuLinkSelected";
						}
					}
				}
				
				// if item found exit loop ...
				break;
			}
		}
	}
	return false;
}


// this function retrieves menu name from the field on the page
// that suppose to be selected in the navigation menu
// this field is div on the page with id='LeftMenuName'
function GetSelectedMenuName()
{
	// IE5,NS6 and up browsers
	if (document.getElementsByTagName)
		return TokenFromHTMLElement(document.getElementById('LeftMenuName'));
	else if (document.all) // IE4 browser support
		return TokenFromHTMLElement(document.all.LeftMenuName);
}


// this function retrieves sub menu name from the field on the page
// that suppose to be selected in the sub navigation menu
// this field is span on the page with id='LeftSubMenuName'
function GetSelectedSubMenuName()
{
	// IE5,NS6 and up browsers
	if (document.getElementsByTagName)
		return TokenFromHTMLElement(document.getElementById('LeftSubMenuName'));
	else if (document.all) // IE4 browser support
		return TokenFromHTMLElement(document.all.LeftSubMenuName);
}


// his function hides all subnavigation "divs" for navigation items
// for browser IE4
/*function HideAllMenuItemsIE4()
{
	
	var allDivsColl = document.all.tags('div');
	var subDivID;	// will hold span object with subnavigation
	
	// looping through all navigation items (divs with class="NavLevel1")
	for (var i=0;i<allDivsColl.length;i++)
	{
		if (allDivsColl[i].className == 'NavLevel1')
		{	
			subDivID = 'b' + allDivsColl[i].id;
			// if subnavigation exists , hide it
			if (document.all[subDivID])
			{
				//alert(document.all[subDivID].style.visibility);
				//document.all[subDivID].style.visibility = 'hidden';
				alert(subDivID);
				document.all[subDivID].style.top='0px';
				//document.all[subDivID].style.position = 'absolute';
			}
		}
	}
}*/

// his function hides all subnavigation "divs" for navigation items
// for browser IE5 and up,NS6 and up
function HideAllMenuItemsMost()
{
	var allDivsColl = document.getElementsByTagName("div");
	var subDivID;	// will hold span object with subnavigation
	
	// looping through all navigation items (divs with class="NavLevel1")
	for (var i=0;i<allDivsColl.length;i++)
	{
		if (allDivsColl[i].className == 'NavLevel1')
		{	
			// if subnavigation exists , hide it
			subDivID = 'b' + allDivsColl[i].id;
			if (document.getElementById(subDivID))
			{
				document.getElementById(subDivID).style.visibility = 'hidden';
				document.getElementById(subDivID).style.position = 'absolute';
			}
		}
	}
}


// Returns a token representing the text of the specified HTML object, otherwise null if a token can't be extracted.
// A token can be an empty string '' if no text exists inside the specified HTML element.
// The token is good for loose comparison matching against text of other objects (run through this same function).
// This function strips the HTML from the object, keeps only the text portions, and performs conversions on the text.
// Conversions are: spacing is removed from the text, the text is converted to lowercase.
// NOTE: This function doesn't use the non-greedy matching character '?' for compatibility reasons because some old browsers (ie4, nn4?) don't support it.
// BUGS: Doesn't recognize JavaScript -- Can return a wrong value if JavaScript contains special HTML characters <>&;
//
function TokenFromHTMLElement(objHtml)
{
	if (!objHtml || !objHtml.innerHTML) // if the HTML text of the object is not available
		return null;

	var b = true, ch, token = '';
	// produce a token with HTML tags stripped e.g. <img ...> <a ...></a> and all entities stripped e.g. &nbsp;
	for (var i=0; i < objHtml.innerHTML.length; i++) { // one string index at a time
		ch = objHtml.innerHTML.substr(i, 1); // get char at current index
		if (ch.match(/\<|\&/)) {b = false; continue;} //match beginning char of tag or entity and skip it
		if (ch.match(/\>|\;/)) {b = true; continue;} // match end char of tag or entity and stop skipping
		if (b) token += ch; // append to token if not skipping
	}
	token = token.replace(/\W/g, ''); // compress token into all word chars (eliminates whitespace and abnormal chars)
	return token.toLowerCase(); // return lower case version
}
