// http://members.kaiserpermanente.org/kpweb/js/kp_menus.js

function DHTML_menus() {
  this.menus = [];
  this.setWidthValues = MENUS_setWidths;
  this.setHeightValues = MENUS_setHeights;
  this.loadImages = MENUS_loadImages;
  this.writeMenus = MENUS_writeMenus;
  this.doLayOut = MENUS_doLayOut;
}

var nn4 = (document.layers) ? true : false;
var MENUS_objRef = [];
var minnn4w = 158;
var char_width = 6.5;
var min_right = 15;
var currentW = MENUS_buttonleft;
var w = window;
var d = DHTML_menus;
d.button_left = (w.MENUS_buttonleft) ? MENUS_buttonleft : 15;
d.button_top = (w.MENUS_buttontop) ? MENUS_buttontop : 57;
d.activemenus = [];
d.currentmenu = null;
d.currentitem = null;
d.menukiller = null;
d.maxwidth = 712;

var p = DHTML_menus.prototype;
p.skip = (w.MENUS_skip) ? window.MENUS_skip : new Array(0);
p.imgpath='';
p.itemheight = 19;
p.itemwidth = 100; 
p.dropwidths = []; 
p.itemMsoverBgColor = (w.MENUS_itemMsoverBgColor) ? MENUS_itemMsoverBgColor : '#a6a6a6'; 
p.itemMsoutBgColor = (w.MENUS_itemMsoutBgColor) ? MENUS_itemMsoutBgColor : '#666666'; 
p.subitemMsoverBgColor = (w.MENUS_subitemMsoverBgColor) ? MENUS_subitemMsoverBgColor : '#a6a6a6'; 
p.subitemMsoutBgColor = (w.MENUS_subitemMsoutBgColor) ? MENUS_subitemMsoutBgColor : '#666666';
p.itemMsoverTextColor = (w.MENUS_itemMsoverTextColor) ? MENUS_itemMsoverTextColor : '#00ffff';
p.itemMsoutTextColor = (w.MENUS_itemMsoutTextColor) ? MENUS_itemMsoutTextColor : '#000000';
p.itemoffsetLeft = 9;
p.itemoffsetTop = 0;
p.indent = function() { var s =  (ua.ie && parseInt(ua.ver) == 4) ? '<span style="width:'+this.itemoffsetLeft+'px"></span>' : ''; return s; }
p.selectedmenu = (w.MENUS_currentTheme > -1) ? w.MENUS_currentTheme : null;
p.selecteditem = null; //(srch) ? parseInt(srch.split('_')[1]) : null;

function MENUS_setHeights() {this.heights=arguments;}
function MENUS_setWidths() {this.widths = (arguments.length==1)? arguments[0] : arguments;}

if (ua.codeName == "nn_4_win") {
  document.classes.menuitemlink.all.fontSize = "11px";
}

function MENUS_storeElement(el) {
  MENUS_objRef[MENUS_objRef.length] = el;
}

function MENUS_loadImages() {
  this.imgsA = [];
  this.imgsB = [];
  var a = this.imgsA;
  var b = this.imgsB;
  for(var i=0; i < this.menus.length; i++) {
    var s = this.imgpath+i.toString();
    if ((i == (this.menus.length-1)) && window.MENUS_newmail) {s += "_m"}
    a[i] = new Image(this.widths[i],this.heights[0]);
    a[i].src = (i == this.selectedmenu) ? s+'_1.gif' : s+'_0.gif';
    b[i] = new Image(this.widths[i],this.heights[0]);
    b[i].src = s+'_1.gif';
  }
}

function MENUS_writeMenus() {
  var d = document;
  d.write('<a href="#brand"><img src="/images/spacer.gif" width="1" height="1" alt="Skip navigation links for members" border="0"></a>\n<style>.menuitem{ text-indent:'+this.itemoffsetLeft+ 'px;}</style>');
  d.close();
  var maxW = 0;
  for(var i=0;i < BLinks.length;i++) {
    var s = s2 = "";
    s += '<div class="menubutton" id="m'+i+'"><a href="'+BLinks[i][0]+'" class="menubuttonlink" target="_top"><img src="'+this.imgsA[i].src+'" width="'+this.widths[i]+'" height="'+this.heights[0]+'" border="0" title="" alt="'+BLinks[i][1]+'" /></a></div>\n';
    s += '<div id="menu'+i+'" class="menu">\n';
    for(var j=0;j < MItems[i].length;j++) {
      maxW = Math.max(maxW, MENUS_getWidth(this,i,j));
      var lnk = '<a href="'+MItems[i][j][1]+'" class="menuitemlink" target="_top">'+MItems[i][j][0]+'</a>';
      s += '<div class="menuitem" id="m'+i+'i'+j+'"><nobr>'+this.indent()+lnk+'</nobr></div>\n';
    }
    this.dropwidths[i] = maxW;
    var r = currentW + this.dropwidths[i];
    if(!nn4 && (r > DHTML_menus.maxwidth)) {
      this.dropwidths[i] = DHTML_menus.maxwidth - currentW;
    }
    maxW = 0;
    currentW += MENUS_imgwidths[i];
    if (nn4) s += '<spacer type="block" height="'+((this.itemheight*MItems[i].length)-6)+'" width="'+(this.dropwidths[i]-6)+'"><br>\n'; 
    s += '</div>\n';
    s += s2;
    d.open();
    d.write(s);
    d.close();
  }
}

