// script.aculo.us builder.js v1.8.1, Thu Jan 03 22:07:12 -0500 2008

// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
//
// script.aculo.us is freely distributable under the terms of an MIT-style license.
// For details, see the script.aculo.us web site: http://script.aculo.us/

var Builder = {
  NODEMAP: {
    AREA: 'map',
    CAPTION: 'table',
    COL: 'table',
    COLGROUP: 'table',
    LEGEND: 'fieldset',
    OPTGROUP: 'select',
    OPTION: 'select',
    PARAM: 'object',
    TBODY: 'table',
    TD: 'table',
    TFOOT: 'table',
    TH: 'table',
    THEAD: 'table',
    TR: 'table'
  },
  // note: For Firefox < 1.5, OPTION and OPTGROUP tags are currently broken,
  //       due to a Firefox bug
  node: function(myElementName) {
  
    myElementName = myElementName.toUpperCase();
    
    // try innerHTML approach
    var myParentTag = this.NODEMAP[myElementName] || 'div';
    var myParentElement = document.createElement(myParentTag);
    try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
      myParentElement.innerHTML = "<" + myElementName + "></" + myElementName + ">";
    } catch(e) {
    }
    var myElement = myParentElement.firstChild || null;
      
    // see if browser added wrapping tags
    if(myElement && (myElement.tagName.toUpperCase() != myElementName))
      myElement = myElement.getElementsByTagName(myElementName)[0];
    
    // fallback to createElement approach
    if(!myElement) myElement = document.createElement(myElementName);
    
    // abort if nothing could be created
    if(!myElement) return;


    // attributes (or text)
    if(arguments[1]){
      if(this._isStringOrNumber(arguments[1]) ||
        (arguments[1] instanceof Array) ||
        arguments[1].tagName) {
          this._children(myElement, arguments[1]);
        } else {
          var attrs = this._attributes(arguments[1]);
          if(attrs.length) {
            try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
              myParentElement.innerHTML = "<" +myElementName + " " +
                attrs + "></" + myElementName + ">";
            } catch(e) {
            }
          
            myElement = myParentElement.firstChild || null;
            // workaround firefox 1.0.X bug
            
            
            if(!myElement) {
              myElement = document.createElement(myElementName);
              for(attr in arguments[1])
              {
                try
                {
                  myElement[attr == 'class' ? 'className' : attr] = arguments[1][attr];
                } catch (e) {}
              }
            }
            if(myElement.tagName.toUpperCase() != myElementName)
              myElement = myParentElement.getElementsByTagName(myElementName)[0]
          }
        } 
    }

    // text, or array of children
    if(arguments[2])
      this._children(myElement, arguments[2]);
      
    
    return myElement;
  },
  _text: function(myText) {
     return document.createTextNode(myText);
  },
  
  _appendChild: function(myElement, myText) {
    try
    {
      myElement.appendChild(myText);
    }
    catch (e)
    {
      try
      {
        myElement.text = myText;
      }
      catch(e)
      {
        alert('BUILDER -> APPEND CHILD -> MYELEMENT -> TEXT -> ' + e.message);
      }
    }
    return myElement
  },

  ATTR_MAP: {
    'className': 'class',
    'htmlFor': 'for'
  },

  _attributes: function(attributes) {
    var attrs = [];
    for(attribute in attributes)
      attrs.push((attribute in this.ATTR_MAP ? this.ATTR_MAP[attribute] : attribute) +
          '="' + attributes[attribute].toString().escapeHTML().gsub(/"/,'&quot;') + '"');
    return attrs.join(" ");
  },
  _children: function(myElement, myChildren) {
    if(myChildren.tagName) {
      Builder._appendChild(myElement, myChildren);
      return;
    }
    if(typeof myChildren=='object') { // array can hold nodes and text
      myChildren.flatten().each( function(e) {
        if(typeof e=='object')
          Builder._appendChild(myElement, e);
        else
          if(Builder._isStringOrNumber(e))
            Builder._appendChild(myElement, Builder._text(e));
      });
    } else
      if(Builder._isStringOrNumber(myChildren))
        Builder._appendChild(myElement, Builder._text(myChildren));
  },
  _isStringOrNumber: function(param) {
    return(typeof param=='string' || typeof param=='number');
  },
  build: function(myHtml) {
    var myElement = this.node('div');
    $(myElement).update(myHtml.strip());
    return myElement.down();
  },
  dump: function(myScope) { 
    if(typeof myScope != 'object' && typeof myScope != 'function') myScope = window; //global scope 
  
    var myTags = ("A ABBR ACRONYM ADDRESS APPLET AREA B BASE BASEFONT BDO BIG BLOCKQUOTE BODY " +
      "BR BUTTON CAPTION CENTER CITE CODE COL COLGROUP DD DEL DFN DIR DIV DL DT EM FIELDSET " +
      "FONT FORM FRAME FRAMESET H1 H2 H3 H4 H5 H6 HEAD HR HTML I IFRAME IMG INPUT INS ISINDEX "+
      "KBD LABEL LEGEND LI LINK MAP MENU META NOFRAMES NOSCRIPT OBJECT OL OPTGROUP OPTION P "+
      "PARAM PRE Q S SAMP SCRIPT SELECT SMALL SPAN STRIKE STRONG STYLE SUB SUP TABLE TBODY TD "+
      "TEXTAREA TFOOT TH THEAD TITLE TR TT U UL VAR").split(/\s+/);
  
    myTags.each( function(myTag){ 
      myScope[myTag] = function() { 
        return Builder.node.apply(Builder, [myTag].concat($A(arguments)));  
      } 
    });
  }
}
