/* menu.js - menu system for the bioinformatics program at Wright State University;
 *           This menu currently only supports one layer of sub menus but has been
 *           written in such a way that this can be expanded */

function isMenuItem(str) {
  for (i=0; i< fileNamesInx; i++)
    if (str==fileNames[i]) return true;
  return false; 
}

/* Purpose - returns the file name of the url; 
 * This function will return an empty string if the url
 * does not contain a file name */
function getFileName() {
  href=location.href;
  fields=href.split("/");
  if (!isMenuItem(fields[fields.length-1])) /* If it is not a menu item then show index.shtml */
    return "index.shtml" ;
  if (fields[fields.length-1]=="") return "index.shtml";
  return fields[fields.length-1];
}

/* Purpose - Given an div id the function changes the background color.
 *           If select==0 then bgColor=#222200 else if select==1 bgColor=#006633 */
function changeColor(id,select) {
  o=new getObj(id);
  if (o.style && select==0) {
    if (document.layers) { o.style.bgColor="#222200"; }
    else o.style.backgroundColor="#222200";
  }
  else if (o.style && select==1) {
    if (document.layers) {
      o.style.width="2px";
      o.style.bgColor="#006633";
    }
    else o.style.backgroundColor="#006633";
  } 
  return;
}

/* Purpose - return the object of the given div id */
function getObj(id) {
  if (document.all) {
    if (document.all[id]) {
      this.obj = document.all[id];
      this.style = document.all[id].style;
    } else return null;
  }
  else if (document.getElementById) { 
    if (document.getElementById(id)) {
      this.obj = document.getElementById(id);
      this.style = document.getElementById(id).style;
    } else return null;
  }
  else if (document.layers) {
    if (document.layers[id]) {
      this.obj=document.layers[id]; 
      this.style=document.layers[id];
    } else return null;
  }
  return this;
}

/* Purpose - initially load the menu */
function loadMenu() {
  o=new getObj(menuItems[0].divID);
  i=-1;
  shift(menuItems,o.style.top);
}

/* Purpose - sets the top value of the first element */
function setTop(divID,topValue) {
  o=new getObj(divID/*'top1'*/);
  o.style.top=topValue;
  if (o.style.top=="") 
    o.style.top=topValue+"px";
}

menuItems = new Array();
inx=0;
fileNames = new Array();
fileNamesInx=0;
/* Purpose - add a menu item to the menu, save its height and the height of its sub element
 * if any */
function addMenuItem(fileName,divID,height) {
  menuItems[inx]=new makeMenu(fileName,divID,height);
  inx++;
}

function addSubMenuItem (parentDivID,fileName,divID,height) {
  i=-1;
  for (i=0; i < menuItems.length; i++)
    if (menuItems[i].divID==parentDivID) break;
  menuItems[i].subMenus[menuItems[i].subMenuInx+1]=new makeMenu(fileName,divID,height);
  menuItems[i].subMenuInx++;
}

/* Purpose - create a menu object saving its height and the height of its sub element
 * if any */
function makeMenu(fName,divID,height) {
  fileNames[fileNamesInx]=fName;
  fileNamesInx++;
  this.fileName=fName;
  this.divID=divID;
  this.height=height;
  this.subMenus=new Array();
  this.subMenuInx=-1;
  return this;
}

/* Purpose - shift all of the elements down */
function shift(menus,top) {
  var menuInx=0;
  while (menus[menuInx]!=null) {
    var menuItm=new getObj(menus[menuInx].divID);
    var top=showMenuItem(menuItm,top,menus[menuInx].height);
    if (menus[menuInx] != null && menus[menuInx].subMenus[0]!=null) {
      if (menus[menuInx].fileName==getFileName() || isSubMenu(menus[menuInx].subMenus,getFileName() )) {
        makeSubMenusVisible(menus[menuInx].subMenus,menus);
        top=shift (menus[menuInx].subMenus,top);
      }
    }
    menuInx++;
  } 
  return top;
}

/* Search sub menus for fileN (return true if found and false otherwise) */
function isSubMenu (mns,fileN) {
  for (i=0; i < mns.length; i++) 
    if (mns[i].fileName==fileN) return true;
  return false;
}

/* Purpose - show the sub menus */
function makeSubMenusVisible(menus) {
  for (i=0; i < menus.length; i++) {
    o=new getObj(menus[i].divID);
    o.style.visibility="visible";
  }
}

/* Purpose - shift the menu items vertical placement to
 * top and returns the next top value */
function showMenuItem(menu,top,height) {
  menu.style.top=top;
  if (menu.style.top=="") 
    menu.style.top=top+"px";
  htChange=parseInt(height);
  return htChange+parseInt(top);
}

function expand (divID) {
}
