///////////////////////////////////////////////////////////////////////////////////////////
//  Global variable declarations for the menu
//
var obj_visible = new Array();    // array of visible menu elements idexed on 'path'
var cur_menu=""                   // path of submenu with the mouse captured
var cur_main=""                   // path of active mainmenu
var xxx=""
var obj_cur_menu=null;
var obj_cur_main=null;

var obj_menuitem = new Object
    obj_menuitem["name"]="";
    obj_menuitem["class"]="";    
    obj_menuitem["object"]=null;
    obj_menuitem["parent"]=null

///////////////////////////////////////////////////////////////////////////////////////////
// Name    : initPage
// Use     : Initilize global variables and calls the dohideAll function for the first time
// Expects : nothing
// Returns : nothing
//
function initMenu() {
  obj_visible=new Array(); 
  selectMainMenu(cur_main);
}


///////////////////////////////////////////////////////////////////////////////////////////
// Name    : onOut
// Use     : Called when leaving a menu so we know we are not 'on' a menu.
// Expects : nothing
// Returns : nothing
//
function onOut() { 
	cur_menu=""; 	
}

///////////////////////////////////////////////////////////////////////////////////////////
// Name    : selectMenu
// Use     : Called when leaving a menu so we know we are not 'on' a menu.
// Expects : nothing
// Returns : nothing
//
function selectMenu(selectclass,unselectclass) {  	
	if (obj_cur_menu!=null) obj_cur_menu.className=unselectclass;			
	obj_cur_menu = obj_menuitem["object"]	
	obj_menuitem["object"].className=selectclass;	
	obj_menuitem["class"]=selectclass;	
	var ar_path=obj_menuitem["object"].id.split("/");	
	window.status=ar_path[1];	
	selectMainMenu(ar_path[1]);
	cur_menu="";
	doHideAll();
}

function selectMainImgMenu(newmain) {  		
	if (cur_main!="")  {
		if (obj_cur_main==null )obj_cur_main = GetMenuElement(cur_main) ;	  
	}
	// deselect
	if (obj_cur_main!=null ){
		try{				
			if (obj_cur_main.src != "")	{
				obj_cur_main.src=obj_cur_main.src.replace("_down","");
				//obj_cur_main.prevsrc=obj_cur_main.src;
			}
		} catch(e){}
	}	
	// select
	if (newmain!="") cur_main=newmain
	if (cur_main!="")  {
	  obj_cur_main = GetMenuElement(cur_main) ;	  	  	  	  
	  if (obj_cur_main!=null ){
	  	  try{
			  newsrc=obj_cur_main.src
			  if (newsrc) {
				  newsrc=newsrc.replace("_over","");	  
				  newsrc=newsrc.replace("_down","");	  	  
				  newsrc=newsrc.replace(cur_main,cur_main+"_down")	  	  	  
				  obj_cur_main.src=newsrc;
			  }
			  obj_cur_main.prevsrc=obj_cur_main.src;
		  } catch(e) {}
	  }
  }
}

function selectMainMenu(newmain) {  		
	if (cur_main!="")  {
		if (obj_cur_main==null )obj_cur_main = GetMenuElement(cur_main) ;	  
	}
	// deselect
	if (obj_cur_main!=null ){
		try{				
			if (obj_cur_main.src != "")	{
				obj_cur_main.src=obj_cur_main.src.replace("_down","");
				//obj_cur_main.prevsrc=obj_cur_main.src;
			}
		} catch(e) {}
	}	
	// select
	if (newmain!="") cur_main=newmain
	if (cur_main!="")  {
	  obj_cur_main = GetMenuElement(cur_main) ;	  	  	  	  
	  if (obj_cur_main!=null ){
	  	  try {	
			  newsrc=obj_cur_main.src
			  if (newsrc) {
			  	newsrc=newsrc.replace("_over","");	  
			  	newsrc=newsrc.replace("_down","");	  	  
			  	newsrc=newsrc.replace(cur_main,cur_main+"_down")	  	  	  
			  	obj_cur_main.src=newsrc;
			  }
			  obj_cur_main.prevsrc=obj_cur_main.src;
		  } catch(e) {}
	  }
    }
}

