hasDOM = (document.getElementById) ? true : false;
mac=(navigator.appVersion.indexOf("Mac") != -1) ? true : false;
IE = (document.all) ? true : false;
V = "visible";
H = "hidden";
pagedone = null;

if (hasDOM) {
  layerDom = "document.getElementById('";
  elemEnd = "')";
  idsDom = "document.getElementById('";
  pLeft = "').style.left";
  pTop = "').style.top";
  pHeightS = "').style.height";
  pWidth = "').style.width";
  pWidth2 = "').style.width";
  pWidth3 = "').offsetWidth";
  pHeight = "').offsetHeight";
  pVis = "').style.visibility";
  pBgColor = "').style.backgroundColor";
}

if (IE) {
  layerDom = "document.all['";
  elemEnd = "']";
  idsDom = "document.all['";
  pLeft = "'].style.pixelLeft";
  pTop = "'].style.pixelTop";
  pHeightS = "'].style.pixelHeight";
  pWidth = "'].style.pixelWidth";
  pWidth2 = "'].style.pixelWidth";
  pWidth3 = (navigator.appVersion.indexOf("MSIE 4") != -1) ? "'].scrollWidth" : "'].offsetWidth";
  pHeight = (navigator.appVersion.indexOf("MSIE 4") != -1) ? "'].scrollHeight" : "'].offsetHeight";
  pVis = "'].style.visibility";
  pBgColor = "'].style.backgroundColor";
}

subBg = "#eaeaef";
subHi = "#cacad4";
mainBg = "#f0f0f0";
mainHi = "#cacad4";
topBg = "#333366";
topHi = "#000033";

JSok = (IE || hasDOM) ? true : false;

shifty = null;
function checkKeyUp(e) {
  kc = (IE) ? event.keyCode : (e.keyCode) ? e.keyCode : e.which;
  if (kc == 16) {
    shifty = null;
  }
}

function checkKeyDown(e) {
  kc = (IE) ? event.keyCode : (e.keyCode) ? e.keyCode : e.which;
  if (kc == 16) {
    shifty = 1;
  }
}

function getRelativeLeft(what) {
  eElement = (typeof what == "object") ? what : eval(layerDom+what+elemEnd);
  nLeftPos = eElement.offsetLeft;
  return nLeftPos;
}

function getLeft(what) {
  eElement = (typeof what == "object") ? what : eval(layerDom+what+elemEnd);
  nLeftPos = eElement.offsetLeft;
  eParElement = eElement.offsetParent;
  while (eParElement != null) {
    nLeftPos += eParElement.offsetLeft;
    eParElement = eParElement.offsetParent;
  }
  return nLeftPos;
}

function getRelativeTop(what) {
  eElement = (typeof what == "object") ? what : eval(layerDom+what+elemEnd);
  nTopPos = eElement.offsetTop;
  return nTopPos;
}

function getTop(what) {
  eElement = (typeof what == "object") ? what : eval(layerDom+what+elemEnd);
  nTopPos = eElement.offsetTop;
  eParElement = eElement.offsetParent;
  estr = what+" "+nTopPos+", ";
  while (eParElement != null) {
    nTopPos += eParElement.offsetTop;
    pid = eParElement.id;
    estr = estr+pid+" "+nTopPos+", ";
    eParElement = eParElement.offsetParent;
  }
  return nTopPos;
}

function getHeight(what) {
  eElement = (typeof what == "object") ? what : eval(layerDom+what+elemEnd);
  nHeightPos = eElement.offsetHeight;
  return nHeightPos;
}

function getWidth(what) {
  eElement = (typeof what == "object") ? what : eval(layerDom+what+elemEnd);
  nWidthPos = eElement.offsetWidth;
  return nWidthPos;
}

////

mY = null;
mX = null;
lastSub = null;
lastSubSub = null;
lastMain = null;
lastSubMain = null;
lastOver = null;
menuEvent = false;
menuEvent2 = false;
mainRect = new Array();
subRect = new Array();
mainSubRect = new Array();
subSubRect = new Array();

