/*NUMBER1EXPERT_BDMCode.js:
* Copyright (c) 2001 Best Image Marketing. All Rights Reserved. 
* This code is for NUMBER1EXPERTS only and installation on other web sites is expressly prohibited.
* Free, public domain drop-down menu code can be found elsewhere. 
*/

// rippler
function rippler (aBranch) {
  alert ("rippler requires bimDropMenusR");
}

// bdmItem
function bdmItem (aBranch, aText, aStyleFrag, aClickLink, aContent, switchColors, aPopPointer) {
  this.text = aText;
  this.styleFrag = aStyleFrag;
  this.clickLink = aClickLink;
  this.content = aContent;
  this.colorSwitch = switchColors;
  if (aPopPointer.length > 0) this.popPointer = bdm + aPopPointer;
  else this.popPointer = "";
  aBranch.addItem (this);
}

// bdmBranch
function bdmBranch (aMenu, aPopId, aStyleFrag, aPosBy, aCrnBy, aCrnBranch, aAdjX, aAdjY, itemCount) {
  this.popId = bdm + aPopId;
  this.styleFrag = aStyleFrag;
  if (aPosBy == "abs") {
    this.posType = ptAbs;
    this.posId = "";
  } else if (aPosBy == "par") {
    this.posType = ptPar;
    this.posId = "";
  } else {
    this.posType = ptElem;
    this.posId = aPosBy;
  }
  this.posBy = null;
  this.crnBy = aCrnBy;
  this.crnBranch = aCrnBranch;
  this.adjX = aAdjX;
  this.adjY = aAdjY;
  this.posByX = 0;
  this.posByY = 0;
  this.startX = 0;
  this.startY = 0;
  this.items = new Array (itemCount);
  this.hiUsed = - 1;
  this.menuIndex = aMenu.addBranch (this);
}

function rAddItem (aItem) {
  this.hiUsed ++;
  this.items[this.hiUsed] = aItem;
  itemTtl ++;
}
bdmBranch.prototype.addItem = rAddItem;

// bdmMenu
function bdmMenu (aName, branchCount) {
  this.menuName = aName;
  this.branches = new Array (branchCount);
  this.hiUsed = - 1;
  bdmList.addMenu (this);
}

function rAddBranch (aBranch) {
  this.hiUsed ++;
  this.branches[this.hiUsed] = aBranch;
  branchTtl ++;
  return this.hiUsed;
}
bdmMenu.prototype.addBranch = rAddBranch;

// bdmMenuList
function rAddMenu (aMenu) {
  this.hiUsed ++;
  this.menus[this.hiUsed] = aMenu;
}

function bdmMenuList (aDelay, menuCount) {
  this.offDelay = aDelay; 
  this.menus = new Array (menuCount);
  this.hiUsed = - 1;
  this.addMenu = rAddMenu;
  bdmList = this;
}

// HTML branch (DIV)
function rDoBranchOut(event) {
  if (!event) {
    var event = window.event;
  }
  if (event) {
    if (event.relatedTarget == null) setOutStatus (osTimed);
  }
}

function rDoBranchOver(event) {
  if (event) {
    event.stopPropagation();
  }
  else if (window.event) {
    var event = window.event;
    event.cancelBubble = true;
  }
  if (event) {
    setOutStatus (osOver);
  }
}

function rHereOver (sender) {
  if (setOutStatus (osOver))
    if (sender != this.litItem) {
      if (this.litItem != null) this.litItem.dimItem ();
      sender.liteItem ();
      this.litItem = sender;
    }
}

function rHideBranch () {
  if (this.litItem != null) {
    this.litItem.dimItem ();
    this.litItem = null;
  }
  this.style.visibility = "hidden";
  this.popper = null;
}

