// Script to activate and desactivate the menu-items

//Set here how many submenus there will be at maximum
var MAX_SUBMENUS = 10;


//This section is used to catch a mouse click on the page
var browser = new Browser();

if (browser.isIE)
  document.onmousedown = pageMousedown;
if (browser.isNS)
  document.addEventListener("mousedown", pageMousedown, true);

function pageMousedown(event) {
  	var el;
  	// Find the element that was clicked on.
  	if (browser.isIE)
    	el = window.event.srcElement;
  	if (browser.isNS)
    	el = (event.target.className ? event.target : event.target.parentNode);
	
  	// If the element clicked on was not a menu item or item, -> hide all sub-menus
  	if (el.id != "menu" && el.className != "menuItem" && el.className != "subMenu" && el.className != "subMenuItem"){
    	hideAllSubMenus();
	}
	return;
}
function Browser() {
  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}


// End of catch click part





function setActiveMenu(){
	var url = window.location.toString();
	var start = url.indexOf("menu=")+8;
	var main_id = url.substr(start,1);
	var sub_id = url.substr(start+1,1);
	
	//Unset all elements
	fktMenuUnHover();
	
	//Show active submenu if it exists
	showSubMenu("sub"+main_id);
	
	//Set correct main_item to active
	if(main_id != 0 && document.getElementById("menuItem"+main_id) != null)
		document.getElementById("menuItem"+main_id).style.color = "#ECE7DB";
	
	//If is set, set correct sub_item to active
	if(sub_id != 0 && document.getElementById("subMenuItem"+main_id+sub_id) != null)
		document.getElementById("subMenuItem"+main_id+sub_id).style.color = "#ECE7DB";
}

//Set all menu_items to normal mode, non-active
function fktMenuUnHover(){
	//Get all menu items, including submenu_items
	menu_items = document.getElementById("menu").getElementsByTagName("div");
	
	for (var i=0;i<menu_items.length;i++)
		if(menu_items[i].className == "subMenuItem")
			menu_items[i].style.visibility="hidden";
		else if(menu_items[i].className == "subSubMenuItem")
			menu_items[i].style.visibility = "hidden";
}


//Show the corresponding submenu
function showSubMenu(subName, obj) {
	hideAllSubMenus();
	
	var el = document.getElementById(subName);	
	if(el != null){
		//Place submenu at correct place and show it
		el.style.left = getPageOffsetLeft(obj) + "px";
		el.style.visibility="visible";
	}
}

// hide all sub-menus
function hideAllSubMenus(){
	var el;
	
	for(var i=1;i<=MAX_SUBMENUS;i++){
		el = document.getElementById("sub"+i);
		if(el != null){
			//el.style.position="absolute";
			el.style.visibility="hidden";
		}
	}
}


function getPageOffsetLeft(el) {
  	// Return the true x coordinate of an element relative to the page.
  	return el.offsetLeft + (el.offsetParent ? getPageOffsetLeft(el.offsetParent) : 0);
}


