function menuOut() {
  ob = this
  ob.getElementsByTagName('a')[0].style.color = 'white';
  
  if ((ul = ob.getElementsByTagName('ul')[0])) {
    ul.style.display='none';
    ul.style.zIndex = '1';
  }
}

function menuOver() {
  ob = this
  ob.getElementsByTagName('a')[0].style.color = '#BF311A';

  if ((ul = ob.getElementsByTagName('ul')[0])) {
    ul.style.display='block';
    ul.style.zIndex = '30';
    ul.style.top = '1.9em';
  }
}

function addEvent(obj, type, fn) {
  if (obj.addEventListener) obj.addEventListener(type, fn, false);
  else if (obj.attachEvent) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() {obj["e"+type+fn](window.event); }
    obj.attachEvent("on"+type, obj[type+fn]);
  }
}


function setMenu() {
  li_margin = 7;
  if (navigator.appName == "Microsoft Internet Explorer") li_margin = 0;
  
  menu = document.getElementById('menuTop');
  menu_width = menu.offsetWidth;
  li_arr = menu.getElementsByTagName('li');
  li_num = li_arr.length;
  
  menu_x = checkLeft(menu);
  for (i=0;i<li_num;i++) { 
    li_width = li_arr[i].offsetWidth;
    if ((ul = li_arr[i].getElementsByTagName('ul')[0])) {
      //IE
      addEvent(li_arr[i], "mouseover", menuOver)
      addEvent(li_arr[i], "mouseout", menuOut)
    
      ul.style.display='block';
      ul.style.width='';

      ul.style.fontWeight='bold';
      ul_width = ul.offsetWidth;
      ul_width += 30;
      ul.style.fontWeight='normal';

      ul.style.width = ul_width+'px';

      ul_x = checkLeft(ul);
      li_mid = (ul_width-li_width)/2;
      

      if (ul_x-li_mid-menu_x<0) { //left
        ul.style.left = "-"+(ul_x-menu_x)+"px";
      }
      else if((ul_x-li_mid+ul_width)-(menu_width+menu_x)>0) { //right
        ul.style.left = "-"+((ul_x+ul_width)-(menu_width+menu_x))+"px";
      }
      else {
        ul.style.left = "-"+(li_mid+li_margin)+"px";
      }
      ul.style.display = 'none';
    }
    li_width = 0;
    ul_width = 0;
  }
}

function checkLeft(obj){ 
  var curleft = 0;
  if (obj.offsetParent) {
    do {
			curleft += obj.offsetLeft;
    } while (obj = obj.offsetParent);
  }
  else if (obj.x){
    curleft += obj.x;
  }
  return curleft;
}

function topOver(ob) {
  if ((o = ob.getElementsByTagName('ul')[0]) || (o = ob.getElementsByTagName('table')[0])) {
    o.style.display='block';
    ob.style.backgroundColor = '#ede5d1';
  }
}

function topOut(ob) {
  if ((o = ob.getElementsByTagName('ul')[0]) || (o = ob.getElementsByTagName('table')[0])) {
    o.style.display='none';
    ob.style.backgroundColor = '#FBF3DF';
  }
}