function MENUS_buttonSetup(parent,idx,top,left,width,height) {
  var o = new csspObj('m'+idx);
  o.selected = false;
  o.menuclass = parent;
  o.style.top = top;
  o.style.left = DHTML_menus.button_left;
  o.resizeTo(width,height);
  o.lnk = o.getElementsByTagName('a')[0];
  o.img = o.getElementsByTagName('img')[0];
  o.imgAsrc = parent.imgsA[idx].src;
  o.imgBsrc = parent.imgsB[idx].src;
  if(o.imgAsrc == o.imgBsrc) o.selected = true;
  o.onmouseover = MENUS_activate;
  o.onmouseout = MENUS_deactivate;
  MENUS_storeElement(o);
  return o;
}

function MENUS_menuSetup(rootmenu,parent,id,top,left,issub,idx) {
  var o = new csspObj(id);
  o.rootmenu = rootmenu;
  o.parent = parent;
  o.menuclass = o.parent.menuclass;
  o.kill = MENUS_kill;
  o.onmouseover = MENUS_msover;
  o.issub = issub;
  o.onmouseout =  MENUS_deactivate;
  o.style.top = top+2;
  o.style.visibility = 'hidden';
  o.style.left = left;
  o.items = o.getElementsByTagName('div');
  o.menuheight = o.menuclass.itemheight * o.items.length;
  o.itemAdjust = (ua.ie || nn4) ? 2 : 0;
  
  o.resizeTo((kpmenus.dropwidths[idx] + o.itemAdjust),(o.menuheight + o.itemAdjust));
  MENUS_storeElement(o);
  return o;
}

function MENUS_itemSetup(idx,parent,id,top,left,minwidth,height,islastitem,contidx) {
  var o = new csspObj(id);
  o.parent = parent;
  o.parent.items[o.parent.items.length] = o;
  o.menuclass = parent.menuclass;
  var cl = o.menuclass;
  o.lnk = o.getElementsByTagName('a')[0];
  
  if(!o.lnk.style) o.lnk.style = new Object();
  o.itemovercolor = cl.itemMsoverTextColor;
  o.itemoutcolor = cl.itemMsoutTextColor;
  var selected = parent.parent.selected && idx == cl.selecteditem;
  o.lnk.style.color = (selected) ? cl.itemMsoverTextColor : cl.itemMsoutTextColor;
  o.setProperty('top',top);
  o.bgoutcolor =  cl.itemMsoutBgColor;
  o.bgovercolor = cl.itemMsoverBgColor;
  o.setProperty('bgColor',o.bgoutcolor);
  o.onmousedown = ITEM_msdown; 
  o.onmouseover = ITEM_msover;
  o.style.left = left;
  o.style.cursor = (o.lnk) ? 'hand' : 'default';
  w = kpmenus.dropwidths[contidx];
  if(islastitem == true)  MENUS_reformat(o.parent, contidx); 
  if(o.lnk && o.style.cursor != 'hand') o.style.cursor = 'pointer';
  MENUS_storeElement(o);
  return o;
}

