
var xDOM = { isDOMCSS1: false, isnn4: false };

xDOM.findLayer = function(doc, id) {
  for (var i = 0; i < doc.layers.length; i++) {
    var layer = doc.layers[i];
    if (layer.id && layer.id == id) return layer;
    var obj = this.findLayer(layer.document, id);
    if (obj != null) return obj;
  }
  return null;
};


// Create cross platform func getElementById(id)
if (typeof(document.getElementById) != "undefined") {
  xDOM.isDOMCSS1 = true;
}
else if (typeof(document.all) != "undefined") {
  document.getElementById = function(id) {
    return document.all[id];
  };
  xDOM.isDOMCSS1 = true;
}
else if (typeof(document.layers) != "undefined") {
  xDOM.layerCache = new Object();
  document.getElementById = function(id) {
    if (typeof(xDOM.layerCache[id]) == "undefined") xDOM.layerCache[id] = xDOM.findLayer(document, id);
    return xDOM.layerCache[id];
  };
  xDOM.isnn4 = true;
}

// xDOM element object
xDOM.element = function(id) {
  this.id = id;
  this.obj = document.getElementById(id);
};
xDOM.element.prototype.id = null;
xDOM.element.prototype.obj = null;
xDOM.element.prototype.document = function() {
  var doc = this.obj.document;
  return (typeof(doc) != 'undefined') ? doc : this.obj.ownerDocument;
}

// xDOM element accessor
xDOM.elementCache = new Object();
xDOM.getElement = function(id) {
  if (typeof(xDOM.elementCache[id]) == "undefined") xDOM.elementCache[id] = new xDOM.element(id);
  return xDOM.elementCache[id];
};

// DOM CSS1 Functions
if (xDOM.isDOMCSS1) {

xDOM.element.prototype.left = function() { 
  var left = this.obj.style.left;
  return (left == '' || left.indexOf('px') == -1) ? 0 : parseInt('0' + left, 10);

};
xDOM.element.prototype.setLeft = function(value) { this.obj.style.left = value + 'px'; };
xDOM.element.prototype.top = function() {
  var top = this.obj.style.top;
  return (top == '' || top.indexOf('px') == -1) ? 0 : parseInt('0' + top, 10);
};
xDOM.element.prototype.setTop = function(value) { this.obj.style.top = value + 'px'; };
xDOM.element.prototype.offset = function(elm, offset) {
  if (typeof(elm) == "undefined") elm = this.obj;
  var x = 0;
  for (; elm; elm = elm.offsetParent) { x += elm[offset]; }
  return x;
};
xDOM.element.prototype.pageX = function(elm) { return this.offset(elm, "offsetLeft"); };
xDOM.element.prototype.setPageX = function(value) { this.setLeft(value - this.pageX(this.obj.offsetParent)); };
xDOM.element.prototype.pageY = function(elm) { return this.offset(elm, "offsetTop"); };
xDOM.element.prototype.setPageY = function(value) { this.setTop(value - this.pageY(this.obj.offsetParent)); };
xDOM.element.prototype.height = function() {
  var height = this.obj.style.height;
  return (height == '' || height.indexOf('px') == -1) ? this.obj.offsetHeight : parseInt('0' + height, 10);

};
xDOM.element.prototype.setHeight = function(value) { this.obj.style.height = value + 'px'; };
xDOM.element.prototype.width = function() {
  var width = this.obj.style.width;
  return (width == '' || width.indexOf('px') == -1) ? this.obj.offsetWidth : parseInt('0' + width, 10);

};
xDOM.element.prototype.setWidth = function(value) { this.obj.style.width = value + 'px'; };
xDOM.element.prototype.visibility = function() { return this.obj.style.visibility; };
xDOM.element.prototype.setVisibility = function(value) { this.obj.style.visibility = value; };
xDOM.element.prototype.setInnerHTML = function(value) {
    if (typeof(this.obj.innerHTML) != 'undefined') {
        var s = typeof(value) != 'undefined' ? value.toString() : "";
        this.obj.innerHTML = (s.length > 0 ? s : " ");
    }
};

}

// NN4 Functions
else if (xDOM.isnn4) {

xDOM.element.prototype.left = function() { return this.obj.left; }
xDOM.element.prototype.setLeft = function(value) { this.obj.left = value; };
xDOM.element.prototype.top = function() { return this.obj.top; };
xDOM.element.prototype.setTop = function(value) { this.obj.top = value; };
xDOM.element.prototype.pageX = function() { return this.obj.pageX; };
xDOM.element.prototype.setPageX = function(value) { this.obj.pageX = value; };
xDOM.element.prototype.pageY = function() { return this.obj.pageY; };
xDOM.element.prototype.setPageY = function(value) { this.obj.pageY = value; };
xDOM.element.prototype.height = function() {
  var doc = this.obj.document;
  return (doc && doc.height) ? doc.height : this.obj.clip.height;
};
xDOM.element.prototype.setHeight = function(value) { this.obj.clip.height = value; };
xDOM.element.prototype.width = function() {
  var doc = this.obj.document;
  return (doc && doc.width) ? doc.width : this.obj.clip.width;
};
xDOM.element.prototype.setWidth = function(value) { this.obj.clip.width = value; };
xDOM.element.prototype.visibility = function() {
  switch(this.obj.visibility) {
    case 'hide': return 'hidden';
    case 'show': return 'visible';
  }
  return '';
};
xDOM.element.prototype.setVisibility = function(value) {
  switch(value) {
    case 'hidden': value = 'hide'; break;
    case 'visible': value = 'show'; break;
    case 'inherit': break;
    default: value = 'show'; break;
  }
  this.obj.visibility = value;
};
xDOM.element.prototype.setInnerHTML = function(value) { var doc = this.obj.document;  if (doc) { doc.write(value); doc.close(); } };

}

