//// Navigation menu code for life solutions ////
//

// array of navigation item base names...
var arrMenu = new Array("Home", "Therps", "Kids", "Slim", "Stress", "More", "About");
// index of currently open menu or -1 if none open...
var ndxOpen = -1;
// prefixes for navigation items and drop down menus...
var prefixMenu = "mnu";
var prefixNav = "nav";
var prefixPic = "pic";
// menu configuration and working vars...
var menuOffsetX = 0;
var menuOffsetY = 1;
var navTimer;         // timer for menu hiding
var navDelay = 350;   // milliseconds before drop menu closes

/////////////////////////////////////////////////////////
//
// pre-cache link and rollover images...
//
var imgsLo = new Object();      // resting buttons
var imgsHi = new Object();      // mouse over button

if (document.images)
  {
  imgsLo["picHome"] = new Image(62, 48);   // nav images - normal
  imgsLo["picHome"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_home_mon.jpg";
  imgsLo["picTherps"] = new Image(62, 48);
  imgsLo["picTherps"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_therps_mon.jpg";
  imgsLo["picKids"] = new Image(62, 48);
  imgsLo["picKids"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_kids_mon.jpg";
  imgsLo["picSlim"] = new Image(62, 48);
  imgsLo["picSlim"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_slim_mon.jpg";
  imgsLo["picStress"] = new Image(62, 48);
  imgsLo["picStress"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_stress_mon.jpg";
  imgsLo["picMore"] = new Image(62, 48);
  imgsLo["picMore"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_arches_mon.jpg";
  imgsLo["picAbout"] = new Image(62, 48);
  imgsLo["picAbout"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_canda_mon.jpg";

  imgsHi["picHome"] = new Image(62, 48);   // nav images - normal
  imgsHi["picHome"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_home_clr.jpg";
  imgsHi["picTherps"] = new Image(62, 48);
  imgsHi["picTherps"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_therps_clr.jpg";
  imgsHi["picKids"] = new Image(62, 48);
  imgsHi["picKids"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_kids_clr.jpg";
  imgsHi["picSlim"] = new Image(62, 48);
  imgsHi["picSlim"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_slim_clr.jpg";
  imgsHi["picStress"] = new Image(62, 48);
  imgsHi["picStress"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_stress_clr.jpg";
  imgsHi["picMore"] = new Image(62, 48);
  imgsHi["picMore"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_arches_clr.jpg";
  imgsHi["picAbout"] = new Image(62, 48);
  imgsHi["picAbout"].src = "http://www.lifesolutions.org.uk/common/imgs/nav_canda_clr.jpg";
  }

function setImage(sID, state)
  {
  name = prefixPic + sID;
  if (document.images)
    {
    if (state == 'hi')
      {
      document.images[name].src = imgsHi[name].src;
      return true;
      }
    else if (state == 'lo')
      {
      document.images[name].src = imgsLo[name].src;
      return true;
      }
    }
  return false;
  }

///////////////////////////////////////////////
// openMenu()
//
// Drops open a menu and marks it as open
// sID: string: the base ID name of the menu item to open
//
function openMenu(sID)
  {
  //alert("openMenu()");
  // check the menu exists...
  var sNavID = prefixNav + sID;
  var sMenuID = prefixMenu + sID;
  var nMenu = getMenuIndex(sID);
  //dbg alert("openMenu('" + sID + "'), sNavID='" + sNavID + "', sMenuID='" + sMenuID + "', nMenu=" + nMenu);
  if (nMenu < 0 || arrMenu[nMenu] == "")
    return;

  // highlight nav item (rollover)...
  setImage(sID, 'hi');

  var elemNav = document.getElementById(sNavID);
  var elemMenu = document.getElementById(sMenuID);
  var elemPage = document.getElementById("page");
  if (elemPage && elemNav && elemMenu)
    {
    // cancel any pending menu closures...
    clearTimeout(navTimer);
    // hide any other drop down menus...
    shutAllMenus();
    // drop this menu...
    var nPageRight = findPosX(elemPage) + elemPage.offsetWidth;
    var nMenuLeft = findPosX(elemNav) + menuOffsetX;
    elemMenu.style.display = "block";
    var nOverhang = nMenuLeft + elemMenu.offsetWidth - nPageRight;
    if (nOverhang > 0)
      nMenuLeft -= nOverhang;
    elemMenu.style.position = "absolute";
    elemMenu.style.left = nMenuLeft + "px";
    elemMenu.style.top = findPosY(elemNav)+ elemNav.offsetHeight + menuOffsetY + "px";
    // and mark it as open...
    ndxOpen = nMenu;
    }
  return true;   // allows browser to display link target
  }

///////////////////////////////////////////////
// keepMenuOpen()
//
// Marks a previously closed (but still droppped down)
// menu as open and resets the menu display property
// to ensure it stays displayed.  Used when the mouse
// moves from a nav item down to its dropped menu.
// sID: string: the base ID name of the menu item to open
//
function keepMenuOpen(sID)
  {
  var sMenuID = prefixMenu + sID;
  var nMenu = getMenuIndex(sID);
  if (nMenu < 0 || arrMenu[nMenu] == "")
    return;
  ndxOpen = nMenu;
  var elem = document.getElementById(sMenuID);
  elem.style.display = "block";
  }

///////////////////////////////////////////////
// closeMenu()
//
// Schedules the raising of a dropped menu.  Used
// to allow menu to remain open while mouse is moved
// from nav item to a menu just dropped and to cope
// with minor mouse movement errors
// sID: string: the base ID name of the menu item to close
//
function closeMenu(sID)
  {
  var sMenuID = prefixMenu + sID;
  var nMenu = getMenuIndex(sID);
  
  //dbg alert("closeMenu("+sMenuID+")");
  
  if (nMenu < 0 || arrMenu[nMenu] == "")
    return;

  // un-highlight nav item (rollover)...
  setImage(sID, 'lo');

  ndxOpen = -1;
  // schedule menu closure...
  navTimer = setTimeout("hideClosedMenu(" + nMenu + ")", navDelay);
  }

///////////////////////////////////////////////
// hideClosedMenu()
//
// Internal function to raise a dropped menu.
// Used via setTimeout() to delay the raising of a menu
// for a short while after a mouseout event closes it.
// nMenu: int: the array index of the menu item to close
//
function hideClosedMenu(nMenu)
  {
  if (ndxOpen == -1)
    {
    var elem = document.getElementById(prefixMenu + arrMenu[nMenu]);
    if (elem)
      elem.style.display = "none";
    }
  }

///////////////////////////////////////////////
// shutMenu()
//
// Raises a dropped menu immediately and marks it closed.
// Used to close a menu before opening another one.
// sID: string: the base ID name of the menu item to close
//
function shutMenu(sID)
  {
  var sMenuID = prefixMenu + sID;
  var nMenu = getMenuIndex(sID);
  if (nMenu < 0 || arrMenu[nMenu] == "")
    return;
  ndxOpen = -1;
  var elem = document.getElementById(sMenuID)
  if (elem)
    elem.style.display = "none";
  }

///////////////////////////////////////////////
// shutAllMenus()
//
// Close open menus before opening another one.
//
function shutAllMenus()
  {
  var elemMenu;
  for (var i in arrMenu)
    {
    if (arrMenu[i] != "")
      {
      //dbg alert("arrMenu["+i+"]="+arrMenu[i]);
      elemMenu = document.getElementById(prefixMenu + arrMenu[i]);
      elemMenu.style.display = "none";
      }
    }
  ndxOpen = -1;
  }

///////////////////////////////////////////////
// getMenuIndex()
//
// Internal function to calc index of a specified
// base ID name.
// sID: string: base ID name of menu to find
//
function getMenuIndex(sID)
  {
  for (var i in arrMenu)
    {
    if (arrMenu[i] == sID)
    return i;
    }
  // if we're here we didn't find the menu...
  return -1;
  }

// functions to find the position of an element...
// updated 2/9/2007, MT
// fixes IE offsetParent problem
function findPosX(obj)
  {
  var curleft = obj.offsetLeft;
  while (obj.offsetParent)
    {
    obj = obj.offsetParent;
    curleft += obj.offsetLeft;
    }
  return curleft;
  }

function findPosY(obj)
  {
  var curtop = obj.offsetTop;
  while (obj.offsetParent)
    {
    obj = obj.offsetParent;
    curtop += obj.offsetTop;
    }
  return curtop;
  }


//
///// End of File ///////////////////////////////////////
