//  - set onmouseover for top level menu links to topmenu_over
//  - set onmouseout for top level menu links to topmenu_out

// variable definitions
var menu_visible      = false; // may be set to name of visible menu
var menu_timer        = false; // stores timer id for delay_hide_menu
var menu_delay        = 500; // ms until menu is hidden after mouseout
var buttonheight      = 25; // height of mainnav buttons (used in up/down pos) 

var menu_offset_ie_x  = 0;  //--
var menu_offset_wie_x = 0;  //  |
var menu_offset_ns_x  = 0;  // NOTE: customize these values inside
var menu_offset_ns_y  = 0;  // of the init_offsets function below! Not here!
var menu_offset_ie_y  = 0;  //  |
var menu_offset_wie_y = 0;  //  |
var menu_offset_moz_x = 0;  //  |
var menu_offset_moz_y = 0;  //--

/* customize offesets for menus/dropdown here */
function init_offsets(otype) {
  if( otype == "menus" ) {
    /* offsets for menus */
    menu_offset_ie_x  = 0;    // silly browser-specific values
    menu_offset_wie_x = 0;    // IE/Windows x
    /* menu_offset_ns_x  = 155;    // NS x */
    menu_offset_ns_x  = 0;    // NS x
    /* menu_offset_ns_y  = -6;      // NS y */
    menu_offset_ns_y  = 30;      // NS y
    menu_offset_ie_y  = 30;      // IE y
    menu_offset_wie_y = 30;      // IE/Windows y
    menu_offset_moz_x = 0;    // Mozilla
    menu_offset_moz_y = 30;
  } else {
    /* offsets for dropdown */
    menu_offset_ie_x  = 0;      // silly browser-specific values
    menu_offset_wie_x = 0;      // IE/Windows x
    menu_offset_ns_x  = 0;      // NS x
    /* menu_offset_ns_y  = 12;      // NS y */
    menu_offset_ns_y  = 18;     // NS y
    menu_offset_ie_y  = 18;     // IE y
    menu_offset_wie_y = 18;     // IE/Windows y
    menu_offset_moz_x = 0;      // Mozilla
    menu_offset_moz_y = 18;
  }
}

// by default, initialize for menus
init_offsets("menus");

var placeholder_x, placeholder_y = 0;

/* Macromedia's Netsape Resize Fix */
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function dropdown_over(m) {
        // re-init offsets for dropdown
        init_offsets("dropdown");
        // do the over, like the others
        label_over(m);
        // re-init offsets for menus
        init_offsets("menus");
}

function dropdown_out(m) {
        // re-init offsets for dropdown
        init_offsets("dropdown");
        // do the out, like the others
        label_out(m);
        // re-init offsets for menus
        init_offsets("menus");
}

function submenuitem_over(m) {
        submenu_split = m.split("_");
        current_menu = submenu_split[0];
        menuitem_over(current_menu);
        imgOn("ph_"+current_menu);
}

function submenuitem_out(m) {  
        submenu_split = m.split("_");
        current_menu = submenu_split[0];
        menuitem_out(current_menu);
}

function rollOver(m) {
	imgOn("ph_"+m);
}

function rollOut(m) {
	imgOff("ph_"+m);
}

function label_over(m) {
 stop_menu_timer();
 if(menu_visible) {
  hide_menu(menu_visible);
 }
 imgOn("ph_"+m);
 align_menu_from_bottom(m);
 show_menu(m);
}

function label_out(m) {
 delay_hide_menu(m);
}

function menu_over(m) {
 stop_menu_timer();
}

function menu_out(m) {
 delay_hide_menu(m);
}

function menuitem_over(m) {
 stop_menu_timer();
}

function menuitem_out(m) {
 delay_hide_menu(m);
}

function delay_hide_menu(m) {
 stop_menu_timer();
 menu_timer = setTimeout("hide_menu(\""+m+"\");imgOff(\"ph_"+m+"\");", menu_delay);
}

function stop_menu_timer() {
 if(menu_timer > 0) {
  clearTimeout(menu_timer);
  menu_timer = 0;
 }
}

function show_menu(m) {
 // hide any other menus that are visible
 if(menu_visible > 0) {
  hide_menu(menu_visible);
 }
 show_layer(m+"menu");
 menu_visible = m;
}

function hide_menu(m) {
 hide_layer(m+"menu");
 imgOff("ph_"+m);
 menu_visible = false;
}

var winh = 0;
function get_window_height() {
 if(window.innerHeight) {
  winh = window.innerHeight;
  if(window.pageYOffset) {
   winh += window.pageYOffset;
  }
 } else {
  winh = document.body.clientHeight + document.body.scrollTop;
 }
 return winh;
}

