// menu.js
// Contains all the functions and HTML code for the menus

//////////////////////////////////////////////////////////
// SETUP /////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// To install this menu on a page, do the following:
// 1. Place a link to the external Javascript "/include/sniff.js"
//    in the header of the page, *before* any other scripts.
// 2. Place the menu page configuration code anywhere in a
//    Javascript block in the page header. This sets some page-level
//    configuration values that can be customized for each page.
// 3. Place a link to the external Javascript "/include/menu.js"
//    at the bottom of the HTML page, right before the closing
//    </body> tag.


//////////////////////////////////////////////////////////
// CONFIGURATION /////////////////////////////////////////
//////////////////////////////////////////////////////////

// iTotalNavs -- total number of nav sections. This script
//               only supports up to 9 separate nav sections!!!
iTotalNavs = 4;

// iTimer -- number of seconds before unattended open menus close.
//           if zero or null, menus stay open (default).
iTimer = .25;

// sSubnavBgColor -- background color for subnav divs.
sSubnavBgColor = "#aaaacc";

// Navigation Size & Positioning /////////////////////////
// IE and Nav6 are positioned using the values in the arrays
// below, which are inserted as an inline style. Nav4 is positioned
// using a stylesheet that is dynamically written into the page below.

// aSubnavTops -- array of top-positions for subnav items
aSubnavTops = new Array(61,0,0,0);

// iSubnavLeft -- left-position of subnavs (Nav 4.x must be
// set in stylesheet).
iSubnavLeft = 274;


//////////////////////////////////////////////////////////
// INITIALIZE GLOBAL VARIABLES (do not edit) /////////////
//////////////////////////////////////////////////////////

var idOpen // timer id of open menu

// iHilite -- Which nav link should be "on" for this section?
//            If none is specified, use iStart.
if (iHilite == 0) iHilite = iStart;

// bLoaded -- specifies whether the start() function has completed
//            loading and executing.
bLoaded = 0;

// iOpen -- specifies which nav is currently open. Must be initialized
//          before the navs are rendered, else an error may occur.
if (!iOpen) var iOpen = 0;


//////////////////////////////////////////////////////////
// WRITE MENU HTML ///////////////////////////////////////
// This section writes the menu HTML to the page. Make 
// sure to escape any single-quotes with backslashes (/')
// in the document.write statements below.
//////////////////////////////////////////////////////////
// File these under "Reasons to Use Opera"...
// 
// -In Netscape, the nav top-position must be specified in 
//  the style sheet. For some reason, NN won't see the inline
//  style in the below DIVs when a class is also specified in the
//  external stylesheet.
//
// -In IE, if we specify the top-position of the DIVs in the
//  stylesheet, using the IDs, the IE DHTML reports an incorrect
//  pixelTop. Since no inline style specifies top, it reports
//  top as being zero, even though the element is correctly
//  positioned on the page. So we need to specify top using
//  inline styles in each nav DIV.
//
// So...
//  For IE/Nav6, we'll continue to put the inline style in the DIV tag
//  with the top position. For NN, we'll write an internal stylesheet
//  below.
//////////////////////////////////////////////////////////

function inStyleSub(px) {
  // if IE, write inline style specifying top-position
  if (is_ie4up || is_gecko) {
    var toReturn
    toReturn =  ' style="position: absolute; top: ' + px + 'px; left: '+iSubnavLeft+'px; padding:5px; margin:0px; ';
    toReturn += ' width: 200px; z-index: 99;';
    toReturn += ' font-size: 11px; width: 132px; visibility: hidden;"';
    return toReturn;
  }
}

// Instead of an external stylesheet, write the styles to the page FOR NAV4 ONLY.
// IE/Nav6 can use the inline styles created by the function above.

if (is_nav4) {
  document.write('<STYLE TYPE="text/css">\n');
  document.write('.subnav {                                           ');
  document.write('  position: absolute; width: 132px; left: ' + iSubnavLeft + 'px;    ');
  document.write('  padding: 0px; margin: 0px; border: none;          ');
  document.write('  font-size: 11px; font-family: arial;              ');
  document.write('  z-index: 99; visibility: hidden;                  ');
  document.write('}\n                                                 ');
  
  // start a loop to write an ID style for each subnav section
  for (var i=1; i<=iTotalNavs; i++) {
    document.write('#subnav' + i + ' { position: absolute; top: ' + aSubnavTops[i-1] + 'px; }\n       ');
  }
  
  document.write('</STYLE>\n\n\n');
}

