//      _____     ==============================================================
//   o | O  O| o   DOMokun! - Simple tag generation with Javascript
//    \| vvvv|/    Created by Mattt Thompson (http://matttthompson.com)
//     | ^^^^|     Released under the MIT Licence, 2008
//     |_| |_|    ==============================================================

// Here's how you use it:
//
// $DOM(_tag_name);
// $DOM(_tag_name, _content);
// $DOM(_tag_name, _content, _options)

var $DOM = function() {
  var attributes = {
    tag_name: arguments[0],
    content:  ((!! arguments[1]) ? new Array(arguments[1]) : [ ]),
    options:  arguments[2] || {}
  };
  
  attributes.append = function(){
    for(i = 0; i < arguments.length; i++)
    {
      attributes.content.push(arguments[i]);
    }
    return attributes;
  };
  
  attributes.clear = function(){
    attributes.content = [];
    return attributes;
  };
  
  attributes.setContent = function(_content){
    attributes.content = _content;
    return attributes;
  };
  
  attributes.setOptions = function(_options){
    attributes.options = _options;
    return attributes;
  };
  
  attributes.toString = function(){
    output = '<' + attributes.tag_name;
    for(key in attributes.options)
    {
      output += ' ' + key + '=' + '"' + attributes.options[key] + '"';
    }
    
    if(attributes.content)
    {
      output += '>';
      switch(attributes.content.constructor)
      {
      case Array :
        for(i in attributes.content)
        {
          output += attributes.content[i].toString();
        }
        break;
      case String :
        output += attributes.content;
        break;
      }
      output += '</' + attributes.tag_name + '>';
    }
    else
    {
      output += '/>';
    }
    
    return output;
  };
  attributes.value = function(){
    return attributes.toString();
  }
  
  
  return attributes;
};