﻿function ShowSubMenu(menuid) {
	if (menuid != "Sub_Frontpage")
	{ HighlightRootMenuItem(menuid); }
	
	var oDiv = document.getElementById('SubMenus');
	if (oDiv.firstChild) { // check for children
		var oChild = oDiv.firstChild;
		while (oChild) { // run over them
			if (oChild.nodeType == 1) { // element oChild is a first level child of oDiv Do what you want with it here
				if (oChild.id == menuid)
				{ oChild.style.display = 'block'; }
				else 
				{ oChild.style.display = 'none'; }
			}
			oChild = oChild.nextSibling;
		}
	}
}

function HighlightRootMenuItem(menuid) {
	var oDiv = document.getElementById('MenuRoot');
	if (oDiv.firstChild) {
		var oChild = oDiv.firstChild;
		while (oChild) {
			oChild.className = "";
			oChild = oChild.nextSibling;
		}
	}
	document.getElementById('Main_' + menuid).className = 'Selected';
}

function HighlightMenu() 
{
	if (window.location.pathname == "/") {
		document.getElementById('Main_Frontpage').className = 'Selected';
		ShowSubMenu("Sub_Frontpage");
	}
	else
	{
		HighlightSubMenuItem();
	}

	function HighlightSubMenuItem() {
		pathArray = window.location.pathname.split('/');
		path = pathArray[1];

		var oDiv = document.getElementById('SubMenus');
		var ForceHighlight = document.getElementById('MainMenuForceHighlightItem').value;

		if (oDiv.firstChild) {
			var oChild = oDiv.firstChild;
			while (oChild) {
				if (oChild.nodeType == 1) {

					if (oChild.firstChild) {
						var oChild2 = oChild.firstChild;
						var CurrentLink;

						while (oChild2) {
							CurrentLink = oChild2.getAttribute("value");

							if (CurrentLink) {
								if ((CurrentLink == ForceHighlight) || ("/sivu/" + CurrentLink == ForceHighlight) || (ForceHighlight == '' && (CurrentLink.toLowerCase() == '/' + path.toLowerCase() || CurrentLink.toLowerCase() == path.toLowerCase()))) {
									LinkName = oChild2.firstChild.innerHTML;

									oChild2.className = "Selected";
									oChild2.innerHTML = '<img src="/Content/Images/Master/SubMenuActiveLeft.png"/><a href="' + CurrentLink + '">' + LinkName + '</a><img src="/Content/Images/Master/SubMenuActiveRight.png"/>';

									//document.getElementById('AreaDefaultMenu').value = oChild.id;

									ShowSubMenu(oChild.id);
									if (document.getElementById('Main_' + oChild.id))
									{ document.getElementById('Main_' + oChild.id).className = 'Selected'; }

								}
							}

							oChild2 = oChild2.nextSibling;
						}
					}
				}
				oChild = oChild.nextSibling;
			}
		}
	}
}