// Write Subnav 1 elements to page ///////////////////////////////
document.write('  <div id="subnav1" class="subnav" ' + inStyleSub(aSubnavTops[0]) + '>');
document.write('    <img src="/images/spacer.gif" width="256" height="4" alt=""><br />');
document.write('    <a href="programs1.htm"');
document.write('    		onmouseover="changeImages(\'hsubnav_r1\', \'images/hsubnav_r1_over.gif\'); showNav(1); return true;"');
document.write('    		onmouseout="changeImages(\'hsubnav_r1\', \'images/hsubnav_r1.gif\'); hideNav(1); return true;">');
document.write('    		<img name="hsubnav_r1" src="images/hsubnav_r1.gif" width="256" height="13" alt="neuropsychology service" border="0" /></a><br />');
document.write('    <a href="programs2.htm"');
document.write('    		onmouseover="changeImages(\'hsubnav_r2\', \'images/hsubnav_r2_over.gif\'); showNav(1); return true;"');
document.write('    		onmouseout="changeImages(\'hsubnav_r2\', \'images/hsubnav_r2.gif\'); hideNav(1); return true;">');
document.write('    		<img name="hsubnav_r2" src="images/hsubnav_r2.gif" width="256" height="13" border="0" alt="inpatient psychiatric CL service" /></a><br />');
document.write('    <a href="programs3.htm"');
document.write('    		onmouseover="changeImages(\'hsubnav_r3\', \'images/hsubnav_r3_over.gif\'); showNav(1); return true;"');
document.write('    		onmouseout="changeImages(\'hsubnav_r3\', \'images/hsubnav_r3.gif\'); hideNav(1); return true;">');
document.write('    		<img name="hsubnav_r3" src="images/hsubnav_r3.gif" width="256" height="13" border="0" alt="inpatient psychiatric nursing CL service" /></a><br />');
document.write('    <a href="programs4.htm"');
document.write('    		onmouseover="changeImages(\'hsubnav_r4\', \'images/hsubnav_r4_over.gif\'); showNav(1); return true;"');
document.write('    		onmouseout="changeImages(\'hsubnav_r4\', \'images/hsubnav_r4.gif\'); hideNav(1); return true;">');
document.write('    		<img name="hsubnav_r4" src="images/hsubnav_r4.gif" width="256" height="13" border="0" alt="psychosocial medicine outpatient service" /></a><br />');
document.write('    <a href="programs5.htm"');
document.write('    		onmouseover="changeImages(\'hsubnav_r5\', \'images/hsubnav_r5_over.gif\'); showNav(1); return true;"');
document.write('    		onmouseout="changeImages(\'hsubnav_r5\', \'images/hsubnav_r5.gif\'); hideNav(1); return true;">');
document.write('    		<img name="hsubnav_r5" src="images/hsubnav_r5.gif" width="256" height="13" border="0" alt="emergency department case management program" /></a><br />');
document.write('    <a href="programs6.htm"');
document.write('    		onmouseover="changeImages(\'hsubnav_r6\', \'images/hsubnav_r6_over.gif\'); showNav(1); return true;"');
document.write('    		onmouseout="changeImages(\'hsubnav_r6\', \'images/hsubnav_r6.gif\'); hideNav(1); return true;">');
document.write('    		<img name="hsubnav_r6" src="images/hsubnav_r6.gif" width="256" height="13" border="0" alt="trauma recovery and rape treatment center" /></a><br />');
document.write('    <a href="programs7.htm"');
document.write('    		onmouseover="changeImages(\'hsubnav_r7\', \'images/hsubnav_r7_over.gif\'); showNav(1); return true;"');
document.write('    		onmouseout="changeImages(\'hsubnav_r7\', \'images/hsubnav_r7.gif\'); hideNav(1); return true;">');
document.write('    		<img name="hsubnav_r7" src="images/hsubnav_r7.gif" width="256" height="13" border="0" alt="primary case psychiatry service -  maxine hall" /></a><br />');
document.write('    <a href="programs8.htm"');
document.write('    		onmouseover="changeImages(\'hsubnav_r8\', \'images/hsubnav_r8_over.gif\'); showNav(1); return true;"');
document.write('    		onmouseout="changeImages(\'hsubnav_r8\', \'images/hsubnav_r8.gif\'); hideNav(1); return true;">');
document.write('    		<img name="hsubnav_r8" src="images/hsubnav_r8.gif" width="256" height="13" border="0" alt="psychosocial medicine clinical services research" /></a><br />');
document.write('    <img src="/images/spacer.gif" width="256" height="4" alt=""><br />');
document.write('  </div>');

// Write Subnav 2 elements to page ///////////////////////////////
document.write('  <div id="subnav2" class="subnav"' + inStyleSub(aSubnavTops[1]) + '>');
document.write('  </div>');