function rShowBranch (sender) {
  var info = this.info;
  var flowChanged = (info.posType == ptPar);
  if ((document.body.clientWidth != info.winWidth) || (document.body.clientHeight != info.winHeight)) {
    info.winWidth = document.body.clientWidth;
    info.winHeight = document.body.clientHeight;
    flowChanged = true;
  }
  if (document.body.scrollLeft != info.winSLeft) {
    info.winSLeft = document.body.scrollLeft;
    var xChanged = true;
  } else var xChanged = false;
  if (document.body.scrollTop != info.winSTop) {
    info.winSTop = document.body.scrollTop;
    var yChanged = true;
  } else var yChanged = false;
  // which element
  if (info.posType == ptPar) info.posBy = sender;
  else
    if (info.posBy == null) {
      if (info.posType == ptAbs) info.posBy = document.body;
      else info.posBy = document.getElementById (info.posId);
      flowChanged = true;
    }
  // element's x, y
  if (((info.posType == ptElem) && flowChanged) || (info.posType == ptPar)) {
    var par = info.posBy.offsetParent;
    if (info.posType == ptPar) {
      info.posByX = par.childOffLeft;
      info.posByY = par.childOffTop + info.posBy.offsetTop;
    } else {
      info.posByX = info.posBy.offsetLeft;
      info.posByY = info.posBy.offsetTop;
    }
    while (par != null) {
      info.posByX += par.offsetLeft;
      info.posByY += par.offsetTop;
      par = par.offsetParent;
    }
  }
  // starts
  if (flowChanged) {
    if ((info.crnBy == 0) || (info.crnBy == 3)) info.startX = info.posByX + info.adjX;
    else info.startX = info.posByX + info.posBy.offsetWidth + info.adjX;
    if ((info.crnBy == 2) || (info.crnBy == 3)) info.startY = info.posByY + info.posBy.offsetHeight + info.adjY;
    else info.startY = info.posByY + info.adjY;
    if (info.crnBy != 4) {
      if ((info.crnBranch == 1) || (info.crnBranch == 2)) info.startX -= this.styleWidth;
      if ((info.crnBranch == 2) || (info.crnBranch == 3)) info.startY -= this.offsetHeight;
    }
  }
  // keep
  if (flowChanged || xChanged)
    var keepX = Math.min (info.winWidth + info.winSLeft - (info.startX + this.styleWidth), 0);
  else var keepX = 0;
  if (flowChanged || yChanged)
    var keepY = Math.min (info.winHeight + info.winSTop - (info.startY + this.offsetHeight), 0);
  else var keepY = 0;
  if (info.crnBy == 4)
    if (keepX < 0) {
      var x2 = info.posByX - this.styleWidth - info.adjX;
      var kx2 = Math.max (info.winSLeft - x2, 0);
      if ((- keepX) > kx2) {
        info.startX = x2;
        keepX = kx2;
      }
    }
  // do it
  if (flowChanged || xChanged) this.style.left = info.startX + keepX;
  if (flowChanged || yChanged) this.style.top = info.startY + keepY;
  if (bdmCurMenu.rippleInt > 0) new rippler (this);
  else this.style.visibility = "visible";
}

// HTML item (DIV)
function rDoItemClick () {
  eval (this.info.clickLink);
}

function rDoItemOver(event) {
  if (event) {
    event.stopPropagation();
  }
  else if (window.event) {
    var event = window.event;
    event.cancelBubble = true;
  }
  if (event) {
    this.offsetParent.hereOver (this);
  }
}

function rDimItem () {
  if (this.colorSwitch) {
//    this.style.color = bdmCurMenu.colorLow;
    this.info.fontHolder.style.color = bdmCurMenu.colorLow;
    this.style.backgroundColor = bdmCurMenu.backgroundLow;
  }
  if (this.popee != null) this.popee.hideBranch (this);
}

function rLiteItem () {
  if (this.colorSwitch) {
//    this.style.color = bdmCurMenu.colorHigh;
    this.info.fontHolder.style.color = bdmCurMenu.colorHigh;
    this.style.backgroundColor = bdmCurMenu.backgroundHigh;
  }
  status = this.info.statLine;
  if (this.popee != null) this.popee.showBranch (this);
}

// HTML BODY
function rDoBodyOver () {
  setOutStatus (osTimed);
}

// menu title (HTML element)
function bdmTitleOver(aPopId, event) {
  if (event) {
    event.stopPropagation();
  }
  else if (window.event) {
    var event = window.event;
    event.cancelBubble = true;
  }
  if (event) {
    if (setOutStatus (hTitle)) {
      var popee = document.getElementById(bdm + aPopId);
      if (setTopVis(popee)) topVis.showBranch(event.target);
    }
  }
}

function bdmTitleOut(event) {
  if (!event) {
    var event = window.event;
  }
  if (event) {
    if (event.relatedTarget == null) setOutStatus (osTimed);
  }
}

