/*
 * xElement.js, 1.4, 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 frames problem
 * - If html begin with <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
 *   we must use format NNNpx for element position - Gecko work around.
 *   If we use only NNN Gecko convert value to NNNpt - but this might do not work
 * - 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
 */

xElement.prototype.use_style_prop = 0;
xElement.prototype.use_clip_prop  = 0;
xElement.prototype.use_pixel_prop = 0;
xElement.prototype.prop_type      = 0;


function xElement(_id) {
  this.element = xDoc.getElement(_id);
  if (this.element == undefined) {
    alert('xElement: element "' + _id + '" not found!');
    return;
  }
  this.id = _id;

  if (this.element.style != undefined) {
    this.style = this.element.style;
    this.use_style_prop = 1;
  }
  else
    this.style = this.element;

  if (this.style == undefined) {
    alert('xElement: missing style for element "' + this.id + '" !');
    return;
  }

  if (this.element.clip != undefined) {
    this.clip = this.element.clip;
    this.use_clip_prop = 1;
  }
  else {
    if (this.style.pixelWidth != undefined) {
      if (this.element.offsetWidth == undefined) {
        /* we prefer to use style and offsetWidth/Height */
        this.use_pixel_prop = 1;
      }
    }
  }
  return;
}


if (document.layers)
  xElement.prototype.show = function () { this.style.visibility = "show"   ; }
else
  xElement.prototype.show = function () { this.style.visibility = "visible"; }


if (document.layers)
  xElement.prototype.hide = function () { this.style.visibility="hide"  ; }
else
  xElement.prototype.hide = function () { this.style.visibility="hidden"; }


xElement.prototype.parseInt = function (_val) {
  var _v = window.parseInt(_val);
  if (_v == NaN) {
    /* NS 4.7x bug:
      if value is in format .nn parseInt return NaN !!!
    */
    if (-1 < _val && _val < 1)
      return 0;
  }
  return _v;
}


/*
 * Notes:
 * - When "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" ....>" is
 *   defined Gecko accept only position with 'px' at end !!!
 * - NS 4.7x cann`t use format NNNpx (no style) and this class define 'pseudo' style property!!!
 * - Opera 6.x cann`t use format NNNpx for style properties !!!
 */
if (document.layers)
  xElement.prototype.toPixel = function (_val) { return this.parseInt(_val)       ; }
else if (navigator.userAgent.indexOf('Opera') > 0)
  xElement.prototype.toPixel = function (_val) { return this.parseInt(_val)       ; }
else
  xElement.prototype.toPixel = function (_val) { return this.parseInt(_val) + 'px'; }


xElement.prototype.left = function (_v) {
  if (this.use_pixel_prop == 1) {
    if (_v != undefined)
      this.style.pixelLeft = this.parseInt(_v);
    return this.style.pixelLeft;
  }
  /* else */
  if (_v != undefined)
    this.style.left = this.toPixel(_v);
  _v = this.style.left;
  if (_v == '')
    return 0;
  /* else */
  /* TODO: to convert from px,pt,cm,etc to px ! */
  return this.parseInt(_v);
}


xElement.prototype.top = function (_v) {
  if (this.use_pixel_prop == 1) {
    if (_v != undefined)
      this.style.pixelTop = this.parseInt(_v);
    return this.style.pixelTop;
  }
  /* else */
  if (_v != undefined)
    this.style.top = this.toPixel(_v);
  _v = this.style.top;
  if (_v == '')
    return 0;
  /* else */
  /* TODO: to convert from px,pt,cm,etc to px ! */
  return this.parseInt(_v);
}



var __xElement_height_alert = 1;
xElement.prototype.height = function(_v) {
  if (this.use_clip_prop == 1) {
    var o = this.element.clip;
    if (_v != undefined)
      o.height = this.parseInt(_v);
    return o.height;
  }
  /* else */
  if (this.use_pixel_prop == 1) {
    if (_v != undefined) {
      this.style.pixelHeight = this.parseInt(_v);
    }
    return this.style.pixelHeight;
  }
  /* else */
  if (_v != undefined) {
    this.style.height = this.toPixel(_v);
  }

  _v = (this.element.offsetHeight != undefined)
    ? this.element.offsetHeight
    : this.element.clientHeight
  ;
  if (_v == 0) {
    if (__xElement_height_alert > 0 ) {
      __xElement_height_alert--;
      alert('xElement.height() return zero');
    }
  }
  return _v;
}


xElement.prototype.heightAlert = function(_) {
  var msg = 'xElement.heightAlert():\n';
  if (this.element.offsetHeight != undefined) msg += '\nelement.offsetHeight : ' + this.element.offsetHeight;
  if (this.element.clientHeight != undefined) msg += '\nelement.clientHeight : ' + this.element.clientHeight;
  if (this.style.pixelHeight    != undefined) msg += '\nstyle.pixelHeight    : ' + this.style.pixelHeight   ;
  if (this.style.height         != undefined) msg += '\nstyle.height         : ' + this.style.height        ;
  if (this.clip                 != undefined) msg += '\nclip.height          : ' + this.clip.height         ;

  alert(msg);
}


var __xElement_width_alert = 1;
xElement.prototype.width = function(_v) {
  if (this.use_clip_prop == 1) {
    var o = this.element.clip;
    if (_v != undefined)
      o.width = this.toPixel(_v);
    return o.width;
  }
  /* else */
  if (this.use_pixel_prop == 1) {
    if (_v != undefined) {
      this.style.pixelWidth = this.parseInt(_v);
    }
    return this.style.pixelWidth;
  }
  /* else */
  if (_v != undefined) {
    this.style.width = this.toPixel(_v);
  }

  _v = (this.element.offsetWidth != undefined)
    ? this.element.offsetWidth
    : this.element.clientWidth
  ;
  if (_v == 0) {
    if (__xElement_width_alert > 0) {
      __xElement_width_alert--;
      alert('xElement.width() return zero');
    }
  }
  return _v;
}


xElement.prototype.right = function(_v) {
  var W = this.width();
  if (_v != undefined) {
    this.left(_v - W);
  }
  return (this.left() + W);
}


xElement.prototype.bottom = function(_v) {
  var H = this.height();
  if (_v != undefined) {
    this.top(_v - H);
  }
  return (this.top() + H);
}


xElement.prototype.moveTo = function (x,y) {
  if (this.use_pixel_prop == 1) {
    this.style.pixelLeft = this.parseInt(x);
    this.style.pixelTop  = this.parseInt(y);
  }
  else {
    this.style.left = this.toPixel(x);
    this.style.top  = this.toPixel(y);
  }
}


xElement.prototype.centerTo = function(_x, _y) {
  var W2 = this.width () / 2;
  var H2 = this.height() / 2;

  this.moveTo(_x - W2, _y - H2);
}

