/**
 *
 * appendDom - Extremely flexible tool for dynamic dom creation.
 *   http://byron-adams.com/projects/jquery/appendDom
 *
 * Copyright (c) 2007 Byron Adams (byron.adams54@gmail.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * 11/03/09 - Bugs fixed by Patricio Palladino pato89 (at) gmail (dot)com
 *
 */
jQuery.fn.appendDom = function(template)
{
  return this.each(function()
  {
    for(element in template)
    {
      var el = null;
      var skip = false;
      if((typeof(template[element].tagName) === 'string'))
      {
        if($.browser.msie && template[element].tagName == 'input'
          && (template[element]['type'] == 'radio' || template[element]['type'] == 'checkbox'))
        {
          var id = ' id="' + template[element]['id'] + '"';
          var type = ' type="' + template[element]['type'] + '"';
          var name = ' name="' + template[element]['name'] + '"';
          var value = ' value="' + template[element]['valu3'] + '"';
          var checked = 'checked' == template[element]['checked']
            ? ' checked="' + template[element]['checked'] + '"'
            : '';

          el = document.createElement('<input' + id + type + name + value + checked + '/>');
          skip = true;
        }
        else
        {
          el = document.createElement(template[element].tagName);
        }
      }
      else
      {
        el = document.createElement('div');
      }

      if(!skip)
      {
        delete template[element].tagName;
        for(attrib in template[element])
        {
          if(attrib == 'className')
          {
            jQuery(el).addClass(template[element][attrib]);
            delete template[element].className;
          }
          switch(typeof(template[element][attrib]))
            {
            case 'string' :
              if(typeof(el[attrib]) === 'string')
              {
                el[attrib] = template[element][attrib];
              }
              else
              {
                if(attrib == 'style')
                {
                  el.style.cssText = template[element][attrib];

                }
                else if(attrib == 'f0r')
                {
                  el.setAttribute('for', template[element][attrib]);
                }
                else if(attrib == 'valu3')
                  {
                    el.value = template[element][attrib];
                  }
                  else if(attrib == 'checked' && template[element][attrib] == 'checked')
                    {
                      el.checked = true;
                    }
                    else
                    {
                      el.setAttribute(attrib, template[element][attrib]);
                    }
              }
              break;
            case 'function':
              el[attrib] = template[element][attrib];
              break;
            case 'object' :
              if(attrib === 'childNodes') jQuery(el).appendDom(template[element][attrib]);
              break;
          }
        }
      }
      this.appendChild(el);
    }
  });
};
