// Table Of Content
function TableOfContent (container_node_id, content_node_id) { // {{{

   // TOC container and content divs {{{
   this.container_id = container_node_id;
   this.container = (function (id) {
         var cont = document.getElementById(id);
         return document.getElementById(id);
   }) (this.container_id)

   // content div
   this.content_id = content_node_id;
   this.content = (function (id) {
         var cont = document.getElementById(id);
         return document.getElementById(id);
   }) (this.content_id)
   // }}}

   // link to the table Of Content
   this.linkTOC = (function (href, title, content, className) { // {{{
      var a = document.createElement('a');
      a.appendChild(document.createTextNode(content));
      a.title = title;
      a.href = href
      a.className = className;
      return a
   } ) ('#'+this.container_id, 'Table Of Content', 'Back to the TOC', 'linkTOC');
   // }}}
   
   // the TOC <ul> 
   this.ul = (function (container, content, aTOC) { // {{{
      // check if we really need to go further
      if (!container)
         return ul;

      // number the different items, and make a list of link to those items, and then return the resulting <ul>
      function makeUL (parentNode, preNb) { // {{{
         var selector = 'ul[contains(concat(" ", @class, " "), " hierarchy ")]/li/span[contains(concat(" ", @class, " "), " title ")]';
         var nodesSnap = document.evaluate(selector, parentNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

         for (var i=0, preNb_tmp, parentId, node, ul, ul_sub, li, a, aTOC_tmp, txt ; node = nodesSnap.snapshotItem(i) ; i++) {
            if (typeof(ul) == 'undefined' )
               ul = document.createElement('ul');

            li = document.createElement('li');
            a = document.createElement('a');

            // getting info from the parent node
            parentId = node.parentNode.id;
            preNb_tmp = preNb + (i+1);

            // Nice title to the link
            a.title = node.textContent.replace(/[^A-Z0-9._ -]/gi, ' ');
            a.title = a.title.replace(/\s+/g, ' ');

            // adding the item number to the title
            node.textContent = preNb_tmp + ' - ' + node.textContent;

            // the link to the TOC
            node.parentNode.insertBefore(aTOC.cloneNode(true), node);

            // preparing the link
            a.appendChild(document.createTextNode(node.textContent));
            a.href = '#'+parentId;

            li.appendChild(a);
            
            // Do the same on subsections recursively (node.parentNode should point on a <li> element)
            ul_sub = makeUL(node.parentNode, preNb_tmp + '.'); 

            // then just add it to this item (<li>)
            if (ul_sub)
               li.appendChild(ul_sub);

            // and add it to the list (<ul>);
            ul.appendChild(li);
         }

         // return the resulting list (<ul>)
         return ul;
      }
      // }}}

      var ul = makeUL(content, '');

      // Finally, we replace what's in the container with the <ul>
      for (var child ; child = container.lastChild ;)
         container.removeChild(child);
      if (ul)
         container.appendChild(ul);

      // adding the apppropriate class to be used later with CSS
      container.className += ' tableOfContent';

      return ul;

   }) (this.container, this.content, this.linkTOC)
   // }}}

}
// }}}

window.addEventListener('load', myinitcommon, false);

function myinitcommon () {
   // clear  "span[contains(@class,'clearMe')]" fields {{{
   var selector = "//span[contains(@class,'clearMe')]";
   var spanSnap = document.evaluate(selector, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
   for (var i=0, node ; node = spanSnap.snapshotItem(i) ; i++)
      node.textContent = node.textContent.replace(/__/g, ' ').replace(/_/g, '');
   // }}}

   // make the Table Of Content (if div#tableOfContent and div#content exist)
   var mainTOC = new TableOfContent('tableOfContent', 'content');
}

/* vim: set et sts=3 sw=3 foldmethod=marker : */



