

/*      Copyright 2008, Michael J. Hill.  All rights reserved. Used with permission.  www.javascript-demos.com */
/*      Free use of the code, so long as the above notice is kept intact */

	var wordList = []; 
	var definitionList = [];
	var tipContainer = "";
	var midWindow = 0;	
	var midWindowWidth = 0;
	var midWindowHeight = 0;
	var msgWidth = 0;
	var msgHeight = 0;
	var IE = navigator.appName == "Microsoft Internet Explorer" ? true : false;	

	function stayHome(m){	
		
		if (!tipContainer)
			{
			 return;
			}
		var sL = document.documentElement.scrollLeft;	
		var sT = !IE ? document.documentElement.scrollTop : document.body.scrollTop;
		var currX = "";
		var currY = "";
		IE ? currX = event.clientX : currX = m.pageX - sL;
		IE ? currY = event.clientY : currY = m.pageY; 			
		currX > midWindowWidth ? tipContainer.style.left = Number(currX - msgWidth - 10 + sL) + "px" 
				       : tipContainer.style.left = Number(currX + 10 + sL) + "px";					
		IE && currY >= midWindowHeight ?  tipContainer.style.top = Number(currY + sT - msgHeight) + "px" : null;
		IE && currY < midWindowHeight ? tipContainer.style.top = Number(currY + sT) + "px" : null;		
		!IE && currY - sT >= midWindowHeight ? tipContainer.style.top = Number(currY - msgHeight) + "px" : null;
		!IE && currY - sT < midWindowHeight ? tipContainer.style.top = currY + "px" : null;			
	}	

	function getMidWindow(){		
		
		midWindowWidth = Math.round(document.documentElement.clientWidth/2);
		midWindowHeight = Math.round(document.documentElement.clientHeight/2);			
	}

	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].toLowerCase() == 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'))
			}
		msgWidth = tipContainer.clientWidth;
		msgHeight = tipContainer.clientHeight;	
	}

	function initTip(){

		tipContainer = document.getElementById('nFloat')
		tipContainer.style.display = 'none';
		IE ? document.attachEvent('onmousemove', stayHome, false) :  document.addEventListener('mousemove', stayHome, false);
		getMidWindow();
	}

	function initContext(){

		var rawText = document.getElementById('subjectMatter');
		var workText = rawText.innerHTML;  
		workText = workText.replace(/(\<)/gi," $1").replace(/(\>)/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='term'>$1<\/span> ");
			}
		rawText.innerHTML = workText;
	}

	function init(){

		var temp = [];
		for (i=0; i<nPairs.length; i++)
			{
			 temp = nPairs[i].split(">");
			 wordList[wordList.length] = temp[0].replace(/\s+$/,"");
			 definitionList[definitionList.length] = temp[1];
			}
		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();
				}
			}
	}

	IE ? attachEvent('onresize', getMidWindow, false) : addEventListener('resize', getMidWindow, false);
	IE ? attachEvent('onload', init, false) : addEventListener('load', init, false);
