/*
 * A jQuery plugin by Brian Holt that will search the selected blocks for 
 * specially-defined footnote elements.	 If found, these elements will be 
 * moved to a footnotes section and links to and from the footnotes will 
 * be created.
 *
 * See http://www.planetholt.com/articles/jQuery-Footnotes	
 * for full documentation.
 *
 * By default, footnotes will be found in SPANs with the footnote class,
 * and in BLOCKQUOTEs with a TITLE attribute.
 *
 * Thanks to CSSNewbies.com for the general idea, which I have enhanced 
 * and implemented with as a jQuery plugin.
 * 
 * Copyright 2008-2009 Brian Holt.	
 * Licensed under the LGPL license. See 
 * http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 * 
 * Version 1.2.1
 */
(function(c){c.fn.footnotes=function(d){var e=c.extend({},c.fn.footnotes.defaults,d);return this.each(function(f){b("INFO: Building footnotes for "+(f+1)+"...",e.debugMode);c(e.footnotes,this).addClass(e.autoFootnoteClass);var h=(""==e.contentBlock)?c(this):c(e.contentBlock,this),g=e.orderedList?"<ol/>":"<ul/>";c("."+e.autoFootnoteClass).each(function(n){var t=-1,p=f+"-"+n,l,r,s,u,q,k,o,j,m;if(e.singleFootnoteDestination){l=c("#"+e.destination);if(0==l.length){b("INFO: No #autoFootnotes found; adding our own",e.debugMode);l=c(g).attr("id",e.destination).addClass("footnotesList").appendTo(h)}}else{l=c("#"+e.destination+f);if(0==l.length){b("INFO: No #autoFootnotes"+f+" found; adding our own for "+(f+1),e.debugMode);l=c(g).attr("id",e.destination+f).addClass("footnotesList").appendTo(h)}}c(this).removeClass(e.autoFootnoteClass);r=e.fnExtractFootnote(this);t=-1;p=f+"-"+n;c("li > .footnoteContent",l).each(function(i){if(c(this).html()==r){t=i;s=c(c(this).parents("li").get(0));return false}});if(-1==t){u=c("<a/>").attr("href","#cite-text-"+p).attr("name","cite-ref-"+p).attr("dir","ltr").text("["+(n+1)+"]").addClass("footnoteLink");if(c(this).is(e.prependTags)){c("<sup/>").prependTo(this).append(u)}else{c("<sup/>").appendTo(this).append(u)}q=c("<li/>").attr("id","cite-text-"+p);k=c("<span/>").addClass("footnoteBackReferenceGroup").appendTo(q);c("<span/>").addClass("footnoteContent").html(r).appendTo(q);u=c("<a/>").text("^").attr("href","#cite-ref-"+p).addClass("footnoteBackref").prependTo(k);l.append(q)}else{p=f+"-"+t;o=c(c("li > .footnoteBackReferenceGroup",l).get(t));j=o.find(".footnoteBackref");m=j.length;switch(j.length){case 0:b("ERROR: backRefs.length == 0, which should have prevented this code path",e.debugMode);break;case 1:c("<sup/>").text("^ ").addClass("footnoteBackref").prependTo(o);c(j).text("a");++m;default:u=c("<a/>").attr("href","#"+s.attr("id")).attr("name","cite-ref-"+p+"-"+j.length).text("["+(t+1)+"]").addClass("footnoteLink");if(c(this).is(e.prependTags)){c("<sup/>").prependTo(this).append(u)}else{c("<sup/>").appendTo(this).append(u)}u=c("<a/>").attr("href","#cite-ref-"+p+"-"+j.length).addClass("footnoteBackref");if(m>=26){b("WARN: multiple letter functionality is probably broken when more than 26 footnotes exist",e.debugMode)}u.prepend(String.fromCharCode((m)+96));c("<sup/>").appendTo(o).append(u);break}}});b("INFO: Done building footnotes for "+(f+1),e.debugMode)})};c.fn.footnotes.version=function(){return"1.2.1"};c.fn.footnotes.defaults={footnotes:"blockquote[title],span.footnote,blockquote[cite]",prependTags:"blockquote",singleFootnoteDestination:false,destination:"autoFootnotes",contentBlock:".content",autoFootnoteClass:"autoFootnote",fnExtractFootnote:a,orderedList:true,debugMode:true};function b(e,d){if(d){if(window.console&&window.console.log){window.console.log(e)}}}function a(i){var j=c(i),e,f,h,g,d;if(j.is("span.footnote")){e=c(i).html();f=/^(?:(?:&nbsp;)|\s)*\(([\S\s]+)\)(?:(?:&nbsp;)|\s)*$/;h=e.match(f);if(h&&2==h.length){e=h[1]}j.empty()}else{if(j.is("blockquote[title]")){g=j.attr("cite");e=j.attr("title");if(""!=g){d=c("<a/>").attr("href",g);if(c(e).length==0){e=d.text(e)}else{e=d.text(g).wrap("<span/>").parent().append(": "+e);j.attr("title","")}}}else{if(j.is("blockquote[cite]")){g=j.attr("cite");e=c("<a/>").attr("href",g).text(g)}}}return e}})(jQuery);
