/*   Copyright 2006, Michael J. Hill.  Used with permission. www.javascript-demos.com */
/*   Free use of the code, so long as BOTH copyright notices are kept intact */

	var GlossaryResponse = "";
	var wordList = [];
	var definitionList = [];
	var midWindow = 0;	
	var tipContainer = "";
	var IE = true;
	if (navigator.appName != 'Microsoft Internet Explorer'){IE = false}
	
	function stayHome(m){	

		var currX = 0;
		var currY = 0;
		var iL = 0;
		var iV = 0;

		if (IE)
			{
			 currX = event.clientX;
			 currY = event.clientY;
			}
		else	{
			 currX = m.pageX;
			 currY = m.pageY;
			}
		if (document.documentElement && document.documentElement.scrollLeft || document.documentElement && document.documentElement.scrollTop)
			{
			 iL = document.documentElement.scrollLeft;	
			 iV = document.documentElement.scrollTop;
			}
		else 	{
			 iL = document.body.scrollLeft;	
			 iV = document.body.scrollTop;	
			}
		if (currX > midWindow-30)
			{
			 var msgWidth = tipContainer.clientWidth;
			 if (IE){tipContainer.style.left = (currX-msgWidth-10+iL)+'px'}
			 else {tipContainer.style.left = (currX-msgWidth-10)+'px'}
			}
		else	{
			 if (IE){tipContainer.style.left = (currX+25+iL)+'px'}
			 else {tipContainer.style.left = (currX+25)+'px'}
			}
		if (IE) {tipContainer.style.top = (currY+iV)+'px'}
		else {tipContainer.style.top = currY+'px'}
	}

	function hideDefinition(){

		tipContainer.style.display = 'none';
		while (tipContainer.lastChild)
			{tipContainer.removeChild(tipContainer.lastChild)}
	}

	function getDefinition(currWord){

		var nDefinition = "";
		for (i=0; i<wordList.length; i++)
			{
			 if (wordList[i] == currWord)
				{
				 nDefinition = definitionList[i];
				}
			}
		return nDefinition; 
	}

	function showDefinition(defineWord){

		var toolTip = getDefinition(defineWord);	
		var tipTxt = toolTip.split("|");
		tipContainer.style.display = '';
		for (i=0; i<tipTxt.length; i++)
			{
			 tipContainer.appendChild(document.createTextNode(tipTxt[i]))
			 tipContainer.appendChild(document.createElement('br'))
			}
	}

	function getMidWindow(){

		if (document.documentElement && document.documentElement.scrollLeft || document.documentElement && document.documentElement.scrollTop)
			{
			 midWindow = document.documentElement.clientWidth/2;
			}
		else 	{
			 midWindow = document.body.clientWidth/2;
			}
	}

	function initContext(){

		var rawText = document.getElementById('subjectMatter');
		var workText = rawText.innerHTML;  
		workText = workText.replace(/(\<p\>)/gi,"$1 ").replace(/(\<\/p\>)/gi," $1");
		for (i=0; i<wordList.length; i++)
			{
			 var currWord = new RegExp("([\\s\\r\\n]+"+wordList[i]+"[\\s,;.:?!]+)",'gi')
			 workText = workText.replace(currWord,"<span class='word'>$1<\/span>");
			}
		rawText.innerHTML = workText;
	}

	function initTip(){

		tipContainer = document.getElementById('nFloat')
		tipContainer.style.display = 'none';
		if (!IE){document.captureEvents(Event.mousemove)}
		document.onmousemove=stayHome;
		getMidWindow();
	}

	function init(){

		initContext();
		var nBody = document.getElementsByTagName('body')[0];
		var tipBox = document.createElement('div');
		tipBox.className = "definition";
		tipBox.id = "nFloat";
		nBody.appendChild(tipBox);
		initTip();	
		var nWords = document.getElementById('subjectMatter').getElementsByTagName('span');
		for (i=0; i<nWords.length; i++)
			{
			 nWords[i].onmouseover = function()
				{
				 showDefinition(this.firstChild.data.toLowerCase().replace(/^\s+|\s+$/,"").replace(/[^a-zA-Z-\s]/g,"").replace(/^\s+|\s+$/,""));
				}
			 nWords[i].onmouseout = function()
				{
				 hideDefinition();
				}
			}
	}

	function parseGlossary(){

		var nList = GlossaryResponse.getElementsByTagName('word');
		var nDef = GlossaryResponse.getElementsByTagName('definition');
		for (i=0; i<nList.length; i++)
			{
			 wordList[i] = nList[i].firstChild.data;
			 definitionList[i] = nDef[i].firstChild.data;
			}
		init();
	}

	function createGlossary(){

		var GlossaryRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
		GlossaryRequest.onreadystatechange = function()
			{
		 	 if (GlossaryRequest.readyState == 4)
				{
		 	 	 if (GlossaryRequest.status == 200)
					{
			 	 	 GlossaryResponse = GlossaryRequest.responseXML;
			 	 	 parseGlossary();
					}
		 	 	 else 	{
				 	 alert('Error Glossary.xml File '+ GlossaryRequest.statusText);
					}
				}
			}
		var forceGET = "?n="+ parseInt(Math.random()*999999999);
		GlossaryRequest.open("GET", "../glossary.xml"+forceGET, true);
		GlossaryRequest.send(null); 
	}

	onload=createGlossary;
	onresize=getMidWindow;