//***************************************************
//* Javascript code used to control vertical menu
//***************************************************
//
//***************************************************
//* Variables
//***************************************************
var menuState = new Object;
var moverColor = '#FF0000';
var moverBackgroundColor = '#FFFF00';
var selId = '';
var selColor = '#BDBDBD';
var selBackgroundColor = '#FF0000';
var orgBackgroundColor = '#000000';
var orgColor = '#FFFFFF';
var orgSelBackgroundColor = '#000000';
var orgSelColor = '#FFFFFF';
//***************************************************
//* ActivateMainDiv
//* In order to make IE expand and collapse main div correctly
//* for some reason main div has to be 'activated'
//***************************************************
function activateMainDiv(){
	hidemsg('leftcol');
	showmsg('leftcol');
}
//***************************************************
//* getMenuState
//***************************************************
function getMenuState(){
	var strAllMenus = '';
  for (id in menuState){
    if (menuState[id] == true){
    	if (strAllMenus == ''){
				strMenuId = id;
			}
			else{
				strMenuId = "/" + id;
			}
      strAllMenus = strAllMenus + strMenuId;
    }
  }
	return strAllMenus;
}
//***************************************************
//* menuUnLoad
//***************************************************
function menuUnLoad(){
  //Create Cookie for menustate
  strOpenMenus = getMenuState();
  setCookie('menuitemopen',strOpenMenus,0);
  //Create Cookie for selected menu item
  setCookie('menuitemsselected',selId,0);
  strOpenMenus = null;
}
//***************************************************
//* menuLoad
//***************************************************
function menuLoad(){
	//***** Get menu state ******
	cookievalue = getCookie('menuitemopen');
	//If no cookie is set return
	if (cookievalue !== null){
    var menus =  new Array();
    var menus = cookievalue.split("/");
    for (i=0;i<menus.length;i++){
      var MenuId = menus[i];
      //Only if menu item still exist we call menuShowHide
  		obj = document.getElementById(MenuId);
  		if (obj !== null){
      	menuShowHide(MenuId);
      }
    }
	}
	menus = null;
	activateMainDiv();
}
//***************************************************
//* preserveMenuState
//***************************************************
function setMenuState(menuid, value){
	menuState[menuid] = value;
}
//***************************************************
//* Close all sub menus which id starts withd MenuId
//***************************************************
function closeAllSubMenus(MenuId){
	MenuIdLength = MenuId.length;
	//Get all div tag's
  var divs = document.getElementsByTagName("div");
  //Loop through all div's
  for (var i = 0; i < divs.length; i++){
    var curId = divs[i].id;
		//Id's for submenus are longer than parent menu
    if (divs[i].id.length > MenuId.length){
     	//All subemnus has parent menu id as start of their own id
		  if (divs[i].id.substr(0,MenuIdLength) == MenuId.substr(0,MenuIdLength)){
				document.getElementById(curId).style.display = 'none';
				setMenuState(curId, false);
			}
		}
	}
	divs = null;
}
//***************************************************
//* menuShowHide
//***************************************************
function menuShowHide(MenuId){
	obj = document.getElementById(MenuId);

	if (obj.style.display == 'block'){
		obj.style.display = 'none';
		setMenuState(MenuId, false);
	}
	else{
		obj.style.display = 'block';
		setMenuState(MenuId, true);
		closeAllSubMenus(MenuId);
	}
	obj = null;
}
//***************************************************
//* getObjectId
//***************************************************
function getObjectId(e){
	var targ
	var tname
	var tid
	if (!e){
		var e = window.event;
	}
	if (e.target){
	  targ = e.target;
	 }
	else if (e.srcElement){
	  targ = e.srcElement;
	}
	if (targ.nodeType == 3){ // defeat Safari bug
   targ = targ.parentNode;
	}
	if (targ.tagName.toLowerCase() == 'a'){
   targ = targ.parentNode;
	}
	tid=targ.id;
	return tid;
}
//***************************************************
//* menuMouseOut
//***************************************************
function menuMouseOut(e){
  return;
	tid = getObjectId(e);
	if (tid.charAt(0) == 'D'){
		obj = document.getElementById(tid);
		//Don't restore orgriginal colors if item is selected
    obj.style.color = orgColor;
	}
	obj = null;
}
//***************************************************
//* menuMouseOver
//***************************************************
function menuMouseOver(e){
  return;
	tid = getObjectId(e);
	if (tid.charAt(0)=='D'){
		obj = document.getElementById(tid);
    orgColor = obj.style.color;
    obj.style.color = moverColor;
	}
}
//***************************************************
//* menuSelect
//***************************************************
function menuSelect(tid){
	if (selId == tid){
	 	selId = '';
		return;
	}
	orgSelColor =	orgColor;
	selId = tid;
}
//***************************************************
//* menuClick
//***************************************************
function menuClick(e){
	tid = getObjectId(e);
	if (tid.charAt(0) == 'D'){
		mid = 'M' + tid.substr(1,tid.length-1);
		//Open or close menuitem
		menuSelect(tid);
		if (eval(obj = document.getElementById(mid))){
		  menuShowHide(mid);
    }
	}
	activateMainDiv();
}
//***************************************************
//* Initialize events
//***************************************************
addEvent(window, 'load', menuLoad);
addEvent(window, 'unload', menuUnLoad);
addEvent(document, 'click', menuClick);
addEvent(document, 'mouseover', menuMouseOver);
addEvent(document, 'mouseout', menuMouseOut);
