//***************************************************************************************************
//***************************************************************************************************
//**
//** RELATIVE POSITIONS-ERMITTLUNG VON DIV-ELEMENTEN
//** Autor: DF
//** Erstellung: 24.05.2004
//** © rdts AG
//**
//***************************************************************************************************
//***************************************************************************************************


var undefined;


function ElementProps(id, idParentElement) {
  // Attribute
  this._id = undefined;
  this._idParentElement = undefined;
  this._positionTop = undefined;
  this._positionLeft = undefined;
  this._width = undefined;
  this._height = undefined;
  this._positionBottom = undefined;
  this._positionRight = undefined;
  // Initialisierungen
  this.id(id);
  this.idParentElement(idParentElement);
  this.positionTop(this._getPositionTop());
  this.positionLeft(this._getPositionLeft());
  this.width(this._getWidth());
  this.height(this._getHeight());
  this.positionBottom(this._getPositionBottom());
  this.positionRight(this._getPositionRight());
}

// ------------------------------------------------------------
// Zugriffsfunktionen
// ------------------------------------------------------------

// -----------
// id(string)
// -----------
//
// Beschreibung:
// -------------
//

ElementProps.prototype.id = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      focus();
      throw new Error("ArgumentError:noString!");
    }
    this._id = str;
  }
  return this._id;
}

// -----------
// idParentElement(string)
// -----------
//
// Beschreibung:
// -------------
//

ElementProps.prototype.idParentElement = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      focus();
      throw new Error("ArgumentError:noString!");
    }
    this._idParentElement = str;
  }
  return this._idParentElement;
}

// -----------
// positionTop(number)
// -----------
//
// Beschreibung:
// -------------
//

ElementProps.prototype.positionTop = function(n) {
  if (arguments.length) {
    if (!ElementProps.checkNumber(n)) {
      focus();
      throw new Error("ArgumentError:noNumber!");
    }
    this._positionTop = n;
  }
  return this._positionTop;
}

// -----------
// positionLeft(number)
// -----------
//
// Beschreibung:
// -------------
//

ElementProps.prototype.positionLeft = function(n) {
  if (arguments.length) {
    if (!ElementProps.checkNumber(n)) {
      focus();
      throw new Error("ArgumentError:noNumber!");
    }
    this._positionLeft = n;
  }
  return this._positionLeft;
}

// -----------
// positionBottom(number)
// -----------
//
// Beschreibung:
// -------------
//

ElementProps.prototype.positionBottom = function(n) {
  if (arguments.length) {
    if (!ElementProps.checkNumber(n)) {
      focus();
      throw new Error("ArgumentError:noNumber!");
    }
    this._positionBottom = n;
  }
  return this._positionBottom;
}

// -----------
// positionRight(number)
// -----------
//
// Beschreibung:
// -------------
//

ElementProps.prototype.positionRight = function(n) {
  if (arguments.length) {
    if (!ElementProps.checkNumber(n)) {
      focus();
      throw new Error("ArgumentError:noNumber!");
    }
    this._positionRight = n;
  }
  return this._positionRight;
}

// -----------
// width(number)
// -----------
//
// Beschreibung:
// -------------
//

ElementProps.prototype.width = function(n) {
  if (arguments.length) {
    if (!ElementProps.checkNumber(n)) {
      focus();
      throw new Error("ArgumentError:noNumber!");
    }
    this._width = n;
  }
  return this._width;
}

// -----------
// height(number)
// -----------
//
// Beschreibung:
// -------------
//

ElementProps.prototype.height = function(n) {
  if (arguments.length) {
    if (!ElementProps.checkNumber(n)) {
      focus();
      throw new Error("ArgumentError:noNumber!");
    }
    this._height = n;
  }
  return this._height;
}

// ------------------------------------------------------------
// Private Instanzmethoden
// ------------------------------------------------------------

// -------------------------------------------
// _getPositionTop()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode ermittelt relative Top-Position zu einem bestimmten Eltern-Element.
//
// Beispiel:
// ---------
// _getPositionTop();
//

ElementProps.prototype._getPositionTop = function() {
  if (this._existsElement(this.id())){
    var parentDiv = document.getElementById(this.id());
    var topPosition = 0;
    if (this._existsElement(this.idParentElement())){
      while (parentDiv && parentDiv.id != this.idParentElement()){
        if (parentDiv.nodeType == 1){
          topPosition += parseFloat(parentDiv.offsetTop);
        }
        parentDiv = parentDiv.offsetParent;
      }
    }
    else{
      while (parentDiv){
        if (parentDiv.nodeType == 1){
          topPosition += parseFloat(parentDiv.offsetTop);
        }
        parentDiv = parentDiv.offsetParent;
      }
    }
    return parseFloat(topPosition);
  }
  return undefined;
}

