var timeOut = null, numMenus = 0;
var menus = new Object ( );	//stores the menu data itself

var ns4 = false;		// true if the browser supports layers
var ie4 = false;		// true if the browser supports document.all
var otherDOM = false;	// true if ns6
var ie5plus = false;		// true if browser is IE 5+
var adjustSpacing = false; 	// true if we need to adjust menu spacing
var activeMenuHeading = -1;	// holds number of the active menu (heading)

function detectBrowser( ) {
  if (document.layers)
    ns4 = true;
  else if (document.all) {
    ie4 = true;
    if ((( navigator.userAgent.indexOf("MSIE 5.5") == -1) && 
         ( navigator.userAgent.indexOf("MSIE 6") == -1)) ||
         ( navigator.userAgent.indexOf("Mac") != -1))
      adjustSpacing = true;
  }
  else if (document.getElementById) {
    otherDOM = true;
    adjustSpacing = true;
  }
}

// ***************************************************************************

function showMenu (which, x, y) {
// accepts an integer indicating which menu to show and x/y if not ns4
  if (ns4) {
    document.layers["menuHeading" + which].bgColor = "#666600";
    document.layers["menu" + which].visibility = "show";
    document.layers["menu" + which].moveAbove(document.layers["mainContent"]);
  }
  else if (ie4) {
    document.all("menu" + which).style.left = x;
    document.all("menu" + which).style.top = y;
    document.all("menu" + which).style.visibility = "visible";
  }
  else if (otherDOM) {
    document.getElementById("menu" + which).style.left = x;
    document.getElementById("menu" + which).style.top = y;
    document.getElementById("menu" + which).style.visibility = "visible";
  }
}

// ***************************************************************************

function hideMenu(which) {
//accepts an integer indicating which menu to hide
  if (ns4) {
    document.layers["menuHeading" + which].bgColor = "#999966";
    document.layers["menu" + which].visibility = "hide";
    document.layers["menu" + which].moveBelow(document.layers["mainContent"]);
  }
  else if (ie4) {
    document.all("menu" + which).style.visibility = "hidden";
  }
  else if (otherDOM) {
    document.getElementById("menu" + which).style.visibility = "hidden";
  }
}

// ***************************************************************************

function addItem (menuName, item, action)
{
  if ( ! menus[menuName] )  {
    var newMenu = new Object ( );	//create a new object for it
    newMenu.name = menuName;	//sets its name property
    newMenu.num = numMenus++;	//sets its menu number

    //create the arrays to hold item/action pairs
    newMenu.items = new Array ( );
    newMenu.actions = new Array ( );
  
    //and add the new menu to the menus associative array
    menus[menuName] = newMenu;
  }

// add the new items to the end of the menu and associate action
menus[menuName].items[menus[menuName].items.length] = item;
menus[menuName].actions[menus[menuName].actions.length] = action; 
}

addItem("  About Us ", "About Us", "http://www.integrativepsychology.net/AboutUs/index.htm");
addItem("  About Us ", "Mission Statement", "http://www.integrativepsychology.net/AboutUs/index.htm#mission");
addItem("  About Us ", "Members", "http://www.integrativepsychology.net/AboutUs/index.htm#members");
addItem("  About Us ", "Alliant International University", "http://www.integrativepsychology.net/AboutUs/index.htm#AIU");
addItem("  About Us ", "San Diego, CA", "http://www.integrativepsychology.net/AboutUs/sandiego.htm");

addItem(" Graduate Program ", "Graduate Programs", "http://www.integrativepsychology.net/GradProg/index.htm");
addItem(" Graduate Program ", "Ph.D. & Psy.D.", "http://www.integrativepsychology.net/GradProg/index.htm#PhD");
addItem(" Graduate Program ", "Masters", "http://www.integrativepsychology.net/GradProg/index.htm#masters");

