/*
 * Package: CcAutoGlossary
 * Copyright (c) 2007, Clareo Consulting, LLC. All rights reserved.
 * Unauthorized use prohibited.
*/

var ccAutoGlossaryConfig = new Object();
	ccAutoGlossaryConfig.termSourceUrl           = '/utility/auto_glossary_xml.php';
	ccAutoGlossaryConfig.searchAreas             = new Array('contentPrimary', 'contentSecondary');
	ccAutoGlossaryConfig.searchableElements      = new Array('p', 'li');
	ccAutoGlossaryConfig.anchorClass             = 'glossaryTerm';
	ccAutoGlossaryConfig.anchorOnclickEvent      = 'return newGlossaryWin(this);';
	ccAutoGlossaryConfig.searchAreasCount        = ccAutoGlossaryConfig.searchAreas.length;
	ccAutoGlossaryConfig.searchableElementsCount = ccAutoGlossaryConfig.searchableElements.length;

var ccAutoGlossaryReceiveReq = getCcXmlHttpRequestObject();

var ccAutoGlossary = new Object();
	ccAutoGlossary.terms             = null;
	ccAutoGlossary.termsCount        = 0;
	ccAutoGlossary.processTermsIter  = 0;
	
window.onload   = initiateCcAutoGlossaryProcessing;	// Other than Opera
document.onload = initiateCcAutoGlossaryProcessing;	// Opera specific

function initiateCcAutoGlossaryProcessing()
{
	if (document.getElementById && document.getElementsByTagName && document.createTextNode && ccAutoGlossaryConfig && ccAutoGlossaryReceiveReq  && ccAutoGlossary)
	{
		if (ccAutoGlossaryReceiveReq.readyState == 4 || ccAutoGlossaryReceiveReq.readyState == 0)
		{
			ccAutoGlossaryReceiveReq.open('GET', ccAutoGlossaryConfig.termSourceUrl, true);
			ccAutoGlossaryReceiveReq.onreadystatechange = processCcAutoGlossary; 
			ccAutoGlossaryReceiveReq.send(null);
		}
	}
}

function processCcAutoGlossary()
{
	if (ccAutoGlossaryReceiveReq.readyState == 4 && ccAutoGlossaryReceiveReq.responseXML)
	{
		ccAutoGlossary.terms = ccAutoGlossaryReceiveReq.responseXML.getElementsByTagName('term');
		ccAutoGlossary.termsCount = ccAutoGlossary.terms.length;
		processCcAutoGlossaryTerms();
	}
}

function processCcAutoGlossaryTerms()
{
	if (ccAutoGlossary.processTermsIter < ccAutoGlossary.termsCount)
	{
		var searchTerm = ccAutoGlossary.terms[ccAutoGlossary.processTermsIter].firstChild.data.toLowerCase();
		var searchTermRegex = new RegExp('\\b' + searchTerm + '\\b');

		searchForTerm:
		for (iSearchAreas = 0; iSearchAreas < ccAutoGlossaryConfig.searchAreasCount; iSearchAreas++)
		{
			if (document.getElementById(ccAutoGlossaryConfig.searchAreas[iSearchAreas]))
			{
				var searchAreaElement = document.getElementById(ccAutoGlossaryConfig.searchAreas[iSearchAreas]);
				
				for (iSearchableElements = 0; iSearchableElements < ccAutoGlossaryConfig.searchableElementsCount; iSearchableElements++)
				{
					var searchElements = searchAreaElement.getElementsByTagName(ccAutoGlossaryConfig.searchableElements[iSearchableElements]);
					var searchElementsCount = searchElements.length;
					
					for (iElements = 0; iElements < searchElementsCount; iElements++)
					{
						termFound = markCcAutoGlossaryTerms(searchElements[iElements], searchTermRegex, searchTerm, ccAutoGlossary.terms[ccAutoGlossary.processTermsIter].getAttribute('tooltip'), ccAutoGlossary.terms[ccAutoGlossary.processTermsIter].getAttribute('url'));
						if (termFound)
						{
							break searchForTerm;
						}
					}
				}
			}
		}
		ccAutoGlossary.processTermsIter++;
		setTimeout('processCcAutoGlossaryTerms()', 1);
	}
}

function markCcAutoGlossaryTerms(searchedNode, termRegex, term, title, url)
{
	var nodeChildCount = searchedNode.childNodes.length;

	for (iSearchedNodeChildren = 0; iSearchedNodeChildren < nodeChildCount; iSearchedNodeChildren++)
	{
		node = searchedNode.childNodes[iSearchedNodeChildren];
		
		if (node.nodeType == 3)
		{
			var matchResult = termRegex.exec(node.nodeValue.toLowerCase());
			
			if (matchResult != null) 
			{
				newPrevSibNode = document.createTextNode(node.nodeValue.substr(0, matchResult.index));
				newNextSibNode = document.createTextNode(node.nodeValue.substr(matchResult.index + term.length));
				
				termTextNode = document.createTextNode(node.nodeValue.substr(matchResult.index, term.length));
				termDefinitionNode = document.createElement('dfn');
				termDefinitionNode.title = title;
				termDefinitionNode.appendChild(termTextNode);

				termAnchorNode = document.createElement('a');
				termAnchorNode.className = ccAutoGlossaryConfig.anchorClass;
				termAnchorNode.href = url;
				termAnchorNode.onclick = new Function(ccAutoGlossaryConfig.anchorOnclickEvent);
				termAnchorNode.appendChild(termDefinitionNode);
				
				searchedNode.insertBefore(newPrevSibNode,node);
				searchedNode.insertBefore(termAnchorNode,node);
				searchedNode.insertBefore(newNextSibNode,node);
				
				searchedNode.removeChild(node);
				return true;
			}
		}
	}
	return false;
}

function getCcXmlHttpRequestObject()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		return new ActiveXObject('Microsoft.XMLHTTP');
	}
	else
	{
		return false;
	}
}

