/* Elements to search and parse links in */
var contentElements = new Array('content');

/* Keeps track of words already linked */
var wordsAlreadyLinked = '';

/* Misc settings */
var linkClassName = 'dictionary-link';

var excludedTagtypes = 'h1,h2,h3,h4,h5,h6';
var excludedElementIDs = 'breadcrumbs';
var excludedElementClasses = 'no-linking';

/* Should each individual term only be linked once per each document? */
var eachTermOnlyOnce = true;

function initParse()
{
	if (window.links != null)
	{
		for (var i = 0; i < contentElements.length; i++)
		{
			if (document.getElementById(contentElements[i]) != null)
			{
				parseLinks(document.getElementById(contentElements[i]));
			}
		}
	}
}

function parseLinks(element)
{
	for (var i = 0; i < links.length; i++)
	{
		var linkid = links[i][0];
		var term = links[i][2];
		var description = links[i][3];
		var url = '/pages/651?linkid=' + linkid;

		linkTerm(element, term, description, url);
	}
}

function linkTerm(node, term, description, url) {

	if (node.hasChildNodes)
	{
		var hi_cn;
		for (hi_cn = 0; hi_cn < node.childNodes.length; hi_cn++)
		{
			linkTerm(node.childNodes[hi_cn], term, description, url);
		}
	}
	
	if (node.nodeType == 3)
	{
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = term.toLowerCase();
		if (tempNodeVal.indexOf(tempWordVal) != -1)
		{
			pn = node.parentNode;
			
			if ((pn.className != "dictionary-link") && (!isLinked(node)) && (!isDisallowedElement(node,term)))
			{
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);

				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,term.length);
				after = document.createTextNode(nv.substr(ni+term.length));
				hiwordtext = document.createTextNode(docWordVal);

				hiword = document.createElement('a');
				hiword.className = linkClassName;
				hiword.setAttribute('href', url);
				hiword.setAttribute('title', description);
				hiword.setAttribute('onclick', "window.open(this.href,'wndLink','width=670,height=335,scrollbars=yes'); return false;");
				hiword.onclick = function() { window.open(this.href,'wndLink','width=670,height=335,scrollbars=yes'); return false; }
				hiword.appendChild(hiwordtext);			

				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);

				wordsAlreadyLinked += term.toLowerCase() + ',';
			}
		}
	}
}

function isLinked(node)
{
	while (node.parentNode != null)
	{
		if (node.parentNode.tagName != null)
		{
			if (node.parentNode.tagName.toLowerCase() == 'a')
			{
				return true;
			}
		}
		node = node.parentNode;
	}
	return false;
}

function isDisallowedElement(node,term)
{
	if (node.parentNode.tagName != null)
	{
		if (excludedTagtypes.indexOf(node.parentNode.tagName.toLowerCase()) > -1)
		{
			return true;
		}

		if ((node.parentNode.id != '') && (excludedElementIDs.indexOf(node.parentNode.id.toLowerCase()) > -1))
		{
			return true;
		}

		if ((node.parentNode.className != '') && (excludedElementClasses.indexOf(node.parentNode.className.toLowerCase()) > -1))
		{
			return true;
		}

		if (eachTermOnlyOnce)
		{
			if (wordsAlreadyLinked.indexOf(term.toLowerCase()) > -1)
			{
				return true;
			}
		}
	}
	return false;
}