// HDTVSolutions copy
// _glossary_tips.js
//
// Author: T.M.
// Creation: 1/27/2006
//
// This script is responsible for the creation of the glossary popup boxes
// that appear when the user hovers over a glossary word that appears in an
// article.
//
// The definition is assumed to be present in the TITLE attribute of the
// corresponding parent element

window.onload = pc_init;

// All javascript body onload functions must be named pc_init
// to allow functionality when user is logged in\
function pc_init() {
	if (document.getElementsByTagName) {
		var allSpans = document.getElementsByTagName('SPAN');
		for (i = 0; i < allSpans.length; i++) {
			if (allSpans[i].className == "gloss") {
				allSpans[i].onmouseover = showDefinition;
				allSpans[i].onmouseout = hideDefinition;
				allSpans[i].setAttribute("defText", allSpans[i].getAttribute("title"));
				allSpans[i].title = "";
			}
		}
	}

}

function showDefinition(e) {
	if (!e) var e = window.event;
	if (e.target) {
		target = e.target;
	}
	else if (e.srcElement) {
		target = e.srcElement;
	}
	else {
		return;
	}
	if (target.nodeType == 3) // defeat Safari bug
		target = target.parentNode;
	
	if (target.getAttribute("defText") == "") {
		return false;
	}
	
	if (target.nodeName != "SPAN" && target.parentNode.nodeName == "SPAN") {
		target = target.parentNode; // The ad blaster seems to mess up the HTML on occasion; this is a workaround (12/8/06)
	}
	var pageWidth, pageHeight;
	if (self.innerWidth) {
		// Non-IE Browsers
		pageWidth = self.innerWidth;
		pageHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		// IE 6 strict mode
		pageWidth = document.documentElement.clientWidth;
		pageHeight = document.documentElement.clientHeight;
	}
	else if (document.body) {
		// Other IEs
		pageWidth = document.body.clientWidth;
		pageHeight = document.body.clientHeight;
	}
	// Get mouse position for later
	var mouseX = 0;
	if (e.pageX || e.pageY) {
		mouseX = e.pageX;
	}
	else if (e.clientX || e.clientY) {
		mouseX = e.clientX + document.body.scrollLeft;
	}
	
	// Check for fringe case where pupup may already exist
	glossTest = document.getElementById("glossTip");
	if (glossTest != null) {
		glossTest.parentNode.removeChild(glossTest);
	}
	
	definitionNode = document.createElement('DIV');
	definitionNode.id = "glossTip";
	definitionNode.innerHTML = target.getAttribute("defText");
	definitionNode.style.position = "absolute";

	/*defPosRight = (pageWidth - findPosX(target) - target.offsetWidth - 10);

	definitionNode.style.right = defPosRight + "px";
	definitionNode.style.bottom = (pageHeight - findPosY(target)) + "px";*/
	
	articleNode = document.getElementById("article_text");
	articleNode.appendChild(definitionNode);
		
	// Position the tooltip like so:
	// +------------------------------------------+
	// | ....... ...... ..... .. ....  ...        |
	// | .... ... .. .... .. ... ... ... .....    |
	// +------------------------------------------+
	// .. ..... .... .... .......[HIGHLIGHTED WORD]
	
	// If top of popup is above the top of the window, move to below the highlighted phrase
	topX = findPosY(target) - definitionNode.offsetHeight;
	maxPosLeft = target.parentNode.offsetWidth + findPosX(target.parentNode);
	minPosLeft = findPosX(target.parentNode);
	posLeft = Math.max(findPosX(target) - definitionNode.offsetWidth + target.offsetWidth, minPosLeft);
	
	if (topX <= 0) {
		definitionNode.style.top = (findPosY(target) + target.offsetHeight + 10) + "px";
	}
	else {
		definitionNode.style.top = (topX) + "px";
	}
	
	// Make sure popup doesn't appear way out in right field. This can happen if the highlighted phrase
	// has a line break in the middle, causing the width of the element to be equal to the width of the page
	
	// This checks if we're off the screen
	// If we over over a split word, the popup might appear on the right of the screen
	// but the mouse is on the left part. Check for this and move popup accordingly
	if (mouseX < findPosX(target)) {
		posLeft = findPosX(target.parentNode);
	}
	// We're drawing the box too far off the screen (also for split phrases
	else if (posLeft > maxPosLeft) {
		posLeft = Math.max(0,maxPosLeft - definitionNode.offsetWidth);
	}
	
	definitionNode.style.left = posLeft + "px"; /**/
		
	return true;
}

function hideDefinition() {
	definitionNode = document.getElementById("glossTip");
	
	if (definitionNode != null && definitionNode.parentNode != null)
		definitionNode.parentNode.removeChild(definitionNode);
	
	return true;
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}
