/*---------------------------------------------------------------------------------------------------
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.
Parameters:	containerID - div block whose links will be footnoted.
			targetID - div block which houses the footnote links list tags.

Updated:  		2005-2007 - Brian LePore
	(Compressed code available at: http://www.cps.brockport.edu/~cpsclub/template/footnoteLinks.js)
	1. Eliminated the use of inArray and push from jsUtilities using Brian Hardy's code.
	2. Replaced the addClass from jsUtilities with my own version that doesn't extend object.
	3. Walks the DOM.
	4. Only adds the h2 ("Links" label) and ol (link item) objects if links are found in containerID.
	5. Strips out "mailto:" and optional spaces from links.
	6. Ignores:
		a. if href is javascript code.
		b. if href's value is simply '#'
		c. if href is equivalent to anchor's inner text.
		d. if parent's class was ignore.

Updated:  	July 15, 2009 - Brian D. Elley (ExxonMobil IT)
	Restored footnotes where href="#" as I have many of these on the Chemicals sample pages.
	Restored footnotes on image links as this eliminated many valid links.
	
Updated:	November 11, 2009 - Matt Miller (Crown Partners)
	Created a new function to find all the links within the parent element which fixed the infinate loop issue 
		in IE6.
	Also removed unneeded functions.
---------------------------------------------------------------------------------------------------*/

function trim(c) {
	if (c)
		c = c.replace(/^\s*|\s*$/,'');
	return c;
}

function hasClass(obj, c) {
	if (!obj || !c) return false;
	c = trim(c);
	var oldC = trim(obj.className);
	if (c && oldC && oldC.match('\\b'+c+'\\b'))
		return true;
	else
		return false;
}

function addClass(obj, c) { 
	if (!obj || !c || hasClass(obj, c)) return;
	c = trim(c);
	var oldC = trim(obj.className);
	if (!c) return;
	obj.className = (oldC) ? oldC + ' ' + c :  c;
	return;
}

function lastChildContainingText(obj) { 
	var testChild = obj.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(testChild); }
	return testChild;
}

function findLinks(parent,list)
{
	var links = parent.getElementsByTagName('a');
	var linkArray = new Array();
	var num = 0;
	
	for(var i=0;i<links.length;i++)
	{
		var link = links[i].href;
		
		if (link != window.location + '#' && !link.match('^javascript:'))
		{
			var note = document.createElement('sup');
			note.className = 'printOnly';
			var note_txt;
			if(linkArray[link])
			{
				note_txt = document.createTextNode(linkArray[link]);
			}else
			{
				var li = document.createElement('li');
				var li_txt = document.createTextNode(link);
				li.appendChild(li_txt);
				list.appendChild(li);
				num++;
				linkArray[link] = num;
				note_txt = document.createTextNode(num);
				
			}
			note.appendChild(note_txt);
			links[i].parentNode.insertBefore(note, links[i].nextSibling);
		}
	}
}

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 h2        = document.createElement('h2');

  h2.className = 'printOnly';

  var h2_txt = document.createTextNode('Links');

  h2.appendChild(h2_txt);

  var ol = document.createElement('ol');

  ol.className = 'printOnly';
 
  findLinks(container,ol);
  
  if (ol.getElementsByTagName('li').length) {
    target.appendChild(h2);
    target.appendChild(ol);
  }
  addClass(document.getElementsByTagName('html')[0], 'noted');
  return true;
}

var myArr = new Array();