function MENUS_doLayOut() {
  for(var i=0;i < this.menus.length;i++) {
    var skipmenu = 0;
    for(var j=0;j<this.skip.length;j++) {
      if(this.skip[j] == i) skipmenu = 1;
    }
    if(skipmenu) continue;
    this.widths[i] = (this.widths[i]) ? this.widths[i] : this.widths[0];
    this.heights[i] = (this.heights[i]) ? this.heights[i] : this.heights[0];
    var B = MENUS_buttonSetup(this,i,DHTML_menus.button_top,DHTML_menus.button_left,this.widths[i],this.heights[i]);
    var itemwidth = this.itemwidth;
    var itemheight = this.itemheight;
    var itemtop = (nn4) ? this.itemoffsetTop+1 : this.itemoffsetTop;
    var menuleft = DHTML_menus.button_left;
    var M = B.menu = MENUS_menuSetup(null,B,'menu'+i,DHTML_menus.button_top + this.heights[i],menuleft,0,i);
    M.items = [];
    for(var j=0;j != null;j++) {
      var islastitem = (!csspObj('m'+i+'i'+(j+1))) ? 1 : 0;
      var id = 'm'+i+'i'+j;
      if(!csspObj(id)){j=null;break}
      var itemleft = (nn4) ? 1 : 0;
      var I = MENUS_itemSetup(j,M,id,itemtop,itemleft,this.dropwidths[i],itemheight,islastitem,i);
      itemtop += itemheight;
    }
    B.style.visibility = 'visible'; 
    DHTML_menus.button_left += this.widths[i];
  }
}

function MENUS_getWidth(o,i,j) {
  var iWidth = (MItems[i][j][0].length * char_width) + (o.itemoffsetLeft * 2);
  var calcWidth = Math.round(Math.max(iWidth, o.widths[i]));
  return (nn4 && (minnn4w > calcWidth)) ? minnn4w : calcWidth;
}

function MENUS_reformat(o, contidx) {
  o.style.zIndex = 100;
  var a = o.items;
  var d = DHTML_menus;
  for(var i=0;i<a.length;i++) {
    var x = (nn4) ? a[i].clip : a[i].style;
    x.width = kpmenus.dropwidths[contidx];
  }
}

function MENUS_activate() {
  var a = DHTML_menus.activemenus;
  MENUS_killall();
  this.img.src = this.imgBsrc;
  this.menu.style.visibility = 'visible';
  a[a.length] = this.menu;
}

function MENUS_killall() {
  var d = DHTML_menus;
  var a = d.activemenus;
  for(var i=0;i < a.length;i++) {
    a[i].kill();
    if(i == a.length - 1) a = new Array(0);
  }
  if(d.menukiller) clearTimeout(d.menukiller);
  d.menukiller = null;
}

function MENUS_deactivate() {
  DHTML_menus.menukiller = setTimeout("MENUS_killall()",150);
}

function MENUS_kill() {
  var d = DHTML_menus;
  var c = d.currentitem;
  if(c) ITEM_msout(c);
  var p = this.parent;
  if(p.img) p.img.src = p.imgAsrc;
  if(p.parent)  ITEM_msout(p);
  this.style.visibility = 'hidden';
}

function MENUS_msover() {
  var d = DHTML_menus;
  if(d.menukiller) clearTimeout(d.menukiller);
  d.menukiller = null;
}

function ITEM_msdown() {
  if(window.event && event.srcElement != "[object]") return true;
  top.location.href=this.lnk.href;
  if(nn4) this.releaseEvents(Event.MOUSEDOWN);
  return false;
}

function ITEM_msover() {
  var d = DHTML_menus;
  if (nn4) this.captureEvents(Event.MOUSEDOWN);
  if (d.currentitem && d.currentitem.submenu != this.parent) ITEM_msout(d.currentitem);
  if (d.currentmenu && d.currentmenu != this.submenu && d.currentmenu != this.parent) {
    d.currentmenu.style.visibility = 'hidden';
    ITEM_msout(d.currentmenu.parent);
    d.currentmenu = null;
  }
  this.setProperty('bgColor',this.bgovercolor);
  d.currentitem = this;
}

function ITEM_msout(o) {
  if(nn4) o.releaseEvents(Event.MOUSEDOWN);
  o.setProperty('bgColor',o.bgoutcolor);
}

if(window.BLinks) {
  var h = (window.MENUS_btnheight) ? MENUS_btnheight : 26;
  var kpmenus = new DHTML_menus();
  with(kpmenus) {
    setHeightValues(h);
    setWidthValues(MENUS_imgwidths);
    menus.length = BLinks.length; 
    imgpath = MENUS_imgpath;
    loadImages();
    writeMenus();
  }
  window.setTimeout('kpmenus.doLayOut()',50);
}

function killObject(o) {
  o.parent = null;
  o.rootmenu = null;
  o.menuclass = null;
  o.items = null; 
  o = null;
}

function ieFreeMem() {
  for (i=0; i<MENUS_objRef.length; i++) {
    killObject(MENUS_objRef[i]);
  }
  kpmenus = null;
}

if (ua.ie) {window.onunload = ieFreeMem;}