// global functions
function bdmBuildMenus () {
  var branchDivFixed = "<DIV style='position:absolute;visibility:hidden;";
  var itemDivFixed = "<DIV style='";
  var imgReplaceA = "IMG style='position:absolute;right:";
//  var reB = /\/\/b/g, reT = /\/\/t/g;
  var reImg = /img/i;
  var theItem, htmlItem;   // object
  var branchDivStyle, itemDivStyle, expImgStr; // string
  var branchI, branchHiI, itemI, itemHiI, isUrl;   // integer
  var htmlBranch, htmlImg;   // HTML element
  var menuColorSwitch;   // boolean
  var outs = new Array ((branchTtl * branchOutCount) + (itemTtl * itemOutCount));
  var i1 = 0;
  var menuI = 0, menuHiI = bdmList.hiUsed;
  while (menuI <= menuHiI) {
    thisMenu = bdmList.menus[menuI];
    if ((thisMenu.rippleAmt > 0) && (thisMenu.rippleSpeed > 0))
      thisMenu.rippleInt = Math.ceil (thisMenu.rippleAmt * 1000 / thisMenu.rippleSpeed);
    else thisMenu.rippleInt = 0;
    branchDivStyle = branchDivFixed;
    if (thisMenu.branchStyle.length > 0) branchDivStyle += thisMenu.branchStyle + ";";
    itemDivStyle = itemDivFixed;
    if (thisMenu.itemStyle.length > 0) itemDivStyle += thisMenu.itemStyle + ";";
    expImgStr = thisMenu.expandImage.replace (reImg, imgReplaceA + thisMenu.expImgPad + "'");
    if (thisMenu.colorHigh.length == 0) thisMenu.colorHigh = thisMenu.colorLow;
    if (thisMenu.backgroundHigh.length == 0) thisMenu.backgroundHigh = thisMenu.backgroundLow;
    branchI = 0, branchHiI = thisMenu.hiUsed;
    while (branchI <= branchHiI) {
      thisBranch = thisMenu.branches[branchI];
      outs[i1] = branchDivStyle;
      outs[i1 + 1] = thisBranch.styleFrag;
      outs[i1 + 2] = "' id=";
      outs[i1 + 3] = thisBranch.popId;
      outs[i1 + 4] = ">";
      i1 += branchOutCount - 1;
      itemI = 0, itemHiI = thisBranch.hiUsed;
      while (itemI <= itemHiI) {
        theItem = thisBranch.items[itemI];
        isUrl = 0;
        if (theItem.clickLink.length > 0)
          if (theItem.clickLink.substr (0, 11) == "javascript:") {
            theItem.clickLink = theItem.clickLink.substr (11);
            isUrl = 1;
          }
          else if (theItem.clickLink.substr (0, 6) == "blank:") {
            theItem.content = "<a id='BDA" + thisBranch.popId + itemI + "' href='" + theItem.clickLink.substr(6) + "' target='_blank' style='color:inherit;font-weight:normal;text-decoration:none;cursor:pointer'>" + theItem.content + "</a>";
            theItem.clickLink = "";
            if (bdmIsIE) {
              theItem.fontHolder = theItem;
            }
            isUrl = 1;
          }
          else {
            theItem.clickLink = "location.href='" + theItem.clickLink + "';";
            isUrl = 2;
          }
        if ((thisMenu.itemStatLine == 0) || (isUrl == 0)) theItem.statLine = "";
        else
          if (thisMenu.itemStatLine == 2) theItem.statLine = theItem.text;
          else
            if (isUrl == 1) theItem.statLine = theItem.clickLink;
            else theItem.statLine = theItem.clickLink.substring (15, theItem.clickLink.length - 2);
        outs[i1] = itemDivStyle;
        outs[i1 + 1] = theItem.styleFrag;
        outs[i1 + 2] = "' id=BDI" + thisBranch.popId;
        outs[i1 + 3] = itemI;
        outs[i1 + 4] = ">";
        outs[i1 + 5] = theItem.content.replace ("//b", thisMenu.bulletImage).replace (/\/\/t/g, theItem.text);
        outs[i1 + 6] = "</DIV>";
        if (theItem.popPointer.length > 0)
          outs[i1 + 7] = expImgStr.replace (reImg, "IMG id=" + thisBranch.popId + itemI);
        else outs[i1 + 7] = "";
        i1 += itemOutCount;
        itemI++;
      }
      outs[i1] = "</DIV>";
      i1 ++;
      branchI++;
    }
    menuI++;
  }
  document.write (outs.join (""));
//  alert (outs.join (""));
  menuI = 0;
  while (menuI <= menuHiI) {
    thisMenu = bdmList.menus[menuI];
    if ((thisMenu.colorLow == thisMenu.colorHigh) && (thisMenu.backgroundLow == thisMenu.backgroundHigh))
      menuColorSwitch = false;
    else menuColorSwitch = true;
    branchI = 0, branchHiI = thisMenu.hiUsed;
    while (branchI <= branchHiI) {
      thisBranch = thisMenu.branches[branchI];
      htmlBranch = document.getElementById (thisBranch.popId);
      thisBranch.branch = htmlBranch;
      htmlBranch.info = thisBranch;
      htmlBranch.menuInfo = thisMenu;
      htmlBranch.styleWidth = parseInt (htmlBranch.style.width);
      if (htmlBranch.style.borderLeftWidth.length > 0)
        htmlBranch.childOffLeft = parseInt (htmlBranch.style.borderLeftWidth);
      else htmlBranch.childOffLeft = 0;
      if (htmlBranch.style.borderTopWidth.length > 0)
        htmlBranch.childOffTop = parseInt (htmlBranch.style.borderTopWidth);
      else htmlBranch.childOffTop = 0;
      htmlBranch.litItem = null;
      htmlBranch.hereOver = rHereOver;
      htmlBranch.onmouseover = rDoBranchOver;
      htmlBranch.onmouseout = rDoBranchOut;
      htmlBranch.showBranch = rShowBranch;
      htmlBranch.hideBranch = rHideBranch;
      itemI = 0, itemHiI = thisBranch.hiUsed;
      while (itemI <= itemHiI) {
        theItem = thisBranch.items[itemI];
        htmlItem = document.getElementById (bdi + thisBranch.popId + itemI);
        htmlItem.info = theItem;

        if (!theItem.fontHolder) {
          theItem.fontHolder = htmlItem;
        }
        else {
          theItem.fontHolder = document.getElementById (bda + thisBranch.popId + itemI);
          if (!theItem.fontHolder) {
            theItem.fontHolder = htmlItem;
          }
        }
//        htmlItem.style.color = thisMenu.colorLow;
        theItem.fontHolder.style.color = thisMenu.colorLow;
        htmlItem.style.backgroundColor = thisMenu.backgroundLow;
        htmlItem.colorSwitch = (theItem.colorSwitch && menuColorSwitch);
        if (theItem.popPointer.length > 0) {
          htmlItem.popee = document.getElementById (theItem.popPointer);
          htmlImg = document.getElementById (thisBranch.popId + itemI);
          htmlImg.style.top = htmlItem.offsetTop - htmlBranch.childOffTop
                                + Math.ceil ((htmlItem.offsetHeight - htmlImg.height) / 2)
                                + thisMenu.expImgAdjTop;
        } else htmlItem.popee = null;
        if (theItem.clickLink.length > 0) {
          htmlItem.onclick = rDoItemClick;
          htmlItem.style.cursor = "pointer";
        }
        htmlItem.onmouseover = rDoItemOver;
        htmlItem.liteItem = rLiteItem;
        htmlItem.dimItem = rDimItem;
        itemI ++;
      }
      branchI ++;
    }
    menuI ++;
  }
}