///////////////////////////////////////////////////////////////////////////////////////////
// Name    : doDisplay
// Use     : Is called when moving the mouse over a menu item. Then checks if a submenu
//           is avialable display this and hides all menu's not in the path.
// Expects : submenu  [REQUIRED]    path of the menu about to displayed
//           top      [REQUIRED]    top position
//           left     [REQUIRED]    left position
//           width    [REQUIRED]    width position
//           prefix   [REQUIRED]    css prefix
// Returns : nothing
//
function doDisplay(submenu,top,left,width,level,prefix) {
	
	if (cur_menu==submenu) return;
	var cur_levels =submenu.split("/");
	// remember the name. The timer needs this information. It
	// will hide all the menus when this is empty.
	cur_menu=submenu;
	// Checks weather it should hide previous displayed menu's
	// Only menu's wich are in the same (parents) path are displayed
	for  (var id in obj_visible) {
		var hide=false;
		var prev_levels=id.split("/");
		if (prev_levels.length<=cur_levels.length) {
			for (var i=0 ; i< prev_levels.length ; i++ ) {
		    if (prev_levels[i]==cur_levels[i]) continue;
				hide=true
		 		break;
		  }
		} else {
			var hide=true;
		}
		obj_visible[id].style.visibility=(hide)?"hidden":"visible";
  	}
  	
	// DE-SELECT when we are in the same menu	
	// the previous levels will be the same
	if (isMenuObject(obj_menuitem["object"]) && obj_menuitem["object"]!=null) {
	  var prev_levels=obj_menuitem["name"].split("/");
	  if (prev_levels.length<cur_levels.length) {
			// if previous levels are smaller we are in the siblings menu we
			// store a copy in the parent propery so we can unselect him later
			var obj_temp=new Object;
			obj_temp["object"]=obj_menuitem["object"];
			obj_temp["class"]=obj_menuitem["class"];
			obj_temp["name"]=obj_menuitem["name"];
			obj_temp["parent"]=obj_menuitem["parent"]
      		obj_menuitem["parent"]=obj_temp;
			obj_temp=null;
		} else {
			obj_menuitem["object"].className=obj_menuitem["class"];
    	}
	
    	if (prev_levels.length>cur_levels.length) {
			if (isMenuObject(obj_menuitem["parent"]) && obj_menuitem["parent"]!=null) {
				obj_menuitem["parent"]["object"].className=obj_menuitem["parent"]["class"];
        		obj_menuitem["parent"]=(isMenuObject(obj_menuitem["parent"]["parent"]))?obj_menuitem["parent"]["parent"]:null;;
			}
		}
	}

	
	// Store the properties of the new menuitem
	obj_menuitem["name"]="menuitem"+submenu;
	obj_menuitem["object"]=GetMenuElement(obj_menuitem["name"]);
	if (isMenuObject(obj_menuitem["object"]) && obj_menuitem["object"]!=null) {
		obj_menuitem["class"]=obj_menuitem["object"].className;
	}
	
	// SELECT by changing classname of the new menuitem
	if (isMenuObject(obj_menuitem["object"]) && obj_menuitem["object"]!=null) {
		obj_menuitem["object"].className=prefix + level +"_OVER";
	}
	
	
	// check if we have siblings
	if (cur_menu=="") return;		
	var obj_submenu = GetMenuElement(submenu);
	
	// If there are no siblings we are finished
	if (obj_submenu==null) return;
	
	// Show siblings
	obj_submenu.style.top=top + 'px'; ;	
	obj_submenu.style.left=left + 'px';
	obj_submenu.style.width=width + 'px';	
	obj_visible[cur_menu]=obj_submenu;	  
	obj_submenu.style.visibility="visible";	  
	ShowPath(cur_menu);
	
}


///////////////////////////////////////////////////////////////////////////////////////////
// Name    : doHideAll
// Use     : Hides all the visible menu's when the mouse is not
//           ( cur_menu=="" this is set in the onOut() function )
//           over them
//           the function will re-scedule itself every second  and is first called in
//           the onload event of the body
// Expects : nothing
// Returns : nothing
//
function doHideAll() {
  // Hide al the visible menu's
  if (cur_menu=="") {
		// always deselect last item
		if (isMenuObject(obj_menuitem["parent"]) && obj_menuitem["parent"]!=null) {
		  deSelectParent(obj_menuitem["parent"]);
		}
	  for  (var id in obj_visible) { obj_visible[id].style.visibility="hidden"; }
	  obj_visible= new Array();;
  }
}
function deSelectParent(obj_menu) {
	if (obj_menu["parent"]  && isMenuObject(obj_menuitem["parent"]) && obj_menuitem["parent"]!=null) {
    deSelectParent(obj_menu["parent"])
  }
	obj_menu["object"].className=obj_menu["class"];
}

function isMenuObject(obj) {	return (typeof obj=="object") }

///////////////////////////////////////////////////////////////////////////////////////////
// Name    : ShowPath
// Use     : Shows the current path above the content
// Expects : current path (id of a meny)
// Returns : nothing
//
function ShowPath(currentpath) {
	//var CurrentPath=CurrentPath.split("/");
	//alert(currentpath);
  //GetMenuElement("path").innerText=currentpath;
}
