/*------------------------------------------------------------------------------
Function:       footnoteLinks()
Author:         Aaron Gustafson (aaron at easy-designs dot net)
Creation Date:  8 May 2005
Version:        1.3
Homepage:       http://www.easy-designs.net/code/footnoteLinks/
License:        Creative Commons Attribution-ShareAlike 2.0 License
                http://creativecommons.org/licenses/by-sa/2.0/
Note:           If you change or improve on this script, please let us know by 
                emailing the author (above) with a link to your demo page.
------------------------------------------------------------------------------*/
/*
NOTES:

Heavily modified to make use of Prototype.js throughout.
	document.createElement => new Element
	document.elementsByTagName => $$ OR el.select
	addClass.apply(el) => .el.addClassName
	used hasClassName for checking if links are ignored
Also added the ability to ignore local links automatically.
Also modified to not print anything when the links list is empty.
Finally, imported the inArray and lastChildContainingText functions.

- Phil DeJarnett

*/
function footnoteLinks(containerID, targetID, local) {
  var container = $(containerID);
  var target    = $(targetID);
  if(!container || !target) {
	  return false;
  }
  var h2        = new Element('h2');
  h2.addClassName('printOnly');
  var h2_txt    = document.createTextNode('Links');
  h2.appendChild(h2_txt);
  var coll = container.select('*');
  var ol   = new Element('ol');
  ol.addClassName('printOnly');
  var myArr = [];
  var thisLink;
  var num = 1;
  // don't add local links
  var localURL;
  if(!local) {
	  localURL = '//'+document.location.hostname;
  }
  for (var i=0; i<coll.length; i++) {
    var thisClass = coll[i].className;
    if ( (coll[i].getAttribute('href') ||
          coll[i].getAttribute('cite')) &&
          !Element.hasClassName(coll[i], 'ignore')) { 
      thisLink = coll[i].getAttribute('href') ? coll[i].href : coll[i].cite;
	  // check to see if the link is local
	  isLocal = false;
	  if(!local) {
		  isLocal = thisLink.indexOf(localURL) > -1;
	  }
	  if(!isLocal) {
		  var note = new Element('sup');
		  note.addClassName('printOnly');
		  var note_txt;
		  var j = inArray.apply(myArr,[thisLink]);
		  if ( j || j===0 ) {
			note_txt = document.createTextNode(j+1);
		  } else {
			var li     = new Element('li');
			var li_txt = document.createTextNode(thisLink);
			li.appendChild(li_txt);
			ol.appendChild(li);
			myArr.push(thisLink);
			note_txt = document.createTextNode(num);
			num++;
		  }
		  note.appendChild(note_txt);
		  if (coll[i].tagName.toLowerCase() == 'blockquote') {
			var lastChild = lastChildContainingText.apply(coll[i]);
			lastChild.appendChild(note);
		  } else {
			coll[i].parentNode.insertBefore(note, coll[i].nextSibling);
		  }
	  }
    }
  }
  // only append if links were found
  if(num > 1) {
 	target.appendChild(h2);
  	target.appendChild(ol);
  }
  $$('html')[0].addClassName('noted');
  return true;
}
function inArray(needle) {
  for (var i=0; i < this.length; i++) {
    if (this[i] === needle) {
      return i;
    }
  }
  return false;
}
function lastChildContainingText() {
  var testChild = this.lastChild;
  var contentCntnr = ['p','li','dd'];
  while (testChild.nodeType != 1) {
    testChild = testChild.previousSibling;
  } 
  var tag = testChild.tagName.toLowerCase();
  var tagInArr = inArray.apply(contentCntnr, [tag]);
  if (!tagInArr && tagInArr!==0) {
    testChild = lastChildContainingText.apply(testChild);
  }
  return testChild;
}