(function($){
$.fn.jqTOC = function(settings) {
function tocToggleDisplay(e){
$('#toc_content')[e.data.mode]();
}
settings = $.extend({
tocWidth: '220px',
tocTitle: 'Content',
tocStart: 1,
tocEnd : 3,
tocContainer : 'toc_container',
tocAutoClose : true,
tocShowOnClick : true,
tocTopLink : 'top'
}, settings || {});
// create the main content container if not already created
if (document.getElementById(settings.tocContainer) == null)
$('body').append('
');
$('#'+settings.tocContainer).css('width',settings.tocWidth).append(
(settings.tocTitle?'':'') +
''
);
var t = $('#toc_content');
var headerLevel,headerId;
// Find the highest level heading used within the range tocStart..tocEnd. Prevents indenting when no higher level exists.
var start=settings.tocEnd;
this.children().each(function(i) {
headerLevel = this.nodeName.substr(1);
if(
this.nodeName.match(/^H\d+$/)
&& headerLevel >= settings.tocStart
&& headerLevel <= settings.tocEnd
&& this.nodeName.substr(1) < start
) {
start = this.nodeName.substr(1);
}
if (start == settings.tocStart) {
return false;
}
});
settings.tocStart=start;
this.children().each(function(i) {
headerLevel = this.nodeName.substr(1);
if(
this.nodeName.match(/^H\d+$/)
&& headerLevel >= settings.tocStart
&& headerLevel <= settings.tocEnd
) {
headerId = this.id || 'jqTOC_link' + i;
t.append(''+ $(this).text() +''
);
this.id = headerId;
if (settings.tocTopLink) {
$(this).before('');
}
}
});
if (settings.tocShowOnClick) {
if (settings.tocTitle) {
$('#toc_header').bind('click', {mode: 'toggle'}, function(e){tocToggleDisplay(e);});
}
if (settings.tocAutoClose) {
$('#toc_content a').bind('click', {mode: 'hide'}, function(e){tocToggleDisplay(e);});
}
} else {
$('#toc_content').show();
}
if (settings.tocTopLink) {
$('.toc_top').bind('click', function(){window.scrollTo(0,0);});
}
return this;
}
})(jQuery);