/************************************

  "waka" is a Japanese poetry 
         composed of 31 syllables. 

  (c) 2005 Pixel-Apes.

  -----------------------------------

  Special sections preset part.
  Do replace itself by contents of 
  its rendering subtree.

*************************************/

function wakaDoSections() { }
         wakaDoSections.prototype = new wakaSections();
         wakaDoSections.prototype.constructor = 
         wakaDoSections;

// static
wakaDoSections.prototype.static_onShowHide = function( elHeaderDiv )
{
  var elDiv = elHeaderDiv.parentElement;
  if (!elDiv) elDiv = elHeaderDiv.parentNode;
  var state = elDiv.className.match( /section-hidden/ );
  var folded = true; 
  if (state === null)
  {
    elDiv.className = elDiv.className.replace( /section/, "section-hidden" );
    elHeaderDiv.title = "Click to show section content";
  }
  else
  {
    elDiv.className = elDiv.className.replace( /section-hidden/, "section" );
    elHeaderDiv.title = "Click to hide section content";
    folded = false;
  }

  // store state to helper
  wakaDoSections.prototype.static_headerStatesSet( elDiv.id, folded );

}

// static header storage --------------------------------------------------------------
wakaDoSections.prototype.static_headerStates = [];
wakaDoSections.prototype.static_headerStatesGet = function( headerNo ) 
{
  if (wakaDoSections.prototype.static_headerStates[headerNo] === wakaFormatter.prototype.undef())
    wakaDoSections.prototype.static_headerStates[headerNo] = false;
  return wakaDoSections.prototype.static_headerStates[headerNo];
};
wakaDoSections.prototype.static_headerStatesSet = function( headerNo, state ) 
{
  wakaDoSections.prototype.static_headerStates[headerNo] = state;
}
wakaDoSections.prototype.static_headerCount = 0;
wakaDoSections.prototype.static_nextId = function() 
{
  wakaDoSections.prototype.static_headerCount++;
  return "wakaDoSectionsHeader_" + wakaDoSections.prototype.static_headerCount;
}
wakaDoSections.prototype.static_nextIdReset = function() 
{
  wakaDoSections.prototype.static_headerCount = 0;
}
// static header storage --------------------------------------------------------------


// private (compilation)
wakaDoSections.prototype._compile   = function()
{
  var inner = this.wf.format( this._data.inner, "default" );
  var glued = this.wf.formatTree( this._tree, "default" );

  var _css1 = this.wf.cssPrefix + "section" + this.wf.cssPostfix;
  var _css2 = this.wf.cssPrefix + "section-content" + this.wf.cssPostfix;
  var _css3 = this.wf.cssPrefix + "section-header" + this.wf.cssPostfix;

  var headerId = wakaDoSections.prototype.static_nextId();
  var state    = wakaDoSections.prototype.static_headerStatesGet( headerId );
  if (state) _css1 = this.wf.cssPrefix + "section-hidden" + this.wf.cssPostfix;

  this._html = '<div id="'+headerId+'" class="'+_css1+'">'+
               '<div class="'+_css3+'" title="Click to hide section content" '+
               ' onclick="wakaDoSections.prototype.static_onShowHide(this)" '+
               '>'+
               "<h"+this._data.level+">"+ inner +"</h"+this._data.level+">"+
               '</div>'+
               '<div class="'+_css2+'">'+
               glued;
}


// glues given token as child. returns true on success
wakaDoSections.prototype.__super__glueChild  = wakaDoSections.prototype.glueChild;
wakaDoSections.prototype.glueChild  = function( tokenChild )
{
  // if section header -- std glue
  if (tokenChild.isSectionHeader) return this.__super__glueChild(tokenChild);

  if (this._tree.length > 0)
  {
    tokenChild._prev = this._tree[ this._tree.length-1 ];
    while (tokenChild._prev && !tokenChild._prev.isSectionHeader)
    {
      tokenChild._prev = tokenChild._prev._prev;
    }
    if (tokenChild._prev && tokenChild._prev != this && tokenChild._prev.isSectionHeader) 
      return tokenChild._prev.glueChild( tokenChild );
  }

  // ok, glue to me.
  tokenChild._parent = this;
  tokenChild._prev   = false;
  this._tree[ this._tree.length ] = tokenChild;

  return true;
}


/* ============================================================================================================== */
/* ============================================================================================================== */
/* ============================================================================================================== */

function wakaDoSectionsContent() { }
         wakaDoSectionsContent.prototype = new wakaSectionsContent();
         wakaDoSectionsContent.prototype.constructor = 
         wakaDoSectionsContent;


// private (up to parent)
wakaDoSectionsContent.prototype._glue = function()
{
  // trim upcoming empty lines
  this._data = this._data.replace(/^\s*\n/, "");

  // parse inner content as nextPreset
  this._subTree = this.wf.buildTree( this._data, this._nextPreset );

  if (!this._prev) return false;

  // glue parsed tree into prev.
  for (var i=0; i<this._subTree.length; i++)
    this._prev.glueChild( this._subTree[i] );
  this._prev.glueChild( this._appendTokenAdd() );

  return true;
}

// private: flatten subtree to replace current node
wakaDoSectionsContent.prototype._flattenTree = function()
{
  var result = this._subTree;

  if (!this._prev || this._prev.isSectionHeader)
    result[ result.length ] = this._appendTokenAdd();

  return result;
}

wakaDoSectionsContent.prototype._appendTokenAdd = function()
{
  var tokenAdd = new wakaDoTodoAdd();
  tokenAdd.bind( this.wf );
  tokenAdd.build("");
  if (this._subTree.length)
    tokenAdd._prev = this._subTree[ this._subTree.length-1 ];
  else
    tokenAdd._prev = this;

  return tokenAdd;
}



