var timers = new Array() ;
var offsets = new Array() ;
var zIndex = 100 ;
var isOpera = navigator.appName.indexOf('Opera') >= 0 ;
var MacIE = ( ( navigator.appVersion.indexOf('MSIE') >= 0 ) && ( navigator.appVersion.indexOf('Mac') >= 0 ) ) ;
if (MacIE) var inits = new Array() ;

var currentMenuId = new String() ;

function overMenu ( menuId , e ) {
 if (e) e.cancelBubble=true;
 else if (event) event.cancelBubble=true;
 hideMenuDiff( currentMenuId , menuId ) ;
// hideMenuBrothers( menuId ) ;
 var menuRoot = document.getElementById(menuId+"_root") ;
 if ( !menuRoot ) return ;
 if (currentMenuId.indexOf("menuId")<0) menuRoot.className = "" ;
 currentMenuId = menuId ;
 menuRoot.className = "menu_hover" ;
 var menu = document.getElementById(menuId) ;
 if ( !menu ) return ;
 if ( menu.style.visibility != "visible" ) {
  if ( menuId.lastIndexOf('_') > menuId.lastIndexOf('.') ) { // root menu
   if (menu.style.width) menu.style.width = menuRoot.offsetWidth ;
   var menuBarId = menuId.substr(0,menuId.lastIndexOf('_')) ;
   var menuBar = document.getElementById(menuBarId) ;
   if ( !offsets[menuBarId] ) {
    var menuBarParent = menuBar ;
    var menuBarOffsetLeft = menuBarParent.offsetLeft ;
    var menuBarOffsetTop = menuBarParent.offsetTop ;
    if ( isOpera ) menuBarOffsetLeft -= menuBarParent.scrollLeft ;
    while ( menuBarParent.parentNode ) {
     menuBarParent = menuBarParent.parentNode ;
     if ( menuBarParent.tagName != "TD" && menuBarParent.tagName != "TABLE" ) continue ;
     menuBarOffsetLeft += menuBarParent.offsetLeft ;
     menuBarOffsetTop += menuBarParent.offsetTop ;
     if ( isOpera ) menuBarOffsetLeft -= menuBarParent.scrollLeft ;
    }
    menuBar._offsetLeft = menuBarOffsetLeft ;
    menuBar._offsetTop = menuBarOffsetTop ;
    offsets[menuBarId] = true ; // alert("X:"+menuBarOffsetLeft+"\nY:"+ menuBarOffsetTop) ;
   }
   menu.style.left = menuBar._offsetLeft + menuRoot.offsetLeft + "px" ;
   if ( MacIE && inits[menuId] ) {
    menu.style.top = menuBar.offsetHeight ;
   } else {
    menu.style.top = menuBar._offsetTop + menuBar.offsetHeight + "px" ;
   } // alert("x:"+menu.style.left+"\ny:"+menu.style.top);
   if (MacIE) inits[menuId] = true ;
  } else { // sub menu
   var parentMenu = document.getElementById(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);
  }
  menu.style.zIndex = zIndex++ ;
  menu.style.visibility = "visible" ;
 }
}

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 hideMenuBrothers ( menuId ) {
 parentId = menuId.substr(0,menuId.lastIndexOf('.')) ;
 brotherId = parentId+".0" ;
 brother = document.getElementById( brotherId ) ;
 for ( var i=1 ; brother ; i++ ) {
  if ( brotherId != menuId ) { hideMenu(brother) ; }
  brotherId = parentId + "." + i ;
  brother = document.getElementById( brotherId ) ; 
 }
}

function hideMenu ( menuId ) {
 var menuRoot = document.getElementById(menuId+"_root") ;
 if ( menuRoot ) menuRoot.className = "menu_down" ;  
 var menu = document.getElementById(menuId) ;
 if ( menu ) 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);
}
