/*------------------------------------------------------------------------------
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.
Modifications:  RLB, June 2007
                  - Added class="addendum" to class="note" (used in my own
                    styles).
                  - Added a thinspace before superscript (class="note").
                  - Appended section will only be made if there are links to
                    display.
                  - Excluded nodes with a "src" attribute (<img>s) because IE
                    uses the "src" like an "href"
------------------------------------------------------------------------------*/

if (navigator.vendor != "Apple Computer, Inc.") {                               // Safari will display the script output with screen styles; Safari's navigator.appName = "Netscape"

function footnoteLinks(containerID,targetID) {
  if (!document.getElementById ||
      !document.getElementsByTagName ||
      !document.createElement) return false;
  if (!document.getElementById(containerID) ||
      !document.getElementById(targetID)) return false;
  var container = document.getElementById(containerID);
  var target    = document.getElementById(targetID);
  var h3        = document.createElement('h3');
  addClass.apply(h3,['printOnly']);
  var h3_txt    = document.createTextNode('Links');
  h3.appendChild(h3_txt);
  var coll = container.getElementsByTagName('*');
  var ol   = document.createElement('ol');
  addClass.apply(ol,['printOnly']);
  var myArr = [];
  var thisLink;
  var num = 1;
  for (var i=0; i<coll.length; i++) {
    var thisClass = coll[i].className;
    if ( (coll[i].getAttribute('href') || coll[i].getAttribute('cite')) &&
         (thisClass == '' || thisClass.indexOf('ignore') == -1) &&
         (!coll[i].getAttribute('src')) ) {                                     // Exclude <img>s because IE counts its "src" attribute as an "href"
      thisLink = coll[i].getAttribute('href') ? coll[i].href : coll[i].cite;
      var note = document.createElement('sup');
      addClass.apply(note,['addendum']);
      addClass.apply(note,['printOnly']);
      var note_txt;
      var j = inArray.apply(myArr,[thisLink]);
      if ( j || j===0 ) {
        note_txt = document.createTextNode(j+1);
      } else {
        var li     = document.createElement('li');
        var li_txt = document.createTextNode(thisLink);
        li.appendChild(li_txt);
        ol.appendChild(li);
        myArr.push(thisLink);
        note_txt = document.createTextNode(num);
        num++;
      }
      var space = document.createTextNode("\u2009");                            // Thin space = U+2009
      note.appendChild(space);
      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);
      }
    }
  }
  if (num > 1) {                                                                // Append the "Links" section only if there are links to display
    target.appendChild(h3);
    target.appendChild(ol);
  }
  addClass.apply(document.getElementsByTagName('html')[0],['noted']);
  return true;
}

}
