var sideMenu = 0;
var thirdLevel = 0;
var sideMenuCounter = 0;
var thirdLevelCounter = 0;
function renderSideMenu(menuID, subMenuContainer, subMenu){
	var menusLoaded = 0;
	for(i=0;i<menuID.length;i++){
		if (document.getElementById('nav_'+menuID[i]) != null){
			menusLoaded++;
		}
	}
	if(menusLoaded == menuID.length){
		clearInterval(sideMenu);
		var foundMenu = 0;
		for(var i=0; i<menuID.length;i++){
			if (document.getElementById('nav_'+menuID[i]) != null){
				var tempMenu = document.getElementById('nav_'+menuID[i]);
				var menuArray = new Array();
				var curElement = "";
				menuArray = tempMenu.childNodes;
				for(j=0;j<menuArray.length;j++){
					if(menuArray[j].className.indexOf("selected") != -1){
						curElement = menuArray[j];
						if(document.getElementById("sideMenuTitle") != null){
							document.getElementById("sideMenuTitle").innerHTML = curElement.firstChild.innerHTML + " MENU";
						}
						if(curElement.lastChild.id.indexOf("navsub_" + menuID[i]) > -1){
							var newMenu = "<ul id='navsub_"+ menuID[i] +"_123456'>" +  curElement.lastChild.innerHTML + "</ul>";
							foundMenu = 1;
						}else{
							var newMenu = "";
						}
					}
				}
				if(document.getElementById(subMenuContainer) != null){
					if(newMenu != ""){
						document.getElementById(subMenuContainer).style.display = "block";
						document.getElementById(subMenu).innerHTML = newMenu;
					}else if(foundMenu == 0){
						document.getElementById(subMenuContainer).style.display = "none";
					}
					if(curElement == "" && foundMenu == 0){
						document.getElementById(subMenuContainer).style.display = "none";
					}
				}
			}			
		}
	}else{
		sideMenuCounter++;
		if(sideMenuCounter >= 150){
			clearInterval(sideMenu);
		}
	}
}

function renderThirdLevel(menuID, subMenuContainer, subMenu){
	var menusLoaded = 0;
	for(i=0;i<menuID.length;i++){
		if (document.getElementById('nav_'+menuID[i]) != null){
			menusLoaded++;
		}
	}
	if(menusLoaded == menuID.length){
		clearInterval(thirdLevel);
		var foundMenu = 0;
		var newMenu = "";
		for(var i=0; i<menuID.length;i++){
			if (document.getElementById('nav_'+menuID[i]) != null){
				var tempMenu = document.getElementById('nav_'+menuID[i]);
				var menuArray = [];
				var curElement = "";
				menuArray = tempMenu.childNodes;
				for(j=0;j<menuArray.length;j++){
					if(menuArray[j].className.indexOf("selected") != -1){
						curElement = menuArray[j];
						if(curElement.lastChild.id.indexOf("navsub_" + menuID[i]) > -1){
							menuArray = [];
							menuArray = curElement.lastChild.childNodes;
							for(k=0;k<menuArray.length;k++){
								if(menuArray[k].className.indexOf("selected") != -1){
									curElement = menuArray[k];
									if(curElement.lastChild.id.indexOf("navsub_" + menuID[i]) > -1){
										var newMenu = "<ul id='navsub_"+ menuID[i] +"_345678'>" +  curElement.lastChild.innerHTML + "</ul>";
										foundMenu = 1;
									}
								}								
							}
						}
					}
				}
				if(document.getElementById(subMenuContainer) != null){
					if(newMenu != ""){
						document.getElementById(subMenuContainer).style.display = "block";
						document.getElementById(subMenu).innerHTML = newMenu;
					}else if(foundMenu == 0){
						document.getElementById(subMenuContainer).style.display = "none";
					}
					if(curElement == "" && foundMenu == 0){
						document.getElementById(subMenuContainer).style.display = "none";
					}
				}
			}			
		}
	}else{
		thirdLevelCounter++;
		if(thirdLevelCounter >= 150){
			clearInterval(thirdLevel);
		}
	}
}