function trackMouse(e) {
  mY = (!IE) ? e.pageY : (document.documentElement && !mac) ? event.clientY + document.documentElement.scrollTop : event.clientY + document.body.scrollTop;
  mX = (!IE) ? e.pageX : (document.documentElement && !mac) ? event.clientX + document.documentElement.scrollLeft : event.clientX + document.body.scrollLeft;
  //window.status = "Y "+mY+": X "+mX;
  mainSubOut = false;
  subSubOut = false;
  if (menuEvent2 == true) {
    if (mY < mainSubRect[0] || mY > mainSubRect[2] || mX < mainSubRect[3] || mX > mainSubRect[1]) {
      mainSubOut = true;
      //alert("mainOut");
    }
    if (lastSubSub) {
      if (mY < subSubRect[0] || mY > subSubRect[2] || mX < subSubRect[3] || mX > subSubRect[1]) {
        subSubOut = true;
      }
    } else {
      subSubOut = true;
    }
    if (mainSubOut == true && subSubOut == true) {
      mainSubRect = new Array();
      subSubRect = new Array();
      hideLayer(lastSubSub);
      lastSubSub = null;
      lastSubMain = null;
      menuEvent2 = false;
      
    }
  }
  mainOut = false;
  subOut = false;
  if (menuEvent == true) {
    if (mY < mainRect[0] || mY > mainRect[2] || mX < mainRect[3] || mX > mainRect[1]) {
      mainOut = true;
      //alert("mainOut");
    }
    if (lastSub) {
      if (mY < subRect[0] || mY > subRect[2] || mX < subRect[3] || mX > subRect[1]) {
        subOut = true;
      }
    } else {
      subOut = true;
    }
    if (mainOut == true && (menuEvent2 == false && subOut == true)) {
      mainRect = new Array();
      subRect = new Array();
      hideLayer(lastSub);
      la = eval(layerDom+lastMain+elemEnd);
      hiC = topBg;
      la.style.backgroundColor = hiC;
      la.className = "topbutton";
      lastSub = null;
      lastMain = null;
      menuEvent = false;
    }
  }
}

function setRect(whatRect,what) {
  t = getTop(what);
  l = getLeft(what);
  w = getWidth(what);
  h = getHeight(what);
  whatRect[0] = t;
  whatRect[1] = l+w;
  whatRect[2] = t+h;
  whatRect[3] = l;
}

function hilite(e) {
  la = (IE) ? event.srcElement : e.target;
  if (!IE) { la = findParentDiv(la); }
  if (la.nodeName == "DIV") {
    cn = la.className;
    if (cn == "subcolbutton") {
      hiC = subHi;
      la.style.backgroundColor = hiC;
    }
    if (cn == "topbutton") {
      hiC = topHi;
      la.style.backgroundColor = hiC;
      la.className = "topbuttonover";
    }
	if (cn == "greenbutton") {
		la.className="greenbutttonover";
	}
    if (cn == "homebutton") {
      //hiC = "#666666";
      //la.style.backgroundColor = hiC;
      la.className = "homebuttonover";
    }
  }
}

function delite(e) {
  la = (IE) ? event.srcElement : e.target;
  if (!IE) { la = findParentDiv(la); }
  if (la.nodeName == "DIV") {  
    cn = la.className;
    if (cn == "subcolbutton") {
      hiC = subBg;
      la.style.backgroundColor = hiC;
    }
    if (cn == "homebuttonover") {
      //hiC = "#666666";
      //la.style.backgroundColor = hiC;
      la.className = "homebutton";
    }
  }
}

function focusSub(main,sublayer) {
  if (!pagedone) { return false; }
  la = findParentDiv(main);
  cn = la.className;
  if (cn == "topbutton") {
    if (lastSub) {
      hideLayer(lastSub);
      lastSub = null;
    }
    lastMain = la;
    showLayer(sublayer);
    lastSub = sublayer;
  } else {
    if (lastSubSub) {
      hideLayer(lastSubSub);
      lastSubSub = null;
    }
    lastSubMain = la;
    showLayer(sublayer,1);
    lastSubSub = sublayer;
  }
}

function showSubs() {
  if (!pagedone) { return false; }
  if (menuEvent == false) {
    if (arguments.length > 1) {
      lastSub = arguments[1];
      lastMain = arguments[0].id;
      setRect(mainRect,lastMain);
      menuEvent = true;
      showLayer(arguments[1],0);
    }
  }
  if (menuEvent2 == false) {
    if (arguments.length > 2) {
      lastSubSub = arguments[1];
      lastSubMain = arguments[0].id;
      setRect(mainSubRect,lastSubMain);
      menuEvent2 = true;
      showLayer(arguments[1],1);
    }
  }
}

function clickClose() {
  dv = null;
  if (lastSub) {
    hideLayer(lastSub);
  }
}

function hideLayer(what,withShift) {
  if (!pagedone) { return false; }
  if (withShift == 1 && !shifty) {
    return;
  }
  if (withShift == 0 && shifty) {
    return;
  }
  eval(layerDom+what+pVis+" = '"+H+"'");
}


//function hideLayer(what) {
//  what = (what == lastSub) ? what : "forsubsubmenus";
//  eval(layerDom+what+pVis+" = '"+H+"'");
//}

function getWindowHeight2() {
  scrollOff = (mac) ? 0 : 5;
  gWH = (IE) ? document.body.clientHeight - scrollOff : parseInt(window.innerHeight);
  return gWH;
}

function getWindowHeight() {
  if (window.innerHeight) {
    gWH = window.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    gWH = document.documentElement.clientHeight;
  }  else if (document.body) {
    gWH = document.body.clientHeight;
  }
  return gWH;
}

function checkBTNClick(e) {
  //clickClose();
  //if (lastOver) {
  //  goLink(lastOver);
  //}
}

