// Insert this code into template
// <script type="text/javascript" src="/js/xref.js"></script>
// Dont forget to escape if going into php file
WR_xref = {
	aLookupData:[

// Format is {id: '#',t:'keyword to change to link',u:'enter url here',d:'description that shows up when link is hovered'},

{id:'1',t:'Link 1',u:'http://thisurl.com',d:'This link has been auto-generated'},
{id:'2',t:'Link 2',u:'http://andthis.com',d:'This link has also been automatically created'}
],
	fScanPageContents:function() {
		if (!document.body || !document.body.hasChildNodes()) return;
		this.rExcludedElements=/^A$/i;
		this.rAllowedElements=/^(DIV|SPAN|P|STRONG|B|EM|I)$/i;
		this.rMetaRE=/([\\\/\[\]\(\)\{\}\*\?\.\+])/g;
		this.rIgnoreRE=/\b(ignoreXref)\b/;
		this.sCheckSplit=0;
		this.aText = [];
		this.fScanText(document.body, 1, 'scan');
		if ((this.aText.length > 0) && (this.sCheckSplit == 1)) {
			var sText = ' ' + this.aText.join(' ') + ' ';
			sText = sText.replace(/[\n\r]+/g, ' ').replace(/\s+/g, ' ');
			sText = sText.replace(/(WR_BREAK )+/g, 'WR_BREAK ');
			var aTerms = [];
			var lookupLength = this.aLookupData.length;
			for (var i = 0; i < lookupLength; i++) {
				var oLookup = this.aLookupData[i];
				var oLookupRE = new RegExp('\\b(' + oLookup.t.replace(this.rMetaRE, "\\$1") + ')\\b', 'i');
				if (oLookupRE.test(sText)) {
					aTerms[aTerms.length] = oLookup;
					if (aTerms.length > 5) break;
				}
			}
			this.aLookupData = aTerms;
			this.fScanText(document.body, 1, 'replace');
		}
	},
	fValidParent:function(eElement) {
		if (eElement && eElement.nodeType && (eElement.nodeType == 1) && (eElement.hasChildNodes())) return true;
		else return false;
	},
	fValidNode:function(eElement, nodeClassAllowed) {
		if (nodeClassAllowed && eElement && eElement.nodeType && eElement.parentNode && eElement.parentNode.nodeName &&
			(eElement.nodeType == 3) &&
			this.rAllowedElements.test(eElement.parentNode.nodeName)) return true;
		else return false;
	},
	fScanText:function(eElement, nodeClassAllowed, scanType) {
		if (this.fValidNode(eElement, nodeClassAllowed)) {
			if (scanType == 'scan') {
				if (!this.sCheckSplit) this.sCheckSplit = (eElement.splitText) ? 1 : 2;
				this.aText[this.aText.length] = eElement.nodeValue || '';
			}
			else {
				termLoop :
				for (var i = 0; i < this.aLookupData.length; i++) {
					var theObj = this.aLookupData[i];
					if (theObj.linked) continue;
					if (window.location.href == theObj.u) continue;
					var theTerm = theObj.t;
					var oTermRE = new RegExp('\\b(' + theTerm.replace(this.rMetaRE, "\\$1") + ')\\b', 'i');
					while (oTermRE.test(eElement.nodeValue)) {
						var oExactRE = new RegExp('^' + theTerm.replace(this.rMetaRE, "\\$1") + '$', 'i');
						if (oExactRE.test(eElement.nodeValue)) {
							var theAnchor = document.createElement('A');
							theAnchor.href = theObj.u;
							theAnchor.className = 'article';
							theAnchor.target = 'new';
							if (theObj.d) theAnchor.title=theObj.d;
							theAnchor.appendChild(document.createTextNode(eElement.nodeValue));
							eElement.parentNode.replaceChild(theAnchor, eElement);
							theObj.linked = 1;
							break termLoop;
						}
						else {
							oTermRE.lastIndex = 0;
							var theStart = eElement.nodeValue.search(oTermRE);
							var theEnd = theStart + theTerm.length;
							if (theEnd != eElement.nodeValue.length) eElement.splitText(theEnd);
							if (theStart != 0) eElement.splitText(theStart);
							oTermRE.lastIndex = 0;
						}
					}
				}
			}
		}
		else if (this.fValidParent(eElement)) {
			if (scanType == 'scan') this.aText[this.aText.length] = 'WR_BREAK';
			if ((!this.rIgnoreRE.test(eElement.className)) && (!this.rExcludedElements.test(eElement.nodeName))) {
				for (var i = 0; i < eElement.childNodes.length; i++) {
					this.fScanText(eElement.childNodes[i], nodeClassAllowed, scanType);
				}
			}
			if (scanType == 'scan') this.aText[this.aText.length] = 'WR_BREAK';
		}
	}
};
if (document.getElementById && document.createElement && document.createTextNode) {
	WR_xref.fScanPageContents();
}
