var FootnoteLinks = {
	elmClass: 'linklist footnotes',
	chkClass: 'fn',
	bodyClass: 'footnoted',
	target: 'sidebar',
	lnks: new Array(),
	init: function(){
		var ot = document.getElementById(this.target);
		var ol = Builder.node('ol');
		var col = document.getElementsByTagName("*");
		var thisLink, thisClass;
		var num = 1;
		for ( var i=0; i<col.length; ++i ) {
			if ( Element.hasClassName(col[i], this.chkClass) && (col[i].getAttribute('href') || col[i].getAttribute('cite')) ) {
				thisLink = col[i].getAttribute('href') ? col[i].href : col[i].cite;
				var txt = "";
				var j = this.inArray(this.lnks, thisLink);
				if ( j || j === 0 ) {
					txt = (j+1);
				} else {
					var li = Builder.node('li', thisLink);
					ol.appendChild(li);
					this.lnks.push(thisLink);
					txt = num;
					++num;
				}
				var note = Builder.node('sup', {className:this.elmClass}, txt);
				if ( col[i].tagName.toLowerCase() == 'blockquote') {
					var lastChild = this.lastChildContainingText.apply(coll[i]);
					lastChild.appendChild(note);
				} else {
					col[i].parentNode.insertBefore(note, col[i].nextSibling);
				}
			}
		}
		if ( this.lnks.length > 0 ) {
			var container = Builder.node('div', {className:this.elmClass});
			var header = Builder.node('h3', 'Links');
			container.appendChild(header);
			container.appendChild(ol);
			ot.appendChild(container);
			Element.addClassName(document.getElementsByTagName('html')[0], this.bodyClass);
		}
	},
	inArray: function(a,n){
		for(var i=0;i<a.length;++i)
			if(a[i]===n)
				return i;
		return false;
	},
	lastChildContainingText: function(lastChild) {
		var testChild = lastChild;
		var contentCntnr = ['p','li','dd'];
		while (testChild.nodeType != 1) {
		  testChild = testChild.previousSibling;
		}
		var tag = testChild.tagName.toLowerCase();
		var tagInArr = inArray(contentCntnr, tag);
		if (!tagInArr && tagInArr!==0) {
		  testChild = lastChildContainingText(testChild);
		}
		return testChild;
	}
};
var doFootnotes = function(){
	FootnoteLinks.init();
};
