/*
 *  the dictionaryURL should point to an xml file consisting of the form:
 *  <dictionary>
 *   <word term="term"><definition>definition</definition></word>
 *   etc...
 *  </dictionary>
 */
var dictionaryURL = "/global/xml/dictionary.xml";

/*
 *  this is the class name that will be given to all of the spans created by this function
 */
var style_name = "acronym";

/*
 *  this is the jquery selector for the element to run the function on
 */
var tooltip_selector = "#secondaryContentMainContainer";

//
// DO NOT EDIT BELOW THIS POINT
//

$(document).ready(function($){
	//this jQuery function will have to parse both the terms and the definitions
	$.get(dictionaryURL, function(data){
		//the dictionary will hold all of the words in the form
		// {term, definition, regular expression (used later to reduce computational complexity)}
		var dictionary = new Array();

		//parse the dictionaryURL xml and insert all of the words found there into the dictionary array
		$(data).find("word").each(function(){
			dictionary.push(new Array(
				$(this).attr("term"),
				$(this).find('definition').text(),
				new RegExp("\\b"+$(this).attr("term")+"\\b", "g")
			))
		});

		//get all of the applicable text nodes for parsing and parse them
		var alltextnodes = $(tooltip_selector + " *").contents().filter(function(){return this.nodeType === 3 && jQuery.trim(this.nodeValue) != "";}).each(function() {
			//define what we want the new text to look like (inclucing the "abbr" tags)
			var newtext = this.nodeValue;
			for (var i in dictionary) {
				newtext = newtext.replace(dictionary[i][2],
					"<abbr title=\"" + dictionary[i][1] + "\" class=\"" + style_name + "\">" + dictionary[i][0] + "</abbr>");
			}
			//replace the existing text with the new text
			var spanNode = document.createElement('span');
			spanNode.innerHTML = newtext;
			this.parentNode.replaceChild(spanNode, this);
		});
	});
});