/*------------------------------------------------------------------------------
Original info about the script:
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.
-----------------------------------------------------------------------------
Added/changed by Lars Gunther:
+ This script has been made XHTML-secure (createElementNS) See jsUtilities.js
+ If there are no links no header or ul will be added
+ Added heading-argument to provide basic i18n or other customization of the heading
+ Uses John Resig's addEvent instead of Scott Andrews'
+ MAJOR BIG FIX: MSIE harvests the src-attribute for images as well -> STOPPED
------------------------------------------------------------------------------*/
function footnoteLinks(containerID,targetID,heading) {
  if (!document.getElementById ||
      !document.getElementsByTagName ||
      !createElement) return false;
  if (!document.getElementById(containerID) ||
      !document.getElementById(targetID)) return false;
  var container = document.getElementById(containerID);
  var target    = document.getElementById(targetID);
  var h2        = createElement('h2');
  addClass.apply(h2,['printOnly']); // MSIE fixar inte h2.addClass('printOnly');
  var h2_txt    = document.createTextNode(heading);
  h2.appendChild(h2_txt);
  var coll = container.getElementsByTagName('*');
  var ol   = createElement('ol');
  addClass.apply(ol,['printOnly']);
  var myArr = [];
  var thisLink;
  var num = 1;
  for (var i=0; i<coll.length; i++) {
    if (coll[i].nodeName == 'IMG') {
        continue; // MSIE fix
    }
    var thisClass = coll[i].className;
    if ( (coll[i].getAttribute('href') ||
          coll[i].getAttribute('cite')) &&
          (thisClass == '' ||
           thisClass.indexOf('ignore') == -1)) {
        thisLink = coll[i].getAttribute('href') ? coll[i].href : coll[i].cite;
        var note = createElement('sup');
        addClass.apply(note,['printOnly']);
        var note_txt;
        // var j = inArray.apply(myArr,[thisLink]); // FUNGERAR INTE!
        var j = myArr.inArray(thisLink);
        if ( j || j===0 ) {
            note_txt = document.createTextNode(j+1);
        } else {
            var li     = createElement('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);
        }
    }
  }
  if ( ol.childNodes.length ) { // Added by Lars Gunther
    target.appendChild(h2);
    target.appendChild(ol);
  } else {
    h2 = null;
    ol = null;
  }
  addClass.apply(document.getElementsByTagName('html')[0],['noted']);
  return true;
}
// Site-specific init
function initFootnote()
{
    if ( !document.getElementById ||
         !createElement ) return false;
    var target = createElement('div');
    target.id = 'fotnoter';
    document.getElementById('artikel').appendChild(target);
    footnoteLinks('artikel','fotnoter','Länkar:');
    return true;
}

addLoadListener(initFootnote);