addItem(" Integrative Fields ", "Integrative Fields", "http://www.integrativepsychology.net/IntFields/index.htm");
addItem(" Integrative Fields ", "Psychotherapies", "http://www.integrativepsychology.net/IntFields/psyTherap.htm");
addItem(" Integrative Fields ", "Health & Medicine", "http://www.integrativepsychology.net/IntFields/health.htm");
addItem(" Integrative Fields ", "Cultural Studies", "http://www.integrativepsychology.net/IntFields/cultStud.htm");
addItem(" Integrative Fields ", "Transpersonal Psychology", "http://www.integrativepsychology.net/IntFields/transPsy.htm");
addItem(" Integrative Fields ", "Humanistic Psychology", "http://www.integrativepsychology.net/IntFields/humPsy.htm");
addItem(" Integrative Fields ", "Jungian Psychology", "http://www.integrativepsychology.net/IntFields/junPsy.htm");
addItem(" Integrative Fields ", "Systems Theory", "http://www.integrativepsychology.net/IntFields/sysTheo.htm");
addItem(" Integrative Fields ", "Ecology", "http://www.integrativepsychology.net/IntFields/ecology.htm");
addItem(" Integrative Fields ", "Spirituality", "http://www.integrativepsychology.net/IntFields/spirit.htm");
addItem(" Integrative Fields ", "Philosophy", "http://www.integrativepsychology.net/IntFields/philo.htm");
addItem(" Integrative Fields ", "Evolution", "http://www.integrativepsychology.net/IntFields/evolution.htm");
addItem(" Integrative Fields ", "Consciousness", "http://www.integrativepsychology.net/IntFields/conscious.htm");
addItem(" Integrative Fields ", "Positive Psychology", "http://www.integrativepsychology.net/IntFields/posPsy.htm");
addItem(" Integrative Fields ", "Philosophy of Science", "http://www.integrativepsychology.net/IntFields/sciPhil.htm");
addItem(" Integrative Fields ", "Ethics", "http://www.integrativepsychology.net/IntFields/philo.htm#ethics");
addItem(" Integrative Fields ", "Parapsychology", "http://www.integrativepsychology.net/IntFields/paraPsy.htm");

addItem(" Integrative Links ", "Integrative Links", "http://www.integrativepsychology.net/IntLinks/index.htm");
addItem(" Integrative Links ", "Wisdom", "http://www.integrativepsychology.net/IntLinks/wisdom.htm");
addItem(" Integrative Links ", "Institutes & Universities", "http://www.integrativepsychology.net/IntLinks/institutes.htm");
addItem(" Integrative Links ", "Organizations & Societies", "http://www.integrativepsychology.net/IntLinks/orgSoc.htm");
addItem(" Integrative Links ", "Philosophy of Science", "http://www.integrativepsychology.net/IntFields/sciPhil.htm");
addItem(" Integrative Links ", "Retreats", "http://www.integrativepsychology.net/IntLinks/retreats.htm");
addItem(" Integrative Links ", "Clinics", "http://www.integrativepsychology.net/IntLinks/clinics.htm");
addItem(" Integrative Links ", "Research", "http://www.integrativepsychology.net/IntLinks/research.htm");
addItem(" Integrative Links ", "Community Programs", "http://www.integrativepsychology.net/IntLinks/comProg.htm");
addItem(" Integrative Links ", "People", "http://www.integrativepsychology.net/IntLinks/people.htm");
addItem(" Integrative Links ", "News Discussion Groups", "http://www.integrativepsychology.net/IntLinks/news.htm");
addItem(" Integrative Links ", "Media", "http://www.integrativepsychology.net/IntLinks/media.htm");
addItem(" Integrative Links ", "Quotes, Parables, Humor", "http://www.integrativepsychology.net/IntLinks/quotes.htm");
addItem(" Integrative Links ", "Discussion Groups Online", "http://www.integrativepsychology.net/IntLinks/groups.htm");

