//********************************************************************
// jquery.autotoc.js
// 
// Automatically populate a Table of Contents in $(this) with headings
// from specified element.  This is a jQuery plugin.
//
//   Author:    Colin J. Wynne <cwynne(at)avtokrator(dot)org>
//   Created:   2010-09-08
//   Version:   (see RCS string below)
//
//********************************************************************
// $Id: jquery.autotoc.js,v 1.3 2011/01/24 01:48:15 cwynne Exp $
//********************************************************************

(function($) {

  // Auto-generate the ToC.  Clone headers from $(content), convert
  // them to <li> elements, wrap in a <ul>, and append <ul> to
  // $(this).
$.fn.autotoc = function(content, options) {

      // Default private variables for this plugin.
    var defaults = $.extend ({

        tocLink:        "toclink",  // * @id for created anchors and
                                    //   links.
        tocClass:       "toc",      // * Base bane for @class of created
                                    //   ToC elements; appended with 
                                    //   '-Hn' for corresponding <Hn>
    }, options);                    //   element.

    if( ! this.length ) {
        alert("Context node is invalid.");
        return this;
    }

      // Check for valid arg.  Must be a jQuery selector representing
      // a unique element.
    if( !content ) {
        alert("No content node specified.");
        return this;
    }

    if( $(content).length != 1 ) {
        alert(content + " does not specify a unique element.");
        return this;
    }

      // Create the <UL> to hold the ToC elements.
    var tocList = document.createElement('ul');

      // Iterate through all headings in the content element.
    $( "h1,h2,h3,h4,h5,h6,h7,h8,h9", $(content) ).each( function(i) {

          // Determine the anchor name and the ToC class.
        var anchor    = defaults.tocLink  + "-" + i;
        var className = defaults.tocClass + "-" + this.nodeName;

          // Copy the header html, removing @id/@name (to avoid
          // breaking uniqueness).
        var hdr  = $(this).clone();

        $("*", hdr).removeAttr(  "id");
        $("*", hdr).removeAttr("name");

          // Set the anchor in the content.
        $(this).prepend( document.createElement('a') );
        $(this).children().first().attr("name", anchor);

          // Create destination elements.
        var item = document.createElement('li');
        var link = document.createElement( 'a');

            // Set the href and link content.
        $(   link).attr("href", "#"+anchor);
        $(   link).html( hdr.html() );
        $(   link).addClass( className );

              // CLean up link internals.
        $("a", link).remove();

            // Set the ToC class and content.
        $(   item).append(        link );
        $(tocList).append(        item );
  
    });

      // Add the ToC to the context node.
    return this.append( tocList );
};

})(jQuery);

//********************************************************************
// END
//********************************************************************

