How to extend this function?

How to extend this function?

Hello,

i am using the following jQuery Function for generating a Table of Contents on my page. But now i want to extent it, but dont know how. Perhaps someone can help me here?

First the Function:

<script type="text/javascript">
   (function($) {
  $.toc = function(tocList) {

    $(tocList).addClass('jquery-toc');
    var tocListArray = tocList.split(',');
    $.each(tocListArray, function(i,v) { tocListArray[i] = $.trim(v); });

    var $elements = $('.jquery-toc');

    $('body').append('<div></div>');
    var $toc = $('body div:last');

    var lastLevel = 1;

    $toc.append('<ul class="jquery-toc-1"></ul>');

    $elements.each(function() {
      var $e = $(this);
      var text = $e.text();
      var anchor = text.replace(/ /g,'-');

      $e.before('<a name="' + anchor + '"></a>');

      var level;

      $.each(tocListArray, function(i,v) {
        if (v.match(' ')) {
          var vArray = v.split(' ');
          var e = vArray[vArray.length - 1];
        } else { e = v; }
        if ($e.is(e)) { level = i+1; }
      });

      var className = 'jquery-toc-' + level;

      var li = '<li><a href="#' + anchor + '">' + text + '</a></li>';

      if (level == lastLevel) {
        $('ul.' + className + ':last',$toc).append(li);
      } else if (level > lastLevel) {
        var parentLevel = level - 1;
        var parentClassName = 'jquery-toc-' + parentLevel;
        $('ul.' + parentClassName + ':last',$toc).
          append('<ul class="' + className + '"></ul>');
        $('ul.' + className + ':last',$toc).append(li);
      } else if (level < lastLevel) {
        $('ul.' + className + ':last',$toc).append(li);
      }

      lastLevel = level;

    });

    var $toc_ul = $('ul.jquery-toc-1',$toc);
    $toc.remove();
    return($toc_ul);

}

})(jQuery);

     $(document).ready(function(){
      $.toc('#pagecontent_b h1,#pagecontent_b h2,#pagecontent_b h3').prependTo('#pagecontent_b');
     });
   </script>


What i want to do are two things:

1. give each level a number like 1., 2., 2.1, 2.1.1 etc.
2. add this number to the Text inside the Header Tag, too (<h1>1. my header</h1>)

Example:

1. Head 1
2. Head 2

…
…

1. Head 1
djaskdjasödjaskdj aslökjd asd
dkajsdjaslödkjasdkljasldkjasd

2. Head 2
sldkasäfasdfkasdjflöasdf
asdfasdkfjasdfnasdm,fn sdjaf asöd

Can someone help?

Thank you
bc