/************************************

  "waka" is a Japanese poetry 
         composed of 31 syllables. 

  (c) 2005 Pixel-Apes.

  -----------------------------------

  Email quotation:

  > Level one quotation
  >> Level two one
  >>> Level three and soon

  could be tuned by next parameters:

  wakaEmailQuotes.prototype.htmlQuoteSign -- each line would start with this character/string
  wakaEmailQuotes.prototype.htmlQuoteGlue -- if "true", several lines with one level 
                                             would be treated as one

*************************************/

function wakaEmailQuotes() {}
         wakaEmailQuotes.prototype = new wakaToken();
         wakaEmailQuotes.prototype.constructor = 
         wakaEmailQuotes;

// parameters for symbolic decoration
/*
wakaEmailQuotes.prototype.htmlQuoteSign = "&rsaquo; ";
wakaEmailQuotes.prototype.htmlQuoteGlue = false;
*/
// parameters for CSS-decoration
wakaEmailQuotes.prototype.htmlQuoteSign = "";
wakaEmailQuotes.prototype.htmlQuoteGlue = true;

// static
wakaEmailQuotes.prototype.getRegexpPart = function()
{
  return "((^|\n) ?(> ?)+.+)";
}
                                    //12       34      5
wakaEmailQuotes.prototype.isRE = /^\n?((^|\n) ?((> ?)+)(.+))$/;
wakaEmailQuotes.prototype.is = function( outerText )
{
  var matches = this.isRE.exec( outerText );
  if (matches === null) return false;

  var levelStr = matches[3].replace( /\s/g, "" );

  return {
            inner : matches[5],
            level : levelStr.length, // numeric representation
            LVL   : levelStr,        // original string for some reason
            text  : outerText
         };
}

// private (up to parent)
wakaEmailQuotes.prototype._glue = function()
{
  // glue only if prev is wakaEmailQuote 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 quoted div. 
wakaEmailQuotes.prototype.hack_separateTrimNextNewline = true;


// returns separation from a given "next"
wakaEmailQuotes.prototype.separateFromNext   = function( next )
{
  var result = "";
  if (!next || next.constructor != this.constructor 
            || next._data.level != this._data.level
            || !this.htmlQuoteGlue)
  {
    return "</div>";
  }

  return result;
}

// private (compilation)
wakaEmailQuotes.prototype._compile   = function()
{
  var inner = this.wf.format( this._data.inner, "default" );
  var glued = this.wf.formatTree( this._tree, "default" );

  var listStartIfAny = "";
  if (!this._prev || (this._prev.constructor != this.constructor )
                  || (this._prev._data.level != this._data.level)
                  || !this.htmlQuoteGlue) 
  {

    var sign = "";
    if (this.htmlQuoteSign != "") 
      for (var i=0; i< this._data.level; i++) 
        sign+=this.htmlQuoteSign;
    var _css  = this.wf.cssPrefix + "email" + this._data.level%2 + this.wf.cssPostfix;
    listStartIfAny += '<div class="'+_css+'">'+sign;

  }
      
  this._html = listStartIfAny+
               inner+" "+
               glued;
}
