/************************************

  "waka" is a Japanese poetry 
         composed of 31 syllables. 

  (c) 2005 Pixel-Apes.

  -----------------------------------

  Todo markup:

  [ ] todo item
  [+] solved item
  [ ] item with
    [ ] sub items
  [ ] item with notes
      Here goes notes

  See also: http://npj.ru/kuso/315783_razmetka_todo

  There is two-token preset: 
  Todo & Content pair, as "sections" 
  preset do.

*************************************/

function wakaTodo() { this.isTodo = true; }
         wakaTodo.prototype = new wakaToken();
         wakaTodo.prototype.constructor = 
         wakaTodo;

// static
wakaTodo.prototype.getRegexpPart = function()
{
  return "((^|\n)(  )*((\\[[\\-\\.+ õÕxX]?\\])|(\\([\\-\\.+ õÕxX]?\\))) ?.*)";
}
wakaTodo.prototype.completionSymbols = { "x" : true, "X" : true, "+" : true,
                                         "õ" : true, "Õ" : true // russian
                                       };
                             //12     34  5                  6  7                    8
wakaTodo.prototype.isRE = /^\n?((  )*)((\[([+ õÕxX\-\.]?)\])|(\(([+ õÕxX\-\.]?)\))) ?(.*)$/;
wakaTodo.prototype.is = function( outerText )
{
  var matches = this.isRE.exec( outerText );
  if (matches === null) return false;

  var _completed = matches[5]? matches[5]: matches[7];
  if (this.completionSymbols[_completed]) _completed = true;
  else                                    _completed = false;

  return {
            inner : matches[8],
            level : matches[1].length/2,
            completed : _completed,
            text  : outerText
         };
}

// private (up to parent)
wakaTodo.prototype._glue = function()
{
  // glue only if prev is wakaList with lesser deep
  if (this._prev && (this._prev.constructor == this.constructor)
                 && (this._prev._data.level < this._data.level))
    return this._prev.glueChild( this );
  else
    return false;
}

// this is a hack to trim newlines after <li>
wakaTodo.prototype.hack_separateTrimNextNewline = true;

// returns separation from a given "next"
wakaTodo.prototype.separateFromNext   = function( next )
{ return ""; }

// private (compilation)
wakaTodo.prototype._compile   = function()
{
  var inner = this.wf.format( this._data.inner, "default" );
  var glued = this.wf.formatTree( this._tree, "default" );

  var _html = "";
  if (this._prev && !this._prev.isTodo && !this._parent)
    _html = "<br />";

  this._html = _html +
               "<div style='padding-left:20px'>"+ 
               "<input type='checkbox' " +(this._data.completed?" checked='checked' ":"")+ "/>"+
               inner+
               glued+
               "</div>";
}

// make a break, if exporting to text
wakaTodo.prototype.toText = function()
{
  var level = "";

  if (this._prev && !this._prev.isTodo && !this._parent)
    level += "\n";

  for (var i=0; i< this._data.level; i++) 
    level+="  ";
  return level + "["+ (this._data.completed?"+":" ")+"] " + this._data.inner + "\n";
}




/************************************

  here starts second token of a preset,
  which denotes NOTES of a todo,
  or a regular content entry.

*************************************/


function wakaTodoContent() {}
         wakaTodoContent.prototype = new wakaToken();
         wakaTodoContent.prototype.constructor = 
         wakaTodoContent;

// private (up to parent)
wakaTodoContent.prototype._glue = function()
{
  // glue always, if there is to what.
  if (this._prev && this._prev.isTodo)
  {
    var todoNoteText = "";
    // find if there is empty line inside
    var matches = this._data.match( /(^|\n)[\r\t ]*\n/ );
    if (matches != null)
    {  
      // get text for todoNote
      var pos = this._data.indexOf( matches[0] );
      todoNoteText = this._data.substr(0, pos);
      if (matches[1]) todoNoteText+="\n";
      
      if (pos == 0) return false;

      // rip that text out from Content
      this._data = this._data.substr( pos + matches[0].length );
    }
    else
    {
      todoNoteText = this._data;
      this._data = "";
    }

    // spawn new Content and glue it up to previous todo
    var todoNote = new this.constructor();
    todoNote.bind( this.wf );
    todoNote.setNextPreset( this._nextPreset );
    todoNote.strictDontGlue = true;
    todoNote.build( todoNoteText );
    this._prev.glueChild( todoNote );

    // NB: we glue some part of our content, but not all content.
    if (this._data == "") return true;
    else                  return false;
  }
  return false;
}

// private: compile from struct to html
wakaTodoContent.prototype._compile   = function()
{
   if (this._nextPreset)
     this._html = this.wf.format( this._data, this._nextPreset );
   else
     this._html = this._data;

   if (this._parent.isTodo)
   {
     this._html = "<div><small>"+this._html+"</small></div>";
   }
   
   this._compiled = true;
}