addItem(" Psychology Links ", "Psychology Links", "http://www.integrativepsychology.net/PsycLinks/index.htm");
addItem(" Psychology Links ", "History of Psychology", "http://www.integrativepsychology.net/PsycLinks/history.htm");
addItem(" Psychology Links ", "Psychology Mega-Sites", "http://www.integrativepsychology.net/PsycLinks/megaSites.htm");
addItem(" Psychology Links ", "Organizations & Societies", "http://www.integrativepsychology.net/PsycLinks/orgSoc.htm");
addItem(" Psychology Links ", "Psychological Science", "http://www.integrativepsychology.net/PsycLinks/psyScien.htm");
addItem(" Psychology Links ", "Statistical Software", "http://www.integrativepsychology.net/PsycLinks/statSoft.htm");
addItem(" Psychology Links ", "Fields of Psychology", "http://www.integrativepsychology.net/PsycLinks/fields.htm");
addItem(" Psychology Links ", "Academic Departments", "http://www.integrativepsychology.net/PsycLinks/dept.htm");
addItem(" Psychology Links ", "Journals, Articles, Books", "http://www.integrativepsychology.net/PsycLinks/jourPub.htm");
addItem(" Psychology Links ", "Self-Help Resources", "http://www.integrativepsychology.net/PsycLinks/selfHelp.htm");
addItem(" Psychology Links ", "Psychology News", "http://www.integrativepsychology.net/PsycLinks/psyNews.htm");
addItem(" Psychology Links ", "Funding Agencies/Grants", "http://www.integrativepsychology.net/PsycLinks/fundAgen.htm");
addItem(" Psychology Links ", "Discussion Groups", "http://www.integrativepsychology.net/PsycLinks/discusGrp.htm");
addItem(" Psychology Links ", "Other Sites of Interest", "http://www.integrativepsychology.net/PsycLinks/interest.htm");
 
// ***************************************************************************
function createMenuHeadings( )
{  // generates the menu bar
  if (ns4) {
    document.write('<layer name="background" bgcolor="#999966" width="95%" z-index="0" top="10" left="10" height="21"> &nbsp; </layer>');
  }
  else if (ie4 || otherDOM) {
    document.write('<div class="menuBar">');
  }
  for (var menu in menus) {
    var theMenu = menus[menu];

    if (ns4) 
      document.write('<layer name="menuHeading' + theMenu.num + '" onmouseout="timeOut = setTimeout(\'hideMenu(' + theMenu.num + ')\', 500)" width="80" bgcolor="#999966" top="10" height="21" left="' + ((theMenu.num * 100) + 10) + '" z-index="20"><a class="menuHeadingNS4" href="javascript:showMenu(' + theMenu.num + ')">' + theMenu.name + '</a></layer>');
    else if (ie4 || otherDOM)
      document.write('<a onclick="menuHeadingMouseClick(event, ' + theMenu.num + '); return false" onmouseout="menuHeadingMouseOut(' + theMenu.num + ')" onmouseover="menuHeadingMouseOver(' + theMenu.num + ')" class="menuHeading" id="menuHeading' + theMenu.num + '">' + theMenu.name + '</a>');
    
    if (adjustSpacing)
      document.write('&nbsp;&nbsp;');
  }
  if (ie4 || otherDOM)
     document.write('</div>');
}

// ***************************************************************************

