/*
 * xDocument.js, 1.3, 2005-05-16
 *
 * Copyright (c) 2002-2005 Roumen Petrov.
 *
 * Roumen Petrov grants you a royalty free license to use, modify or
 * distribute this software provided that this copyright notice appears
 * on all copies.  This software is provided "AS IS," without a
 * warranty of any kind.
 *
 * Notes:
 * - Do not put script in HTML head section - NS4 and frames problem
 * - works on:
 *   - Gecko 0.9.x/1.x (Mozilla, Firefox, Netscape 6x/7x, Galeon)
 *   - IE 5.x/6.x
 *   - Netscape 4.7x
 *   - Opera 5.x/6.x/7.x/8.x
 *   - Konqueror 2.2.x/3.x
 */

function xDocument() {
}


function __NS4getElement(_doc,_id) {
  var k;
  var e;

  if (_doc == undefined) {
    return;
  }

  e = _doc[_id];
  if (e != undefined) {
    return e;
  }

  e = _doc.layers[_id];
  if (e != undefined) {
    return e;
  }

  for (k=0; k < _doc.layers.length; k++) {
    e = __NS4getElement(_doc.layers[k].document,_id);
    if (e != undefined) {
      return e;
    }
  }

  return;
}


if (document.getElementById) {
  xDocument.prototype.getElement = function (_id) {
    var e = document.getElementById(_id);
    if (e != null)
      return e;
    /* Gecko work around:
       - getElementById return null for img[ages] !!!
     */
    return document[_id];
  }
} else if (document.all) {
  xDocument.prototype.getElement = function (_id) {
    return document.all[_id];
  }
} else if (document.layers) {
  xDocument.prototype.getElement = function (_id) {
    var e = __NS4getElement(document,_id);
    return e;
  }
} else {
  alert ('xDocument:Unsupported browser');
}


xDocument.prototype.visibleLeft  = function () { return (window.pageXOffset != undefined) ? window.pageXOffset : document.body.scrollLeft; }
xDocument.prototype.visibleTop   = function () { return (window.pageYOffset != undefined) ? window.pageYOffset : document.body.scrollTop ; }


xDocument.prototype.visibleWidth  = function () { return (window.innerWidth  != undefined) ? window.innerWidth : document.body.clientWidth ; }
xDocument.prototype.visibleHeight = function () { return (window.innerHeight != undefined) ? window.innerHeight: document.body.clientHeight; }


xDocument.prototype.visibleRight  = function () {
  var v = this.visibleLeft() + this.visibleWidth()
  if (navigator.appName == 'Netscape' ) {
    /*
      For NS 4.x and Gecko - without scrollbar width/height.
      Value of 15 is enough (otherwise depend of Operating System).
    */
    return (v - 15);
  }
  return v;
}


xDocument.prototype.visibleBottom = function () {
  var v = this.visibleTop()  + this.visibleHeight()
  if (navigator.appName == 'Netscape' ) {
    /*
      For NS 4.x and Gecko - without scrollbar width/height.
      Value of 15 is enough (otherwise depend of Operating System).
    */
    return (v - 15);
  }
  return v;
}


var xDoc = new xDocument;

