
var zIndex = 100 ;
var currentMenuId = new String() ;

function overMenu ( menuId , e ) {
 if (e) e.cancelBubble=true;
 else if (event) event.cancelBubble=true;
 
 hideMenuDiff( currentMenuId , menuId ) ;

 var menuRoot = $(menuId+'_root') ;
 if ( !menuRoot ) {
   return;
 }
 if (currentMenuId.indexOf("menuId")<0) {
   menuRoot.className = "" ;
 }
 currentMenuId = menuId;
 menuRoot.className = "menu_hover" ;

 var menu = $(menuId);
 if (!menu || menu.style.visibility == "visible") {
  return ;
 }
 if ( menuId.lastIndexOf('_') > menuId.lastIndexOf('.') ) {
  overRootMenu(menuRoot, menu, menuId);
 } else {
  overSubMenu(menuRoot, menu, menuId);
 }
 menu.style.zIndex = zIndex++ ;
 menu.style.visibility = "visible" ;
}

function overSubMenu(menuRoot, menu, menuId) {
   var parentMenu = $(menuId.substr(0,menuId.lastIndexOf('.'))) ;
   menu.style.left = parentMenu.offsetLeft + parentMenu.offsetWidth + "px" ; menu.left=menu.style.left;
   menu.style.top = parseInt(parentMenu.style.top) + menuRoot.offsetTop+"px" ; // alert("_x:"+menu.style.left+"\n_y:"+menu.style.top);
}

function overRootMenu(menuRoot, menu, menuId) {
   if (menu.style.width) menu.style.width = menuRoot.offsetWidth ;
   var menuBarId = menuId.substr(0,menuId.lastIndexOf('_')) ;
   
   // Compute offset
   var menuBar = $(menuBarId) ;
   var offset = Position.positionedOffset(menuBar);

   // Set menu position
   menu.style.left = offset[0] + menuRoot.offsetLeft + "px" ;
   menu.style.top = offset[1] + menuBar.offsetHeight + "px" ;
}

function outMenu ( menuId , e ) {
 if (e) e.cancelBubble=true;
 else if (event) event.cancelBubble=true;
}

function hideMenuDiff ( oldMenuId , newMenuId ) {
 if ( oldMenuId && oldMenuId != newMenuId ) {
  if ( newMenuId.indexOf(oldMenuId) == -1 ) {
   hideMenu( oldMenuId ) ;
   hideMenuDiff( oldMenuId.substr(0,oldMenuId.lastIndexOf('.')) , newMenuId ) ;
  } else if ( oldMenuId.indexOf(newMenuId) > -1 ) {
   while ( oldMenuId != newMenuId ) {
    hideMenu( oldMenuId ) ;
    oldMenuId = oldMenuId.substr(0,oldMenuId.lastIndexOf('.')) ;
   }
  }
  if ( oldMenuId.split('.').length > newMenuId.split('.').length && oldMenuId.lastIndexOf('.') > oldMenuId.lastIndexOf('_') && newMenuId.lastIndexOf('.') > newMenuId.lastIndexOf('_') && oldMenuId.substr(0,oldMenuId.lastIndexOf('.')).indexOf(newMenuId.substr(0,newMenuId.lastIndexOf('.'))) > -1 ) {
   hideMenu( oldMenuId.substr(0,oldMenuId.lastIndexOf('.')) ) ;
  } 
 }
}

function hideMenu ( menuId ) {
 var menuRoot = $(menuId+"_root") ;
 if ( menuRoot ) menuRoot.className = "menu_down" ;  
 var menu = $(menuId) ;
 if ( menu && menu.style ) menu.style.visibility = "hidden" ; 
}

function hideAllMenus ( ) {
 var menuPath = currentMenuId ;
 currentMenuId = new String() ;
 while ( menuPath.lastIndexOf('_') < menuPath.lastIndexOf('.') ) {
  hideMenu(menuPath);
  menuPath = menuPath.substr(0,menuPath.lastIndexOf('.')) ;
 }
 hideMenu(menuPath);
}
