/************************************

  "waka" is a Japanese poetry 
         composed of 31 syllables. 

  (c) 2005 Pixel-Apes.

  -----------------------------------

  Tables preset. 
  (two token classes in one file)
  Used to parse tables. First token
  denotes whole table and switches
  formatter to another preset, where
  only "wakaTablesRow" tokens can appear.

  #|
  || cell1 | cell2 ||
  || cell3 | cell4 ||
  |#

  #|| ... ||#

*************************************/


// ------------------------- TABLES TOKEN ---------------------
function wakaTables() {}
         wakaTables.prototype = new wakaToken();
         wakaTables.prototype.constructor = 
         wakaTables;

// static
wakaTables.prototype.getRegexpPart = function()
{
  return "(#\\|{1,2}(.|\n)*?\\|{1,2}#)";
}
                            // 1        23
wakaTables.prototype.isRE = /^#(\|{1,2})\n*((.|\n)*?)\n*\|{1,2}#$/;
wakaTables.prototype.is = function( outerText )
{
  var matches = this.isRE.exec( outerText );
  if (matches === null) return false;

  return {
            inner : matches[2],
            type  : matches[1].length,
            text  : outerText
         };
}

// private (compilation)
wakaTables.prototype.cssMap     = [ "", "usertable", "dtable" ];
wakaTables.prototype._compile   = function()
{
  var _css = this.wf.cssPrefix + this.cssMap[ this._data.type ] + this.wf.cssPostfix;

  var inner = this.wf.format( this._data.inner, "table" );
  this._html = '<table class="'+_css+'">'+inner+'</table>';
}


// ------------------------------ ROW + CELL TOKEN ----------------------------
function wakaTablesRow() {}
         wakaTablesRow.prototype = new wakaToken();
         wakaTablesRow.prototype.constructor = 
         wakaTablesRow;

// static
wakaTablesRow.prototype.getRegexpPart = function()
{
  return "\\|\\|(.|\n)*?\\|\\|";
}
                               //     12
wakaTablesRow.prototype.isRE = /^\|{2}((.|\n)*?)\|{2}$/;
wakaTablesRow.prototype.is = function( outerText )
{
  var matches = this.isRE.exec( outerText );
  if (matches === null) return false;

  var _cells = matches[1].split("|");
  for( var i in _cells )
    _cells[i] = _cells[i].replace(/^ ?\n+/, "").replace(/\n+ ?$/, "");

  return {
            cells    : _cells,
            colSpan  : [ 1 ],
            text     : outerText
         };
}

// todo: override "build" to build rows BEFORE compiling

// private (up to parent)
wakaTablesRow.prototype._glue = function()
{
  this._countColumns();

  if (!this._prev) return false;
  if (this._prev.constructor != this.constructor) return false;

  // need to fix colspan
  if (this._colCount > this._prev._colCount)
  {
    // todo: make a prettier logic of correct colspanning
    this._prev._data.colSpan[0]++;
   
    // continue fixing colspan
    return this._prev._glue();
  }

  // need to expand this row
  if (this._colCount < this._prev._colCount)
    this._data.colSpan[ this._data.cells.length-1 ] = this._prev._colCount - this._colCount+1;
  this._countColumns();

  return false;
}

wakaTablesRow.prototype._countColumns = function()
{
  var count=0;
  for( var i=0; i< this._data.cells.length; i++ )
    if (this._data.colSpan[i])
      count+=this._data.colSpan[i];
    else
      count++;

  this._colCount = count;
}

// private (compilation)
wakaTablesRow.prototype._compile   = function()
{
  // break into cells
  var cells = this._data.cells;
  var cellData = [];

  // compile data
  for( var i in cells )
  {
    var colspan = '';
    if (this._data.colSpan[i] > 1) colspan = ' colspan="'+this._data.colSpan[i]+'"';

    var content = this.wf.format( cells[i], this._nextPreset );

    cellData[i] = '<td'+colspan+'>' + content + '</td>';
  }

  this._html = "<tr>" + cellData.join("") + "</tr>";
}