var menuArr = new Array('14623'); //array form must be used for this to work properly
sideMenu = setInterval("renderSideMenu(menuArr,'internal_nav','subMenu');", 100);

var orderMenu = 0;
var orderMenuCount = 0;
function addAccountMenu(menuID, subMenuContainer, subMenu){
	var menusLoaded = 0;
	for(i=0;i<menuID.length;i++){
		if (document.getElementById('nav_'+menuID[i]) != null){
			menusLoaded++;
		}
	}
	if(menusLoaded == menuID.length && document.getElementById(subMenuContainer) != null){
		clearInterval(orderMenu);
		var foundMenu = 0;
		for(var i=0; i<menuID.length;i++){
			if (document.getElementById('nav_'+menuID[i]) != null){
				var tempMenu = document.getElementById('nav_'+menuID[i]);
				var menuArray = new Array();
				var curElement = "";
				menuArray = tempMenu.childNodes;
				for(j=0;j<menuArray.length;j++){
					if(menuArray[j].className.indexOf("selected") != -1){
						curElement = menuArray[j];
						if(document.getElementById("sideMenuTitle") != null){
							document.getElementById("sideMenuTitle").innerHTML = curElement.firstChild.innerHTML + " MENU";
						}
						if(curElement.lastChild.id.indexOf("navsub_" + menuID[i]) > -1){
							var newMenu = "<ul id='navsub_"+ menuID[i] +"_123456'>" +  curElement.lastChild.innerHTML + "</ul>";
							foundMenu = 1;
						}else{
							var newMenu = "";
						}
					}
				}
				if(document.getElementById(subMenuContainer) != null){
					if(newMenu != ""){
						document.getElementById(subMenuContainer).style.display = "block";
						document.getElementById(subMenu).innerHTML = newMenu;
					}else if(foundMenu == 0){
						document.getElementById(subMenuContainer).style.display = "none";
					}
					if(curElement == "" && foundMenu == 0){
						document.getElementById(subMenuContainer).style.display = "none";
					}
				}
			}			
		}
	}else{
		
		orderMenuCount++;
		if(orderMenuCount >= 150){
			clearInterval(orderMenu);
		}
	}
}
//thirdLevel = setInterval("renderThirdLevel(menuArr,'thirdLevelNav','tlMenu');", 100);
/*
This .js file need only be included at the top of the page inside the <head> area and the file does the rest. 
The call is actually set up as an asynchronous function call so that the page can load and the function won't actually 
do anything until the div ID of nav_###### actually exists on the page. This is used to get rid of a javascript error made 
by the code running with a null ID, and then actually rendering the side menu as soon as it's available. The parameters 
of window.setTimeout are the function call and an amount of time to wait before starting to run the function. The first 
parameter is the one that we're interested in because that is what it going to change whenever this .js file is implemented 
elsewhere. The function is 

	renderSideMenu( menuID, subMenuContainer, subMenu ) 
	
and here is an explanation of each parameter:

	menuID: The menu's ID that is generated by BC. eg, in {module_menu,302603} it would be '302603'.
	
	subMenuContainer: The area that contains the submenu in the event that you want the area to not display if 
		no submenu exists for this menu.
		eg, in
			<div id="internal_right_nav">
				<div id="internal_right_content">
					<span class="header_span"></span>
					<div class="pad">
						<h3 id="sideMenuTitle"></h3>
						<span id="text_menu"></span>
					</div>
				</div>
			</div>
		it would be 'internal_right_nav'.
	
	subMenu: The area where the submenu will be rendered. Any text that is inside that area will be replaced 
		with the menu. 
		eg, in 
			<div id="internal_right_nav">
				<div id="internal_right_content">
					<span class="header_span"></span>
					<div class="pad">
						<h3 id="sideMenuTitle"></h3>
						<span id="text_menu"></span>
					</div>
				</div>
			</div>
		 it would be 'text_menu'.
*/
