;(function($){$.fn.footnotes=function(options){var opts=$.extend({},$.fn.footnotes.defaults,options);return this.each(function(j){logToConsole("INFO: Building footnotes for "+(j+1)+"...",opts.debugMode);$(opts.footnotes,this).addClass(opts.autoFootnoteClass);var $contentPlaceholder=(""===opts.contentBlock)?$(this):$(opts.contentBlock,this),newListElement=opts.orderedList?"<ol/>":"<ul/>";$("."+opts.autoFootnoteClass).each(function(i){var foundFootnoteIdx=-1,refID=j+"-"+i,$this=$(this),$footnoteDestination,footnoteHTML,$foundFootnoteLI,$anchor,$li,$backRefSpan,$backRefs,letterCounter;if(opts.singleFootnoteDestination){$footnoteDestination=$("#"+opts.destination);if(0===$footnoteDestination.length){logToConsole("INFO: No #autoFootnotes found; adding our own",opts.debugMode);$footnoteDestination=$(newListElement).attr("id",opts.destination).addClass("footnotesList").appendTo($contentPlaceholder);}}else{$footnoteDestination=$("#"+opts.destination+j);if(0===$footnoteDestination.length){logToConsole("INFO: No #autoFootnotes"+j+" found; adding our own for "+(j+1),opts.debugMode);$footnoteDestination=$(newListElement).attr("id",opts.destination+j).addClass("footnotesList").appendTo($contentPlaceholder);}}
$this.removeClass(opts.autoFootnoteClass);footnoteHTML=opts.fnExtractFootnote(this);foundFootnoteIdx=-1;refID=j+"-"+i;$footnoteDestination.find("li > .footnoteContent").each(function(k){var $this=$(this);if($this.html()===footnoteHTML){foundFootnoteIdx=k;$foundFootnoteLI=$($this.parents("li").get(0));return false;}});if(-1===foundFootnoteIdx){$anchor=$("<a/>").attr("href","#cite-text-"+refID).attr("name","cite-ref-"+refID).attr("id","cite-ref-"+refID).attr("dir","ltr").attr("title",footnoteHTML).text("["+($footnoteDestination.find("li").length+1)+"]").addClass("footnoteLink");if($this.is(opts.prependTags)){$("<sup/>").prependTo(this).append($anchor);}else{$("<sup/>").appendTo(this).append($anchor);}
$li=$("<li/>").attr("id","cite-text-"+refID);$backRefSpan=$("<span/>").addClass("footnoteBackReferenceGroup").appendTo($li);$("<span/>").addClass("footnoteContent").html(footnoteHTML).appendTo($li);$anchor=$("<a/>").text("^").attr("href","#cite-ref-"+refID).addClass("footnoteBackref").prependTo($backRefSpan);$footnoteDestination.append($li);}else{refID=j+"-"+foundFootnoteIdx;$backRefSpan=$($("li > .footnoteBackReferenceGroup",$footnoteDestination).get(foundFootnoteIdx));$backRefs=$backRefSpan.find(".footnoteBackref");letterCounter=$backRefs.length;if(0===$backRefs.length){logToConsole("ERROR: $backRefs.length == 0, which should have prevented this code path",opts.debugMode);}else{if(1===$backRefs.length){$("<sup/>").text("^ ").addClass("footnoteBackref").prependTo($backRefSpan);$backRefs.html("<sup>a</sup>");++letterCounter;}
$anchor=$("<a/>").attr("href","#"+$foundFootnoteLI.attr("id")).attr("name","cite-ref-"+refID+"-"+$backRefs.length).attr("id","cite-ref-"+refID+"-"+$backRefs.length).attr("title",footnoteHTML).text("["+(foundFootnoteIdx+1)+"]").addClass("footnoteLink");if($this.is(opts.prependTags)){$("<sup/>").prependTo(this).append($anchor);}else{$("<sup/>").appendTo(this).append($anchor);}
$anchor=$("<a/>").attr("href","#cite-ref-"+refID+"-"+$backRefs.length).addClass("footnoteBackref");if(letterCounter>=26){logToConsole("WARN: multiple letter functionality is probably broken when more than 26 footnotes exist",opts.debugMode);}
$anchor.prepend(String.fromCharCode((letterCounter)+96));$("<sup/>").appendTo($backRefSpan).append($anchor);}}});logToConsole("INFO: Done building footnotes for "+(j+1),opts.debugMode);});};$.fn.footnotes.version=function(){return"1.2.2";};$.fn.footnotes.defaults={footnotes:"blockquote[title],span.footnote,blockquote[cite]",prependTags:"blockquote",singleFootnoteDestination:false,destination:"autoFootnotes",contentBlock:".content",autoFootnoteClass:"autoFootnote",fnExtractFootnote:fnDefaultExtractHTML,orderedList:true,debugMode:true};function logToConsole(str,debugMode){if(debugMode){if(window.console&&window.console.log){window.console.log(str);}}}
function fnDefaultExtractHTML(obj){var $obj=$(obj),footnoteHTML,regexRemoveParens,matches,cite,a;if($obj.is("span.footnote")){footnoteHTML=$(obj).html();regexRemoveParens=/^(?:(?:&nbsp;)|\s)*\(([\S\s]+)\)(?:(?:&nbsp;)|\s)*$/;matches=footnoteHTML.match(regexRemoveParens);if(matches&&2===matches.length){footnoteHTML=matches[1];}
$obj.empty();}
else if($obj.is("blockquote[title]")){cite=$obj.attr("cite");footnoteHTML=$obj.attr("title");if(""!==cite){a=$("<a/>").attr("href",cite);if(0===$(footnoteHTML).length){footnoteHTML=a.text(footnoteHTML);}
else{footnoteHTML=a.text(cite).wrap("<span/>").parent().append(": "+footnoteHTML);$obj.attr("title","");}}}
else if($obj.is("blockquote[cite]")){cite=$obj.attr("cite");footnoteHTML=$("<a/>").attr("href",cite).text(cite);}
return footnoteHTML;}})(jQuery);
