/*
 * Created By Scott Steil of Imperium Inc.
 *
 * Add footnote definitions to a list of tagged terms. This script will search 
 * for <span> tags that have the class 'footnote'. Then add the title attribute
 * to a list of footnotes. An anchor will be added that links the the specific
 * footnote.
 * 
 * Add HTML to the title tag to converting the < and > into &lt; and &gt; 
 * special characters. These will be converted when added to the internal HTML
 * of the footnote item. 
 */
 
window.addEvent('domready', function() {
	if($defined($('footnotes')))
	{
		//clear current data in case this was run twice
		$('footnotes').empty();
		
		// find the list of footnote terms
		var terms = $$('span.footnote');
		if(terms.length > 0) {
			var title = new Element('h3', {'html': 'Footnotes'});
			var list = new Element('ol', {'class': 'footnotes'});
			
			terms.each( function(term, i) {
				//create the footnote for the list
				var footnote = new Element('li', {'html': term.get('title')});
				// add it to the list with an anchor
				list.adopt(footnote.grab(new Element('a', {'name': 'footnotes_'+(i+1)}), 'top'));
				// then add a link to the footnote anchor
				term.adopt(new Element('a', {'html': '['+(i+1)+']', 'href': '#footnotes_'+(i+1)}));
			});
			
			$('footnotes').adopt(title, list);
			$('footnotes').show();
		}
		else
		{
			$('footnotes').hide();
		}
	}
	
});
