//menu
function showMenu(menuCapTD_ID, menuDiv_ID){
  var menuCapTD = window.document.getElementById(menuCapTD_ID);
  var menuDiv = window.document.getElementById(menuDiv_ID);
  if (menuCapTD || menuDiv){
    //if (menuDiv.style.display == "none"){
      menuDiv.style.display = "";
      menuDiv.style.width = menuWidth(menuCapTD, menuDiv) + "px";
      menuDiv.style.top = menuTop(menuCapTD, menuDiv) + "px";
      menuDiv.style.left = menuLeft(menuCapTD, menuDiv) + "px";
    //}
  }
}

function hideMenu(menuDiv_ID){
  var menuDiv = window.document.getElementById(menuDiv_ID);
  if (menuDiv){
    menuDiv.style.display = "none";
  }
}

function overCell(menuCell){
  menuCell.className = "menu_over_cell";
  var _idx = 0;
  var _child;
  if( typeof menuCell.children == 'undefined' ) {//mozilla
    _child = menuCell.childNodes[_idx];
  }
  else {//IE
    _child = menuCell.children[_idx];
  }
  //alert("_child.tagName: "+_child.tagName);
  while(_child && (_child.tagName == "A" || _child.tagName == "INPUT" || _child.tagName == "LABEL")){
    _child.className = "menu_over_cell";
    _idx += 1;
    if( typeof menuCell.children == 'undefined' ) {//mozilla
      _child = menuCell.childNodes[_idx];
    }
    else {//IE
      _child = menuCell.children[_idx];
    }
  }
}

function outCell(menuCell){
  menuCell.className = "menu_out_cell";
  var _idx = 0;
  var _child;
  if( typeof menuCell.children == 'undefined' ) {//mozilla
    _child = menuCell.childNodes[_idx];
  }
  else {//IE
    _child = menuCell.children[_idx];
  }
  while(_child && (_child.tagName == "A" || _child.tagName == "INPUT" || _child.tagName == "LABEL")){
    _child.className = "menu_out_cell";
    _idx += 1;
    if( typeof menuCell.children == 'undefined' ) {//mozilla
      _child = menuCell.childNodes[_idx];
    }
    else {//IE
      _child = menuCell.children[_idx];
    }
  }
}

function menuTop(menuCapTD, menuDiv){
  var _top = menuCapTD.offsetTop + menuCapTD.clientHeight - 4;
  //var _clientHeight = menuCapTD.offsetHeight;
  //var _clientHeight = menuCapTD.clientHeight;
  var _scrollTop = 0;
  var _menuHeight = menuDiv.offsetHeight;

	while (menuCapTD.offsetParent) {
	  menuCapTD = menuCapTD.offsetParent
		_top += menuCapTD.offsetTop;
	}
  /*
  if (menuCapTD.tagName == "BODY"){
    _clientHeight = menuCapTD.clientHeight;
    _scrollTop = menuCapTD.scrollTop;
  }

  if (_top < _scrollTop){
    hideMenu(menuDiv);
  }
  else if (_menuHeight + 5 > _clientHeight){
    hideMenu(menuDiv);
  }
  else if (_top + _menuHeight > _scrollTop + _clientHeight){
    _top = _scrollTop + _clientHeight - _menuHeight;
  }
  */
  return _top;
}

function menuWidth(menuCapTD, menuDiv){
  var _width = 0;
  var _idx = 0;
  var _table;
  if( typeof menuDiv.children == 'undefined' ) {//mozilla
    _table = menuDiv.childNodes[0];
  }
  else {//IE
    _table = menuDiv.children[0];
  }
  var _cell;
  if( typeof _table.cells == 'undefined' ) {//mozilla
    _cell = _table[_idx];
  }
  else {//IE
    _cell = _table.cells[_idx];
  }
  while(_cell){
    if (_width < _cell.offsetWidth)
      _width = _cell.offsetWidth;
    _idx += 1;
    if( typeof _table.cells == 'undefined' ) {//mozilla
      _cell = _table[_idx];
    }
    else {//IE
      _cell = _table.cells[_idx];
    }
  }
  if (_width > 0)
    return _width + 20;
  else
    return menuCapTD.clientWidth + 20;
}

function menuLeft(menuCapTD, menuDiv){
  var _left = 0;
  var _clientWidth = 0;
  var _scrollLeft = 0;
  var _scrollWidth = 0;
  var _captionWidth = menuCapTD.offsetWidth;
  var _maxWidth = menuDiv.offsetWidth;
  while(menuCapTD.offsetParent){
    _left += menuCapTD.offsetLeft;
    menuCapTD = menuCapTD.offsetParent;
  }
  if (menuCapTD.tagName == "BODY"){
    _clientWidth = menuCapTD.clientWidth;
    _scrollLeft = menuCapTD.scrollLeft;
    _scrollWidth = menuCapTD.scrollWidth;
  }
  
  if (_scrollLeft > _left + _captionWidth - 10){
    hideMenu(menuDiv);
  }
  else if (_maxWidth + 5 > _clientWidth){
    hideMenu(menuDiv);
  }
  else if (_scrollLeft + _clientWidth < _left + 10){
    hideMenu(menuDiv);
  }
  else {
    if (_left < _scrollLeft){
      _left = _scrollLeft;
    }
    else if (_left + _maxWidth > _scrollLeft + _clientWidth){
      _left = _scrollLeft + _clientWidth - _maxWidth;
    }
  }
  return _left;
}
//end of menu