var menuh = 0;
function get_menu_height(m) {
 if(document.getElementById) {
  if( document.getElementById(m+"menu").offsetHeight) {
    menuh = document.getElementById(m+"menu").offsetHeight;
  }
  else {
    menuh = document.getElementById(m+"menu").scrollHeight;
  }
 } else if(document[m+"menu"].style) {
  menuh = document[m+"menu"].style.clientHeight;
 } else if(document.layers) {
  // this isn't working yet
  menuh = document.layers[m+"menu"].clip.height;
 }
 return menuh;
}

var myy = 0;
function calculate_bottom_diff(wh, mh, py) {
 if((py + mh) < wh) {
  myy = 0;
 } else if((py + mh) > wh) {
  myy = -(mh - (wh - py));
 } else if((py + mh) == wh) {
  myy = -15; // or some negative offset to give a buffer
 } else {
  myy = -15; // or whatever
 }
 return myy;
}

function align_menu_from_bottom(m) {  // Need to deal with changes here, 3/14/04
 // need to get the location of the placeholder image
 placeholder_x = 0;
 placeholder_y = 0;
 // and the window height and menu height
 winh = get_window_height();
 menuh = get_menu_height(m);
 //
 var y_offset = 0;
 if(document.getElementById && (navigator.appVersion.indexOf("MSIE") != -1)) {
  // MSIE
  var current = document.getElementById('ph_'+m);
  var menu = document.getElementById(m+"menu");
  while(current.offsetParent != null) {
   placeholder_x += current.offsetLeft;
   placeholder_y += current.offsetTop;
   current = current.offsetParent;
  }
  // position the menu
        if(navigator.appVersion.indexOf("Win") > 1) {
   placeholder_x += menu_offset_wie_x;
   placeholder_y += menu_offset_wie_y;
         y_offset = menu_offset_wie_y;
  } else {
   placeholder_x += menu_offset_ie_x;
   placeholder_y += menu_offset_ie_y;
         y_offset = menu_offset_ie_y;
  }
  // work in y offsets based on height of window, height of menu
  placeholder_y += calculate_bottom_diff(winh, menuh, placeholder_y);
  menu.style.left = placeholder_x +"px";
  menu.style.top = placeholder_y  +"px";
 } else if(document.getElementById && (navigator.userAgent.indexOf("Gecko") != -1)) {
  // Mozilla/NS6-7
  winh = get_window_height();
  menuh = get_menu_height(m);
  current = document.getElementById('ph_'+m);
  var menu = document.getElementById(m+"menu");
  while(current.offsetParent != null) {
   placeholder_x += current.offsetLeft;
   placeholder_y += current.offsetTop;
   current = current.offsetParent;
  }
  placeholder_x += menu_offset_moz_x;
  placeholder_y += menu_offset_moz_y;
  placeholder_y += calculate_bottom_diff(winh, menuh, placeholder_y);
  menu.style.top = placeholder_y +"px";
  menu.style.left = placeholder_x +"px";
 } else if(document.all) {
  // MSIE <5
  var current = eval('document.images["ph_'+m+'"]');
  while(current.offsetParent != null) {
   placeholder_x += current.offsetLeft;
   placeholder_y += current.offsetTop;
   current = current.offsetParent;
  }
  // position the menu
        if(navigator.appVersion.indexOf("Win") > 1) {
   placeholder_x += menu_offset_wie_x;
   placeholder_y += menu_offset_wie_y;
         y_offset = menu_offset_wie_y;
  } else {
   placeholder_x += menu_offset_ie_x;
   placeholder_y += menu_offset_ie_y;
         y_offset = menu_offset_ie_y;
  }
  placeholder_y += calculate_bottom_diff(winh, menuh, placeholder_y);
  eval('document.all.'+m+'menu.style.left = '+placeholder_x);
  eval('document.all.'+m+'menu.style.top = '+placeholder_y);
 } else { //NS4
  placeholder_x = eval('document.images["ph_'+m+'"].x');
  placeholder_y = eval('document.images["ph_'+m+'"].y');
  placeholder_x += menu_offset_ns_x;
  placeholder_y += menu_offset_ns_y;
  placeholder_y += calculate_bottom_diff(winh, menuh, placeholder_y);
  eval('document.layers.'+m+'menu.moveTo(placeholder_x, placeholder_y)');
 }
}

function show_layer(l) {
 var tmp = '';
 if(document.getElementById) {
  document.getElementById(l).style.visibility = "visible";
  return;
 } else if(document.all) {
  tmp = 'document.all.'+l+'.style';
 } else {
  tmp = 'document.'+l;
 }
 eval(tmp+'.visibility="visible";');
}

function hide_layer(l) {
 var tmp = '';
 if(document.getElementById) {
  document.getElementById(l).style.visibility = "hidden";
  return;
 } else if(document.all) {
  tmp = 'document.all.'+l+'.style';
 } else {
  tmp = 'document.'+l;
 }
 eval(tmp+'.visibility="hidden";');
}
