/**
 * @fileoverview Plugin for Zapatec Grid to input grid data from HTML source.
 *
 * <pre>
 * Copyright (c) 2004-2006 by Zapatec, Inc.
 * http://www.zapatec.com
 * 1700 MLK Way, Berkeley, California,
 * 94709, U.S.A.
 * All rights reserved.
 * </pre>
 */

/**
 * @private Loads data from HTML table source. Utilizes custom "style"
 * attributes.
 *
 * @param {object} objSource Input HTMLElement object
 */
Zapatec.Grid.prototype.loadDataHtml = function(objSource) {
  var objTable = null;
  if (objSource) {
    objTable = objSource;
  } else if (this.container) {
    objTable = Zapatec.Utils.getFirstChild(this.container, 'table');
  }
  if (!objTable) {
    alert("Zapatec.Grid invalid configuration: Can't find source table");
    return;
  }
  var objTbody = Zapatec.Utils.getFirstChild(objTable, 'tbody');
  if (!objTbody) {
    objTbody = objTable;
  }
  var objHeaderRow = Zapatec.Utils.getFirstChild(objTbody, 'tr');
  if (!objHeaderRow) {
    alert("Zapatec.Grid invalid configuration: Can't find header for table");
    return;
  }
  // Remove old data
  this.data = this.newDataHtml(objTable, objHeaderRow);
  this.fields = this.data.fields;
  this.rows = this.data.rows;
  this.rowsIndex = [];
  // Go to first page
  this.currentPage = 0;
  // Get fields
  var objTableCell = Zapatec.Utils.getFirstChild(objHeaderRow, 'th', 'td');
  while (objTableCell) {
    // Add field
    this.fields.push(this.newFieldHtml(objTableCell));
    // Next field
    objTableCell = Zapatec.Utils.getNextSibling(objTableCell, 'th', 'td');
  }
  // Get rows
  var objTableRow = Zapatec.Utils.getNextSibling(objHeaderRow, 'tr');
  while (objTableRow) {
    // Create row
    var objRow = this.newRowHtml(objTableRow);
    // Add row
    this.rows.push(objRow);
    this.rowsIndex.push(objRow);
    // Next row
    objTableRow = Zapatec.Utils.getNextSibling(objTableRow, 'tr');
  }
  // Show grid
  this.show();
};

/**
 * @private Creates new data object from HTML source.
 *
 * @param {object} objTable Table element
 * @param {object} objHeaderRow First table row element in the table
 * @return Data object
 * @type object
 */
Zapatec.Grid.prototype.newDataHtml = function(objTable, objHeaderRow) {
  // Create data object
  var objData = {
    fields: [],
    rows: []
  };
  // Set style
  var strStyle = Zapatec.Widget.getStyle(objTable);
  if (strStyle) {
    objData.style = strStyle;
  }
  var strHeaderStyle = Zapatec.Widget.getStyle(objHeaderRow);
  if (strHeaderStyle) {
    objData.headerStyle = strHeaderStyle;
  }
  return objData;
};

/**
 * @private Creates new field object from HTML table cell element.
 *
 * @param {object} objTableCell Table cell element
 * @return Field object
 * @type object
 */
Zapatec.Grid.prototype.newFieldHtml = function(objTableCell) {
  // Create field object
  var objField = {
    i: this.fields.length,
    title: objTableCell.innerHTML
  };
  // Set data type
  if (this.getTypeByClass) {
    objField.dataType = this.getTypeByClass(objTableCell.className);
  }
  // Set width
  var strWidth = objTableCell.getAttribute('width') + ''; // Convert to string
  if (strWidth.length) {
    objField.columnWidth = strWidth;
  }
  // Set style
  var strStyle = Zapatec.Widget.getStyle(objTableCell);
  if (strStyle) {
    objField.style = strStyle;
  }
  // Set hidden
  var boolHidden = (objTableCell.className.indexOf('zpGridTypeHidden') >= 0);
  if (boolHidden) {
    objField.hidden = boolHidden;
  }
  // Set nosort
  var boolNosort = (objTableCell.className.indexOf('zpGridTypeNosort') >= 0);
  if (boolNosort) {
    objField.nosort = boolNosort;
  }
  // Set notags
  var boolNotags = (objTableCell.className.indexOf('zpGridTypeNotags') >= 0);
  if (boolNotags) {
    objField.notags = boolNotags;
  }
  // Set sortByColumn
  var arrMatch = objTableCell.className.match(/zpGridTypeSortBy(\d+)/);
  if (arrMatch) {
    objField.sortByColumn = arrMatch[1];
  }
  return objField;
};

/**
 * @private Creates new row object from HTML table row element.
 *
 * @param {object} objTableRow Table row element
 * @return Row object
 * @type object
 */
Zapatec.Grid.prototype.newRowHtml = function(objTableRow) {
  // Create new row object
  var objRow = {
    i: this.rowsIndex.length,
    cells: []
  };
  // Set style
  var strStyle = Zapatec.Widget.getStyle(objTableRow);
  if (strStyle) {
    objRow.style = strStyle;
  }
  // Get cells
  var objTableCell = Zapatec.Utils.getFirstChild(objTableRow, 'td', 'th');
  for (var iCol = 0; iCol < this.fields.length; iCol++) {
    // Add cell
    objRow.cells.push(this.newCellHtml(objTableCell, objRow.i, iCol));
    // Next cell
    if (objTableCell) {
      objTableCell = Zapatec.Utils.getNextSibling(objTableCell, 'td', 'th');
    }
  }
  return objRow;
};

/**
 * @private Creates new cell object from HTML table cell element.
 *
 * @param {object} objTableCell Table cell element
 * @param {number} iRow Row id
 * @param {number} iCol Column id
 * @return Cell object
 * @type object
 */
Zapatec.Grid.prototype.newCellHtml = function(objTableCell, iRow, iCol) {
  // Create cell object
  var objCell = {
    i: iCol,
    r: iRow,
    v: ''
  };
  if (objTableCell) {
    // Set value
    objCell.v = objTableCell.innerHTML;
    // Facilitate migrating from an existing <table> to the grid
    if (this.fields[iCol].notags) {
      // Remove tags
      objCell.v = objCell.v.replace(/<[^>]*>/g, '');
    }
    // Set style
    var strCellStyle = Zapatec.Widget.getStyle(objTableCell);
    if (strCellStyle) {
      objCell.style = strCellStyle;
    }
  }
  // Convert cell value
  objCell = this.convertCell(objCell);
  return objCell;
};
