/* Version 3.1
 * Andrew Neil for AHC 8/2004
 * Copyright AHC Ltd
 */
 
CLEAN = true;
gGlossaryPopUpId="popupGlossary";


function hideGlossary()
{
	 popId = document.getElementById(gGlossaryPopUpId);
	 popId.style.visibility = "hidden"
}


function showHelpTip(e,pGlossaryTermObject, sHtml)
{

	var el = e.target || e.srcElement;
	while (el.tagName != "SPAN")
		el = el.parentNode;
	
	if (el._helpTip) {
		helpTipHandler.hideHelpTip(el);
	}

	helpTipHandler.createHelpTip(el, sHtml);
	
	helpTipHandler.positionToolTip(e);

	el.onblur =  helpTipHandler.anchorBlur;
	el.onkeydown =  helpTipHandler.anchorKeyDown;
	pGlossaryTermObject.onmouseout = hideGlossary;
}


var helpTipHandler = {
	hideSelects:	false,
	
	helpTip:		null,
	
	create:	function () {
		var d = document.createElement("DIV");
		d.id = gGlossaryPopUpId;
		d.className = "help-tooltip";
		document.body.appendChild(d);		
		this.helpTip = d;
	},
	
	createHelpTip:	function (el, sHtml) {
		if (this.helpTip == null) {
			this.create();
		}

		var d = this.helpTip;
		d.innerHTML = sHtml;
		d._boundAnchor = el;
		el._helpTip = d;
		return d;
	},
	
	helpTipMouseDown:	function (e) {
		var d = this;
		var el = d._boundAnchor;
		if (!e) e = event;
		var t = e.target || e.srcElement;
		while (t.tagName != "A" && t != d)
			t = t.parentNode;
		if (t == d) return;
		
		el._onblur = el.onblur;
		el.onblur = null;
	},
	
	helpTipMouseUp:	function () {
		var d = this;
		var el = d._boundAnchor;
		el.onblur = el._onblur;
		el._onblur = null;
		el.focus();
	},	
	
	anchorBlur:	function (e) {
		var el = this;
		helpTipHandler.hideHelpTip(el);
	},
	
	anchorKeyDown:	function (e) {
		if (!e) e = window.event
		if (e.keyCode == 27) {	// ESC
			helpTipHandler.hideHelpTip(this);
		}
	},
	
	removeHelpTip:	function (d) {
		d._boundAnchor = null;
		d.style.filter = "none";
		d.innerHTML = "";
		d.onmousedown = null;
		d.onmouseup = null;
		d.parentNode.removeChild(d);
	},
	
	hideHelpTip:	function (el) {
		var d = el._helpTip;
		/*	Mozilla (1.2+) starts a selection session when moved
			and this destroys the mouse events until reloaded
		d.style.top = -el.offsetHeight - 100 + "px";
		*/		
		
		d.style.visibility = "hidden";
		
		el.onblur = null;
		el._onblur = null;
		el._helpTip = null;
		el.onkeydown = null;

	},
	
	positionToolTip:	function (e) {
		var scroll = this.getScroll();
		var d = this.helpTip;
		
		if (d.offsetWidth >= scroll.width)
			d.style.width = scroll.width - 10 + "px";
		else
			d.style.width = "";
		
		if (e.clientX > scroll.width - d.offsetWidth)
			d.style.left = scroll.width - (d.offsetWidth + 50) + scroll.left + "px";
		else
			d.style.left = e.clientX - 2 + scroll.left + "px";
		
		if (e.clientY + d.offsetHeight + 18 < scroll.height)
			d.style.top = e.clientY + 18 + scroll.top + "px";
		else if (e.clientY - d.offsetHeight > 0)
			d.style.top = e.clientY + scroll.top - d.offsetHeight + "px";
		else
			d.style.top = scroll.top + 5 + "px";
			
		d.style.visibility = "visible";
	},
	
	getScroll:	function () {
		if (document.all && typeof document.body.scrollTop != "undefined") {	
			var ieBox = document.compatMode != "CSS1Compat";
			var cont = ieBox ? document.body : document.documentElement;
			return {
				left:	cont.scrollLeft,
				top:	cont.scrollTop,
				width:	cont.clientWidth,
				height:	cont.clientHeight
			};
		}
		else {
			return {
				left:	window.pageXOffset,
				top:	window.pageYOffset,
				width:	window.innerWidth,
				height:	window.innerHeight
			};
		}
	}
}; 


function showGlossaryExplanation(e,pGlossaryTermObject,pForcedExplanation)
{

	if (document.body==null || (!checkBrowserIsCapable()))
	{
		return false; 
	} 

	if (pForcedExplanation)
	{
		tGlossaryTermText = pForcedExplanation;
	}
	else
	{
		tGlossaryTermText = pGlossaryTermObject.innerHTML;
		pForcedExplanation = 0;
	}

	tGlossaryTermText = findGlossaryTermExplanation(tGlossaryTermText, pForcedExplanation);
	showHelpTip(e,pGlossaryTermObject,tGlossaryTermText);
}


function findGlossaryTermExplanation(pGlossaryTermText, pForcedExplanation)
{
	if (CLEAN)
	{
		pGlossaryTermText =	tidy(pGlossaryTermText);
	}
	glossaryEntryIndex=0;
	
	while  ( glossaryEntryIndex < ( gGlossaryTable.length - 1 ) 
		&& ( gGlossaryTable[glossaryEntryIndex][0].toLowerCase() != pGlossaryTermText.toLowerCase() ) ) 
 	{
				glossaryEntryIndex++;
	} 
	if (gGlossaryTable[glossaryEntryIndex][0].toLowerCase() != pGlossaryTermText.toLowerCase() && pForcedExplanation == 0)
	{
	
		outputHTML = "<strong> Error</strong><br>No explanation found."; 
	}
	else if (gGlossaryTable[glossaryEntryIndex][0].toLowerCase() != pGlossaryTermText.toLowerCase() && pForcedExplanation)
	{
		outputHTML = pForcedExplanation;
	}
	else 
	{
		outputHTML = "<strong>"+gGlossaryTable[glossaryEntryIndex][0]+"</strong><br>"+gGlossaryTable[glossaryEntryIndex][1]; 
	}
	return outputHTML;
} 


function tidy(pString)
{
	pString = pString.replace(/\r\n|\r|\n|\t/g, '');
	
	pString = pString.replace(/  +/g, ' ');

	return pString;
}

function checkBrowserIsCapable()
{
	if(document.getElementById) return true;
	else return false;
} 