function createMenus( ) 
{  // generates the menus themselves
  createMenuHeadings( );
  for (var menu in menus) {
    var theMenu = menus[menu];
    if (ns4)
      document.write('<layer class="menuNS4" onmouseover="clearTimeout(timeOut)" onmouseout="timeOut = setTimeout(\'hideMenu(' + theMenu.num + ')\', 500)" visibility="hide" name="menu' + theMenu.num + '" top="31" left="' + ((theMenu.num * 100) + 10) + '" bgcolor="#999966" z-index="0" width="100">');
    else if (ie4 || otherDOM)
      document.write('<div class="menu" id="menu' + theMenu.num + '" onmouseover="clearTimeout(timeOut)" onmouseout="timeOut = setTimeout (\'menuHeadingDeactivate( ); hideMenu(' + theMenu.num + ')\', 500)">');

    for (var i=0; i<theMenu.items.length; i++) {
      if (ns4)
        document.write('<a class="menuItemNS4" href="' + theMenu.actions[i] + '">' + theMenu.items[i] + '</a><br>');
      else if (ie4 || otherDOM)
        document.write('<a style="width: 200px" class="menuItem" href="' + theMenu.actions[i] + '">' + theMenu.items[i] + '</a>');
  
       if (ie4 && !ie5plus)
         document.write('<br>');
    }

    if (ns4)
      document.write('</layer>');
    else if (ie4 || otherDOM)
      document.write('</div>');
  }
}

// ***************************************************************************

function menuHeadingMouseOver(menuHeading) {
  if (ie4)
    document.all("menuHeading" + menuHeading).style.borderColor="#999966 #666600 #666600 #999966";
  else if (otherDOM)
    document.getElementById("menuHeading" + menuHeading).style.borderColor="#999966 #666600 #666600 #999966";
}

// ***************************************************************************

function menuHeadingMouseOut(menuHeading) {
  if (ie4 || otherDOM) {
    if (activeMenuHeading != -1 && activeMenuHeading == menuHeading)
      timeOut=setTimeout('hideMenu(' + activeMenuHeading + '); menuHeadingDeactivate( ); ', 500);
    else {
      if (ie4)
        document.all("menuHeading" + menuHeading).style.borderColor="#999966";
      else
        document.getElementById("menuHeading" + menuHeading).style.borderColor="#999966";
    }
  }
}

// ***************************************************************************

function menuHeadingActivate(menuHeading) {
  if (ie4) {
    document.all("menuHeading" + menuHeading).style.background = "#999966";
    document.all("menuHeading" + menuHeading).style.color = "#666600";
    document.all("menuHeading" + menuHeading).style.borderColor = "#666600 #999966 #999966 #666600";
  }
  else if (otherDOM) {
    document.getElementById("menuHeading" + menuHeading).style.background = "#999966";
    document.getElementById("menuHeading" + menuHeading).style.color = "#666600";
    document.getElementById("menuHeading" + menuHeading).style.borderColor = "#666600 #999966 #999966 #666600";
  }
  activeMenuHeading = menuHeading;
}

// ***************************************************************************

function menuHeadingDeactivate( ) {
  if (activeMenuHeading != -1) {
    if (ie4) {
      document.all("menuHeading" + activeMenuHeading).style.borderColor = "#999966";
      document.all("menuHeading" + activeMenuHeading).style.background = "#666600";
      document.all("menuHeading" + activeMenuHeading).style.color = "white";
    }
    else if (otherDOM) {
      document.getElementById("menuHeading" + activeMenuHeading).style.borderColor = "#999966";
      document.getElementById("menuHeading" + activeMenuHeading).style.background = "#666600";
      document.getElementById("menuHeading" + activeMenuHeading).style.color = "white";
    }
   activeMenuHeading = -1;
  }
}

// ***************************************************************************

function menuHeadingMouseClick (event, which) {
  if (activeMenuHeading != -1) {
    clearTimeout(timeOut);
    hideMenu(activeMenuHeading);
    menuHeadingDeactivate( );
  }
  if (ie4 || otherDOM)
    menuHeadingActivate(which);
  if (event.srcElement) {
    x = event.srcElement.offsetLeft;
    y = event.srcElement.offsetTop + event.srcElement.offsetHeight;
  }
  else if (otherDOM) {
     // NS6 specific
    x = document.getElementById("menuHeading" + which).offsetLeft;
    y = document.getElementById("menuHeading" + which).offsetTop + document.getElementById("menuHeading" + which).offsetHeight;
  }
  if (adjustSpacing) {
    x +=7; y +=265;
  }
  showMenu(which, x, y);
}

detectBrowser( );

