

/*      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 = false;	

	if (navigator.appName == 'Microsoft Internet Explorer'){IE = true;}

	function stayHome(m){	
		
		if (IE)
			{
			 var currX = event.clientX;
			 var currY = event.clientY;
			}
		else	{
			 var currX = m.pageX;
			 var currY = m.pageY;
			}
		var sL = document.documentElement.scrollLeft;	
		var sT = document.documentElement.scrollTop;	
		if (currX > midWindowWidth)
			{			
			 if (IE)
				{
				 tipContainer.style.left = Number(currX - msgWidth - 10 + sL) + 'px';
				}
			 else 	{
				 tipContainer.style.left = Number(currX - msgWidth - 10) + 'px';
				}
			}
		else	{
			 if (IE)
				{
				 tipContainer.style.left = Number(currX + 25 + sL) + 'px';
				}
			 else	{
				 tipContainer.style.left = Number(currX + 25) + 'px';
				}
			}		
		if (IE && currY >= midWindowHeight)
			{
			 tipContainer.style.top = Number(currY + sT - msgHeight) + 'px';			 
			}
		if (IE && currY < midWindowHeight)
			{
			 tipContainer.style.top = Number(currY + sT) + 'px'
			}
		if (!IE && currY - sT >= midWindowHeight)	
			{
			 tipContainer.style.top = Number(currY - msgHeight) + 'px';
			}	
		if (!IE && currY - sT < midWindowHeight)
			{
			 tipContainer.style.top = currY + 'px';
			}
	}	

	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(defineThis){

		var toolTip = getDefinition(defineThis);
		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(/(\<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='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();
				}
			}
	}

	onresize = getMidWindow;
	IE ? attachEvent('onload', init, false) : addEventListener('load', init, false);