function findParentDiv(what) {
  elem = null;
  if (IE && !hasDOM) {
    if (what.tagName == "DIV") {
      return what;
    }
    elem = what.parentElement;
    while (elem.tagName != "DIV") {
      elem = elem.parentElement;
      if (elem.tagName == "BODY") {
        elemId = null;
        break;
      }
    }
  } else if (hasDOM) {
    if (what.nodeName == "DIV") {
      return what;
    }
    if (what.parentNode) {
    elem = what.parentNode;
    while (elem.nodeName != "DIV") {
      elem = elem.parentNode;
      if (elem.nodeName == "#document") {
        elemId = null;
        break;
      }
    }
    }
  }
  return elem;
}

function makeSubHTML(what) {
  htm = "";
  if (IE) {
    tags = what.document.all.tags('div');
    for (i=0;i<tags.length;i++) {
      if (tags[i].parentElement == what && tags[i].className != "colsubbuttongrp") {
        htm = htm+tags[i].outerHTML;
      }
    }
  } else {
    htm = what.innerHTML;
  }
  return htm;
}

function showLayer(what,isSubSub) {
  //window.status = "show layer "+what+" "+isSubSub;
  l = (isSubSub == 1) ? getRelativeLeft(lastSubMain)+160 : getRelativeLeft(lastMain);
  t = (isSubSub == 1) ? getRelativeTop(lastSubMain) : getRelativeTop(lastMain);
  h = getHeight(lastMain);
  subid = what;
  subobj = eval(layerDom+subid+elemEnd);
  whatobj = eval(layerDom+what+elemEnd);
  h2 = getHeight(subid);
  h3 = getWindowHeight();
  b = (isSubSub == 1) ? t : t+h;
  b = (b+h2+82 > h3 && isSubSub == 1) ? b+(h3-(b+h2))-82 : b;
  lval = (!IE) ? " = '"+l+"px'" : " = "+l;
  tval = (!IE) ? " = '"+b+"px'" : " = "+b;
  eval(layerDom+subid+pLeft+lval);
  eval(layerDom+subid+pTop+tval);
  if (isSubSub == 1) {
    subSubRect[0] = getTop(subid);
    subSubRect[1] = getLeft(subid)+170;
    subSubRect[2] = getTop(subid)+h2;
    subSubRect[3] = getLeft(subid);
  } else {
    subRect[0] = getTop(subid);
    subRect[1] = getLeft(subid)+170;
    subRect[2] = getTop(subid)+h2;
    subRect[3] = getLeft(subid);
  }
  
  eval(layerDom+subid+pVis+" = '"+V+"'");
}

function init() {
  if (IE) {
    document.onmousemove = trackMouse;
    document.onkeyup = checkKeyUp;
    document.onkeydown = checkKeyDown;
    //document.onmouseup = checkBTNClick;
    tgs = document.all.tags("div");
    for (i=0;i<tgs.length;i++) {
      cn = tgs[i].className;
      if (cn == "topbutton" || cn == "subcolbutton" || cn == "homebutton") {
        if (mac) {
          //tgs[i].onmouseover = hilite;
          //tgs[i].onmouseout = delite;
        } else {
          tgs[i].attachEvent("onmouseenter",hilite);
          tgs[i].attachEvent("onmouseleave",delite);
        }
      }
    }
    if (mac) {
      ieMacPadAdjust;
      document.all.mainconstruction.style.pixelHeight = 23;
      document.all.about.style.pixelHeight = 23;
      document.all.financial.style.pixelHeight = 23;
    }
  } else if (hasDOM) {
    document.addEventListener('mousemove', trackMouse, true);
    document.addEventListener('keyup', checkKeyUp, true);
    document.addEventListener('keydown', checkKeyDown, true);
    tgs = document.getElementsByTagName("DIV");
    for (i=0;i<tgs.length;i++) {
      cn = tgs[i].className;
      tgs[i].addEventListener("mouseover",hilite,true);
      tgs[i].addEventListener("mouseout",delite,true);
    }
  }
  pagedone = true;
}

function ieMacPadAdjust() {  
  tgs = document.all;
  for (i=0;i<tgs.length;i++) {
    addHpad = 0;
    if (tgs[i].currentStyle.paddingLeft != 0) {
      addHpad += parseInt(tgs[i].currentStyle.paddingLeft);
    }
    if (tgs[i].currentStyle.paddingRight != 0) {
      addHpad += parseInt(tgs[i].currentStyle.paddingRight);
    }
    addVpad = 0;
    if (tgs[i].currentStyle.paddingTop != 0) {
      addVpad += parseInt(tgs[i].currentStyle.paddingTop);
    }
    if (tgs[i].currentStyle.paddingBottom != 0) {
      addVpad += parseInt(tgs[i].currentStyle.paddingBottom);
    }
    if (addHpad > 0) {
      w = parseInt(tgs[i].currentStyle.width);
      nw = w + addHpad;
      tgs[i].style.pixelWidth = nw;
    }
    if (addVpad > 0) {
      h = parseInt(tgs[i].currentStyle.height);
      nh = h + addVpad;
      tgs[i].style.pixelHeight = nh;
    }
  }
}

onload=init;