// overly simplistic test for IE
isIE = (document.all ? true : false);
// both IE5 and NS6 are DOM-compliant
isDOM = (document.getElementById ? true : false);

// get the true offset of anything on NS4, IE4/5 & NS6, even if it's in a table!
function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }
function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt,"Top"); }
function getAbsPos(elt,which) {
 iPos = 0;
 while (elt != null) {
  iPos += elt["offset" + which];
  elt = elt.offsetParent;
 }
 return iPos;
}

// fixPosition() attaches the element named eltname
// to an image named eltname+'Pos'
//
function fixPosition(divname,menuWidth, menuHeight,isPlacedUnder) {
 divstyle = getDivStyle(divname);
 positionerImgName = divname + 'Pos';
 
 // hint: try setting isPlacedUnder to false
 if (isPlacedUnder) {
  setPosition(divstyle,positionerImgName,menuWidth,menuHeight,true);
 } else {
  setPosition(divstyle,positionerImgName,menuWidth,menuHeight,false);
 }
}

function fixPosition(divname,menuWidth,menuHeight,isPlacedUnder,positionerImgName) {
 divstyle = getDivStyle(divname);
 
 // hint: try setting isPlacedUnder to false
 if (isPlacedUnder) {
  setPosition(divstyle,positionerImgName,menuWidth,menuHeight,true);
 } else {
  setPosition(divstyle,positionerImgName,menuWidth,menuHeight,false);
 }
}


function getDivStyle(divname) {
 var style;
 if (isDOM) { style = document.getElementById(divname).style; }
 else { style = isIE ? document.all[divname].style
                     : MM_findObj(divname); } // NS4
 return style;
}

function hideElement(divname) {
 getDivStyle(divname).visibility = 'hidden';
}

// annoying detail: IE and NS6 store elt.top and elt.left as strings.
function moveBy(elt,deltaX,deltaY) {
 elt.left = parseInt(elt.left) + deltaX;
 elt.top = parseInt(elt.top) + deltaY;
}

function toggleVisible(divname, menuWidth,menuHeight,isPlacedUnder) {
 divstyle = getDivStyle(divname);
  if (divstyle.visibility == 'visible' || divstyle.visibility == 'show') {
   divstyle.visibility = 'hidden';
 } else {
   fixPosition(divname, menuWidth,menuHeight,isPlacedUnder);
   divstyle.visibility = 'visible';
 }
}

function toggleVisible(divname,menuWidth,menuHeight,isPlacedUnder,imgURL,imgURLOn,layer1,layer2) {//alert(layer2);
 divstyle = getDivStyle(divname);
 layer1style = getDivStyle(layer1);
 layer2style = getDivStyle(layer2);
 positionerImgName = divname + 'Pos';
 
 var positioner;
 if (isIE) {
  positioner = document.all[positionerImgName];
 } else {
  if (isDOM) {
    positioner = document.getElementById(positionerImgName);
  } else {
    // not IE, not DOM (probably NS4)
    // if the positioner is inside a netscape4 layer this will *not* find it.
    // I should write a finder function which will recurse through all layers
    // until it finds the named image...
    positioner = document.images[positionerImgName];
  }
 }
 
 if (divstyle.visibility == 'visible' || divstyle.visibility == 'show') {
   divstyle.visibility = 'hidden';
   layer1style.visibility = 'hidden';
   layer2style.visibility = 'hidden';
   positioner.src = imgURL;
 } else {
   positioner.src = imgURLOn;
   fixPosition(divname, menuWidth,menuHeight,isPlacedUnder,positionerImgName);
   divstyle.visibility = 'visible';
   layer1style.visibility = 'visible';
   layer2style.visibility = 'visible';
   setPosition(layer1style,positionerImgName,8,15,isPlacedUnder);
   
 }
}

function setPosition(elt,positionername,menuWidth,menuHeight,isPlacedUnder) {//alert();
 var positioner;
 if (isIE) {
  positioner = document.all[positionername];
 } else {
  if (isDOM) {
    positioner = document.getElementById(positionername);
  } else {
    // not IE, not DOM (probably NS4)
    // if the positioner is inside a netscape4 layer this will *not* find it.
    // I should write a finder function which will recurse through all layers
    // until it finds the named image...
    positioner = document.images[positionername];
  }
 }

 elt.left = getAbsX(positioner) + menuWidth;
 elt.top = getAbsY(positioner) + (isPlacedUnder ? positioner.height : 0) + menuHeight;
}
