/****
 ** .OBJECT = AmeliaElement
 ** .PURPOSE = Create and manipulate new Element Object (DOM or JS)
 ** (c) 2008 amelia timbang catalano 
 **
 ****/
 
/**
 * .method = AmeliaElement constructor
 * .purpose = concatenate infinite number of strings in array
 * .args = array of strings
 * .returns = concatenated string
 *
 **/ 
function AmeliaElement(obj) {
  this.elID = obj['elID'];
  this.attr = obj['attr'];
  this.elType = obj['elType']; /* no need for Text Node */
  this.children = obj['children'];
  this.text = obj['text'];
  this.returnDOMObject = obj['returnDOMObject'];
  
  if (typeof(this.elType) === "string") {

    this.newElement = document.createElement(this.elType);
    if (this.attr) {
      for (a in this.attr) {

        if (a === 'onmouseover' || a === 'onclick' || a === 'onmouseout' || a === "className") {
          eval('this.newElement.' + a + ' = this.attr[a]');
        }
        else this.newElement.setAttribute(a,this.attr[a]);
      }
    }

    if (this.children) {
      for (var c = 0; c < this.children.length; c++) {
        this.addChild(this.children[c]);
      }
    }

    if (this.text) {
      var t = document.createTextNode(this.text);
      this.addChild(t);
    }
    
    //Return DOM element
    if (this.returnDOMObject) {
      return this.newElement 
    }
    
    //Return the AmeliaElement object
    else {
      return this;
    }   
  }

}

/**
 * .method = get
 * .purpose = get a DOM element by ID
 * .args = string/number
 * .returns = concatenated string
 *
 **/
AmeliaElement.prototype.get = function (s){
  if (typeof(s) === 'string' || typeof(s) === 'number') {
    try { 
      return document.getElementById(s);
    }
    catch(e) {
      alert("FAILURE: cannot get element");
    }
  }
}