function setOutStatus (newStatus) {
  if (newStatus != outStat) {
    switch (newStatus) {
    case osOff :
      document.body.onmouseover = null;
      outTime = null;
      setTopVis (null);
      outStat = osOff;
      break;
    case osOver : 
      if ((outStat == osTimed) && (outTime == null)) return false;
      else {
        outStat = osOver;
        document.body.onmouseover = rDoBodyOver;
        if (outTime != null) clearTimeout (outTime);
        outTime = null;
      }
      break;
    case osTimed :
      document.body.onmouseover = null;
      outStat = osTimed;
      outTime = setTimeout ("setOutStatus (osOff)", bdmList.offDelay);
      break;
    default : return false;
    }
  }
  return true;
}

function setTopVis (aBranch) {
  if (aBranch != topVis) {
    if (topVis != null) topVis.hideBranch ();
    status = "";
    topVis = aBranch;
    if (topVis != null) {
      bdmCurMenu = topVis.menuInfo;
      var visSetting = "hidden", menuVisible = true;
    }
    else {
      var visSetting = "visible", menuVisible = false;
    }
    if (bdmCurMenu.adjustSelectElems) {
      var elems = document.getElementsByTagName("select");
      var I1 = 0, HiI = elems.length - 1;
      while (I1 <= HiI) {
        var elem = elems[I1];
        elem.style.visibility = visSetting;
        I1 += 1;
      }
    }
    if (bdmCurMenu.onMenuVisible) {
      bdmCurMenu.onMenuVisible(menuVisible);
    }
    return true;
  } else return false;
}

// global constants
var branchOutCount = 6, itemOutCount = 8;
var osOff = 0, osOver = 1, osTimed = 2;
var atRight = 0, atLeft = 1, atBottom = 2;
var bdm = "BDM", bdi = "BDI", bda = "BDA";
// global variables
var bdmList, thisBranch, thisMenu;   // object
var topVis = null, bdmCurMenu = null;
var outTime = null;
var outStat = osOff;
var branchTtl = 0, itemTtl = 0, hTitle = 3;
var ptAbs = 0, ptElem = 1, ptPar = 2;
var bdmIsIE = document.all;
// create top object
new bdmMenuList (bdmDelay, bdmMenuCount);


