/*
 * positioning.js
 *
 * $Date: 2002/02/27 18:22:18 $
 *
 * Copyright 2001 Idium AS. All Rights Reserved.
 *
 * This software is the proprietary information of Idium AS.
 * Use is subject to license terms.
 */



/**********************************************************************
 *
 * Description: This script contains functionality to position html
 * (DIV) elements dynamically in the browser. This is useful for popup
 * menus, etc. 
 * 
 * Author: Gorm Paulsen (gorm@idium.no)
 * $Id: positioning.js,v 1.1 2002/02/27 18:22:18 gorm Exp $
 ***********************************************************************/



/*
 * Determine browser type:
 */

var useragent=navigator.userAgent.toLowerCase();
var majorversion = parseInt(navigator.appVersion);

var ns4 = ((useragent.indexOf('mozilla') != -1) && (useragent.indexOf('spoofer') == -1)
           && (useragent.indexOf('compatible') == -1) && (useragent.indexOf('opera') == -1)
           && (useragent.indexOf('webtv') == -1) && (useragent.indexOf('hotjava') == -1) 
           && majorversion == 4);
var ie4 = ((useragent.indexOf("msie") != -1) && (useragent.indexOf("opera") == -1) && majorversion >= 4);
var gecko = (useragent.indexOf('gecko') != -1);
// Opera will be identified as opera no even if it is spoofing
var opera = (useragent.indexOf("opera") != -1);

var debugStr = "ns4: " + ns4 + "\n" 
    + "ie4: " + ie4 + "\n"
    + "gecko: " + gecko + "\n"
    + "opera: " + opera + "\n";
//alert(debugStr);

/*
 * Retrieves an object  based on its ID attribute 
 * (e.g. <div id="">...</div>)
 */
function getElement(id) {
   if (gecko || opera) {
      return document.getElementById(id);
   } else if (ie4) {
       return document.all[id];
   } else if (ns4) {
      var obj = eval('document.layers.' + id);
      return obj;
   }
   return null;
}


/*
 * Finds the absolute left coordinate of obj
 */
function findLeftPos(obj) {
   var left = 0;
   if (gecko || ie4 || opera) {
       while (obj.offsetParent) {
           left += obj.offsetLeft;
           obj = obj.offsetParent;
       }
   } else if (ns4) {
       left += obj.x;
   }
   return left;
}


/*
 * Finds the absolute top coordinate of obj
 */
function findTopPos(obj) {
    var top = 0;
    if (gecko || ie4 || opera) {
        while (obj.offsetParent) {
            top += obj.offsetTop;
            obj = obj.offsetParent;
        }
    } else if (ns4) {
        top += obj.y;
    }
    return top;
}


/*
 * Find relative vertical position of object within 
 * container object.
 */
function getRelativeHPos(obj, container) {
  var left = 0;
  if (gecko || ie4 || opera) {
     left = findLeftPos(obj) - findLeftPos(container);
  } else if (ns4) {
     // FIXME: will not work with nested <div> tags
     left = obj.x;
  }
  return left;
}


/*
 * Find relative vertical position of object within 
 * container object.
 */
function getRelativeVPos(obj, container) {
  var top = 0;
  if (gecko || ie4 || opera) {
     top = findTopPos(obj) - findTopPos(container);
  } else if (ns4) {
     // FIXME: will not work with nested <div> tags
     top = obj.y;
  }
  return top;
}


/*
 * Find height of obj. This will work not in netscape 4 for any other
 * objects than layers.
 */
function findHeight(obj) {
   if (gecko) {
      return parseInt(document.defaultView.getComputedStyle(obj, "").getPropertyValue("height"));
   } else if (ie4 || opera) {
      return obj.offsetHeight;
   } else if (ns4) {
      return obj.clip.height;
   }
}


/*
 * Find width of obj. This will work not in netscape 4 for any other
 * objects than layers.
 */
function findWidth(obj) {
   if (gecko) {
      return parseInt(document.defaultView.getComputedStyle(obj, "").getPropertyValue("width"));
   } else if (ie4 || opera) {
      return obj.offsetWidth;
   } else if (ns4) {
      return obj.clip.width;
   }
   //return obj.offsetWidth;
}


/*
 * Is the object visible?
 */
function visible(obj) {
   if (gecko || ie4 || opera) {
      return  (obj.style.visibility == "visible");
   } else if (ns4) {
      return (obj.visibility == "show");
   }
}


/*
 * Positions object absolutely at point (x, y) 
 */
function placeObject(obj, x, y) {
   if (gecko || ie4 || opera) {
      obj.style.left = x;
      obj.style.top = y;
   } else if (ns4) {
      obj.left = x;
      obj.top = y;
   }  
   return obj;
}


/*
 * Aligns obj right of anchor, offset by hOffset, vOffset
 */
function alignRight(obj, anchor, hOffset, vOffset) {
   var xPos = findLeftPos(anchor) + findWidth(anchor);
   var yPos = findTopPos(anchor);
   placeObject(obj, xPos + hOffset, yPos + vOffset);
   return obj;
}


/*
 * Aligns obj left of anchor, offset by hOffset, vOffset
 */
function alignLeft(obj, anchor, hOffset, vOffset) {
   var xPos = findLeftPos(anchor) - findWidth(obj);
   var yPos = findTopPos(anchor);
   placeObject(obj, xPos + hOffset, yPos + vOffset);
   return obj;
}

/*
 * Aligns obj above anchor, offset by hOffset, vOffset
 */
function alignTop(obj, anchor, hOffset, vOffset) {
   var xPos = findLeftPos(anchor);
   var yPos = findTopPos(anchor) - findHeight(obj);
   placeObject(obj, xPos + hOffset, yPos + vOffset);
   return obj;
}


/*
 * Aligns obj below anchor, offset by hOffset, vOffset
 */
function alignBottom(obj, anchor, hOffset, vOffset) {
   var xPos = findLeftPos(anchor);
   var yPos = findTopPos(anchor) + findHeight(anchor);
   placeObject(obj, xPos + hOffset, yPos + vOffset);
   return obj;
}



/*
 * Hides an object
 */
function hide(obj) {
   if (gecko || ie4 || opera) {
      obj.style.visibility = "hidden";
   } else if (ns4) {
      obj.visibility = "hide";
   }
   return obj;
}


/*
 * Shows an object
 */
function show(obj) {
   if (gecko || ie4 || opera) {
      obj.style.visibility = "visible";
   } else if (ns4) {
      obj.visibility = "show";
   }
   return obj;
}


/*
 * Either hides or shows an object
 */
function toggleVisible(obj) {
   if (visible(obj)) {
     hide(obj);
   } else {
     show(obj);
   }
   return obj;
}


/*
 * Debugging
 */
function displayPosition(obj) {
   var str = "left: " + findLeftPos(obj) + "\n";
   str += "top: " + findTopPos(obj) + "\n";
   str += "width: " + findWidth(obj) + "\n";
   str += "height: " + findHeight(obj);
   alert(str);
}


/*
 * Debugging
 */
function debugObject(obj, dialog) {
    var str = "";
    for (element in obj) {
        //str = str + "name: " + element + ", val: " + a[element];
        str = str + element + ": " + obj[element];
        if (dialog) {
            str = str + "\n";
        }
        else {
            str = str + "<br>";
        }
    }
    if (dialog) {
        alert(str);
    } else {
        document.write(str);
    }
}