// -------------------------------------------
// _getPositionLeft()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode ermittelt relative Left-Position zu einem bestimmten Eltern-Element.
//
// Beispiel:
// ---------
// _getPositionLeft();
//

ElementProps.prototype._getPositionLeft = function() {
  if (this._existsElement(this.id())){
    var parentDiv = document.getElementById(this.id());
    var leftPosition = 0;
    if (this._existsElement(this.idParentElement())){
      while (parentDiv && parentDiv.id != this.idParentElement()){
        if (parentDiv.nodeType == 1){
          leftPosition += parseFloat(parentDiv.offsetLeft);
        }
        parentDiv = parentDiv.offsetParent;
      }
    }
    else{
      while (parentDiv){
        if (parentDiv.nodeType == 1){
          leftPosition += parseFloat(parentDiv.offsetLeft);
        }
        parentDiv = parentDiv.offsetParent;
      }
    }
    return parseFloat(leftPosition);
  }
  return undefined;
}

// -------------------------------------------
// _getPositionBottom()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode ermittelt relative Bottom-Position zu einem bestimmten Eltern-Element.
//
// Beispiel:
// ---------
// _getPositionBottom();
//

ElementProps.prototype._getPositionBottom = function() {
  if (this._existsElement(this.id())){
    var bottomPosition = parseFloat(this.positionTop() +  this.height());
    return parseFloat(bottomPosition);
  }
  return undefined;
}

// -------------------------------------------
// _getPositionRight()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode ermittelt relative Right-Position zu einem bestimmten Eltern-Element.
//
// Beispiel:
// ---------
// _getPositionRight();
//

ElementProps.prototype._getPositionRight = function() {
  if (this._existsElement(this.id())){
    var rightPosition = parseFloat(this.positionLeft() + this.width());
    return parseFloat(rightPosition);
  }
  return undefined;
}

// -------------------------------------------
// _getWidth()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode ermittelt width des Elements.
//
// Beispiel:
// ---------
// _getWidth();
//

ElementProps.prototype._getWidth = function() {
  if (this._existsElement(this.id())){
    var width = parseFloat(document.getElementById(this.id()).offsetWidth);
    return parseFloat(width);
  }
  return undefined;
}

// -------------------------------------------
// _getHeight()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode ermittelt height des Elements.
//
// Beispiel:
// ---------
// _getHeight();
//

ElementProps.prototype._getHeight = function() {
  if (this._existsElement(this.id())){
    var height = parseFloat(document.getElementById(this.id()).offsetHeight);
    return parseFloat(height);
  }
  return undefined;
}

// -------------------------------------------
// _existsElement()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode ermittelt relative Right-Position zu einem bestimmten Eltern-Element.
//
// Beispiel:
// ---------
// _existsElement();
//

ElementProps.prototype._existsElement = function(id) {
  if (document.getElementById(id) != undefined){
    return true;
  }
  return false;
}

// ------------------------------------------------------------
// Öffentliche Klassenmethoden
// ------------------------------------------------------------

// -------------------------------------------
// ElementProps.createElementProps()
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
// ElementProps.createElementProps(id, idParentElement);
//

ElementProps.createElementProps = function(id, idParentElement) {
  return new ElementProps(id, idParentElement);
}

// ------------------------------------------------------------
// toString()
// ------------------------------------------------------------

ElementProps.prototype.toString = function() {
  // zunaechst an Methode der Basisklasse weiterleiten
  return Object.prototype.toString.apply(this);
}

// ------------------------
// ElementProps.checkNumber
// ------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   var b = ElementProps.checkNumber(n);
//
ElementProps.checkNumber = function(n){
  if (!arguments.length){
    return false;
  }
  if (n == undefined){
    return false;
  }
  n = n.toString();
  if (n == '0'){
    return n;
  }
  if (!n.length){
    return false;
  }
  n = n.replace(/,/g,'.');
  if (isNaN(n)){
    return false;
  }
  if (!isFinite(n)) {
    return false;
  }
  n = parseFloat(n);
  return n;
}