// Write Subnav 3 elements to page ///////////////////////////////
document.write('  <div id="subnav3" class="subnav"' + inStyleSub(aSubnavTops[2]) + '>');
document.write('  </div>');

// Write Subnav 4 elements to page ///////////////////////////////
document.write('  <div id="subnav4" class="subnav"' + inStyleSub(aSubnavTops[3]) + '>');
document.write('  </div>');


//////////////////////////////////////////////////////////
// Pre-load rollover images //////////////////////////////
// There should be an entry below for every rollover
// image on the page.
//////////////////////////////////////////////////////////

if (document.images) {
  nav01 = new Image();
    nav01.src = "/images/hsubnav_r1_over.gif";
  nav02 = new Image();
    nav02.src = "/images/hsubnav_r2_over.gif";
  nav03 = new Image();
    nav03.src = "/images/hsubnav_r3_over.gif";
  nav04 = new Image();
    nav04.src = "/images/hsubnav_r4_over.gif";
  nav05 = new Image();
    nav05.src = "/images/hsubnav_r5_over.gif";
  nav06 = new Image();
    nav06.src = "/images/hsubnav_r6_over.gif";
  nav07 = new Image();
    nav07.src = "/images/hsubnav_r7_over.gif";
  nav08 = new Image();
    nav08.src = "/images/hsubnav_r8_over.gif";

}


////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS (Do not edit) /////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function hideNav(iWhich) {
  //alert('hiding nav');
  // put this on timer
  if (iTimer) {
    idOpen = window.setTimeout('hideNavDo('+iWhich+');', (iTimer*1000));
  } else {
    hideNavDo(iWhich);
  }
}

function hideNavDo(iWhich) {
  // closes the specified subnav panel and moves the trailing 
  // navs up to fill the space.
  // GSC 07/11/01: Code fork updated to support Nav6

  if (bLoaded) {
    // hide subnav
    sSubnav = "subnav" + iWhich;
    if (is_nav4) { document.layers[sSubnav].visibility = "hide"; }
    else if (is_ie4up) { document.all(sSubnav).style.visibility = "hidden"; }
    else { document.getElementById(sSubnav).style.visibility = "hidden"; }
  
    // turn off navlink image
    // (but only if it's not the one for the current page)
    if (iWhich != iHilite) {
      var sImg = "nav_r" + iWhich;
      var sImgSrc = document[sImg].src;
      var sImgSrcNew = sImgSrc.replace(/_over/,"");
      document[sImg].src = sImgSrcNew;
    }
    iOpen = "";
  }
}

function showNav(iWhich) {
  //alert('showing nav');
  // cancels any hideNav timer and immediately hides prev nav
  if (iOpen) {
    window.clearTimeout(idOpen);
    if (iOpen != iWhich) hideNavDo(iOpen);
  }
  showNavDo(iWhich);
}

function showNavDo(iWhich) {
  // opens the specified subnav panel
  // but only if there's not one already open and page is loaded
  
  if (!iOpen && bLoaded) {
    // show subnav
    sSubnav = "subnav" + iWhich;
    if (is_nav4) { document.layers[sSubnav].visibility = "show"; } 
    else if (is_ie4up) { document.all(sSubnav).style.visibility = ""; } 
    else { document.getElementById(sSubnav).style.visibility = "visible"; }
  
    // turn on navlink image
    sImg = "nav_r" + iWhich;
    sImgSrc = document[sImg].src;
    
    if (sImgSrc.indexOf("_over") == -1) {
      sImgSrcNew = sImgSrc.substr(0,sImgSrc.lastIndexOf(".")) + "_over.gif";  
      document[sImg].src = sImgSrcNew;
    }
    
    iOpen = iWhich;
  }
}

function start() {
  //iOpen = iStart;
  
  // whichever section we're in when the page loads needs to
  // be hilighted, and shouldn't turn off on mouseout.
  if (iHilite) {
  
    sOn = "nav_r" + iHilite; // image name
    sOnImg = document[sOn].src; // image "off" state url
    
    // parse off the file extension so we can add the "_over" suffix
    i = sOnImg.lastIndexOf(".");
    ext = sOnImg.substring(i,sOnImg.length);
    sOnImg = sOnImg.substring(0,i) + "_over" + ext;
    
    // swap images
    document[sOn].src = sOnImg;
    
  }
  
  // set completion flag
  bLoaded = 1;
}

function navSwap(sWhere,sWhich,sNav) {
  // this is the basic swap() image swapper function, but modified
  // to check which nav image should be "on" before turning it off.
  // also verify everything is loaded before allowing any functions
  // to function.
  if (document.images && sNav != iHilite && bLoaded) {
    document[sWhere].src = sWhich;
  }
}


