/* Carefully initialise HTML namespace */
if (typeof HTML === "undefined") {
  HTML = {};
}

/*
 * HTML Table class.
 * Make it a quick & easy process to generate a basic HTML Table in JavaScript.
 */
HTML.Table = function(config) {
  var doc = document;
  var columns = config.columns;
  var columnGroup = config.columnGroup;
  var titles = config.titles;
  this.table = doc.createElement("table");
  this.toggle = false;
  /* Copy any values from the config.attributes object literal over (id, class, CSS, etc) */
  var attributes = config.attributes;
  if (typeof attributes === "object") {
    for (var key in attributes) {
      this.table[key] = attributes[key];
    }
  }
  /* Create a colgroup (optional). */
  if (typeof columnGroup !== "undefined" && columns.length > 0) {
    var colgroup  = doc.createElement("colgroup");
    if (typeof columnGroup.span !== "undefined") {
      colgroup.span = columnGroup.span;
    }
    for (var i = 0; i < columnGroup.cols.length; i++) {
      var col = doc.createElement("col");
      var colAttributes = columnGroup.cols[i];
      if (typeof colAttributes !== "undefined") {
        for (var key in colAttributes) {
          col[key] = colAttributes[key];
        }
      }
      colgroup.appendChild(col);
    }
    this.table.appendChild(colgroup);
  }
  /* Table with no headings */
  if (typeof columns === "undefined" || columns.length === 0) {
    return;
  }
  /* XXX: Assumes that the number of headings == columns in each row */
  var header = doc.createElement("thead");
  var headerRow = doc.createElement("tr");
  for (var i = 0; i < columns.length; i++) {
    var heading = doc.createElement("th");
    heading.innerHTML = columns[i];
    /* Add a title if one has been provided. */
    if (titles) {
      if (titles[i]) {
        heading.title = titles[i];
      }
    }
    headerRow.appendChild(heading);
  }
  header.appendChild(headerRow);
  this.table.appendChild(header);
  /* Add an TBODY to keep MSIE happy (will not show content without one) */
  this.tbody = doc.createElement("tbody");
  this.table.appendChild(this.tbody);
};

HTML.Table.prototype.addRow = function(row, attributes) {
  var doc = document;
  var tr = doc.createElement("tr");
  // Add a on/off class for striped rows.
  if (this.toggle) {
  //  tr.className = "b";
  }
  this.toggle = !this.toggle;
  /* Copy any values from the attributes object literal over (id, class, CSS, etc) */
  if (typeof attributes === "object") {
    for (var key in attributes) {
      tr[key] = attributes[key];
    }
  }
  /* Add each column in the row. XXX No support for setting attributes on TDs (yet) */
  for (var i = 0; i < row.length; i++) {
    var column = row[i];
    var td = doc.createElement("td");
    /* Allow for columns to have complex content by embedding functions in the row array */
    if (typeof column === "function") {
      column(td);
    }
    else {
      td.innerHTML = column;
    }
    tr.appendChild(td);
  }
  /* Add the row to the table */
  this.tbody.appendChild(tr);
};

/* Return the TABLE DOM node. */
HTML.Table.prototype.tableNode = function() {
  return this.table;
}

/* HTML TableRow Class */
HTML.TableRow = function() {
  return;
}

