// tooltip.js

function getScrollingPosition()
{
	var position = [0, 0];

 	// Firefox/Mozilla, Safari, Konqueror, Opera.
	if (typeof window.pageYOffset != 'undefined') {
   		position = [window.pageXOffset, window.pageYOffset];
 	
	// IE 6
	// IE 5 recognizes documentElement.scrollTop, but its value is always zero
	} else if (typeof document.documentElement.scrollTop != 'undefined' 
			   && document.documentElement.scrollTop > 0) {
   		
		position = [document.documentElement.scrollLeft, document.documentElement.scrollTop];
 	
	// IE 5, IE 6 (quirks mode) 
	} else if (typeof document.body.scrollTop != 'undefined') {
   		position = [ document.body.scrollLeft, document.body.scrollTop];
	}
 	return position;
}


// return size of the viewable browser window 

function getViewportSize()
{
	var size = [0, 0];

 	// all except Explorer ( browser window params, including scrollbars )
	if (typeof window.innerWidth != 'undefined') {
   		size = [window.innerWidth, window.innerHeight];
 	
	// IE 6 - strict mode (Mozilla 1.6 Strict also supports this but will pick up innerWidth/Height first in this function)
	} else if (typeof document.documentElement != 'undefined'
     			&& typeof document.documentElement.clientWidth != 'undefined' 
				&& document.documentElement.clientWidth != 0) {
   		
		size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
 	
	// IE 5, IE 6 - non-strict
	} else {
   		size = [ document.getElementsByTagName('body')[0].clientWidth, document.getElementsByTagName('body')[0].clientHeight];
 	}
	return size;
}


// display the tooltip

function showTip(event)
{
 	if (typeof event == "undefined") { event = window.event; }

 	var target = getEventTarget(event);

 	while (target.className == null || !/(^| )hastooltip( |$)/.test(target.className)) {
   		target = target.parentNode;
 	}

 	var tip = document.createElement("div");    // holder for our tooltip element (a floating layer)
 	var content = target.getAttribute("title"); // get tooltip text

 	target.tooltip = tip;
	
 	target.setAttribute("title", ""); // remove title to stop default browser tooltip functionality conflicting
									  // with our tooltip ('title' attribute automatically leads to default HTML tooltip)
									  // [Note:] This does not work for opera, it fails to remove the title text (and hence the default tip)
 	
	// if tooltip has an id it can be used for assigning an individual style in addition to
	// the tooltip class specified below
	if (target.getAttribute("id") != "") {
   		tip.setAttribute("id", target.getAttribute("id") + "tooltip");
 	}

 	tip.className = "tooltip"; // for css styling
 	tip.appendChild(document.createTextNode(content)); // add text to our tooltip

 	// position the tooltip
	var scrollingPosition = getScrollingPosition();
 	var cursorPosition = [0, 0];

 	if (typeof event.pageX != "undefined" && typeof event.x != "undefined") {
   		// using pageX/Y includes scrolling position in the coords
		cursorPosition[0] = event.pageX;
   		cursorPosition[1] = event.pageY;
 	} else {
		// using clientX/Y doesn't
   		cursorPosition[0] = event.clientX + scrollingPosition[0];
   		cursorPosition[1] = event.clientY + scrollingPosition[1];
 	}

    // append tooltip direct to 'body' tag using absolute positioning 
 	tip.style.position = "absolute";
 	tip.style.left = cursorPosition[0] + 10 + "px";
 	tip.style.top = cursorPosition[1] + 10 + "px";
	//alert("blah");
	document.getElementsByTagName("body")[0].appendChild(tip);
 	
	return true;
}


function showTip2(event)
{
 	if (typeof event == "undefined") { event = window.event; }
 	var target = getEventTarget(event);

 	while (target.className == null || !/(^| )hastooltip( |$)/.test(target.className)) {
   		target = target.parentNode;
 	}
	
	var xpos = target.getAttribute("xpos");
	var ypos = target.getAttribute("ypos");
 	
	var tip = document.createElement("div");
 	var content = target.getAttribute("title");

 	target.tooltip = tip;
 	target.setAttribute("title", "");

 	if (target.getAttribute("id") != "") {
   		tip.setAttribute("id", target.getAttribute("id") + "tooltip");
 	}

 	tip.className = "tooltip";
 	tip.appendChild(document.createTextNode(content));

	// get mouse coordinates
 	
	var scrollingPosition = getScrollingPosition();
 	var cursorPosition = [0,0];
	
	// IE5 on Mac implements pageX/Y incorrectly - we check for 'x' as well which IE5 Mac doesn't support
	// Opera 8 doesn't support 'x' either, luckily it supports clientX/Y though
 	if (typeof event.pageX != "undefined" && typeof event.x != "undefined") {
   		cursorPosition[0] = event.pageX;
   		cursorPosition[1] = event.pageY;
 	} else {
   		cursorPosition[0] = event.clientX + scrollingPosition[0];
   		cursorPosition[1] = event.clientY + scrollingPosition[1];
 	}
    
	// we attach the tooltip directly to the body of the document using absolute positioning
	// based on our cursor coordinates
 	tip.style.position = "absolute";
 	//tip.style.left = cursorPosition[0] + 0 + "px";
 	//tip.style.top  = cursorPosition[1] + 10 + "px";
	tip.style.left = xpos + "px";
 	tip.style.top  = ypos + "px";
	
	tip.style.visibility = "hidden"; // we hide it until we have double checked its positioning on the screen

	// note use of 'visibility' rather than 'display' here.  'Display: none' means that the element effectively doesn't
	// exist and so has no dimensions that can be measured. 
	// 'visibility: hidden' keeps an elements dimensions/location in place, just hides it

 	document.getElementsByTagName("body")[0].appendChild(tip);
    
	/*
	// check to see if the tooltip is near the screen edge and adjust coordinates if neccessary.
 	// we need to assess the position of the cursor relative to the viewport (viewable browser screen area)
	// we would use clientX/Y here but Safari returns incorrect values
	var viewportSize = getViewportSize();

	//if (getEvent(event)) {alert("cursor: " + cursorPosition[0] + ", " + cursorPosition[1] 
	//						      + "\nscroll: " + scrollingPosition[0] + ", " + scrollingPosition[1]);}
	
	// check/adjust width
 	if (cursorPosition[0] - scrollingPosition[0] + 10 + tip.offsetWidth > viewportSize[0] - 25) {
   		tip.style.left = scrollingPosition[0] + viewportSize[0] - 25 - tip.offsetWidth + "px";
 	} else {
   		tip.style.left = cursorPosition[0] + 10 + "px";
 	}

	// check/adjust height
 	if (cursorPosition[1] - scrollingPosition[1] + 10 + tip.offsetHeight > viewportSize[1] - 25) {
   		
		if (event.clientX > (viewportSize[0] - 25 - tip.offsetWidth)) {
     		tip.style.top = cursorPosition[1] - tip.offsetHeight - 10 + "px";
   		
		} else {
     		tip.style.top = scrollingPosition[1] + viewportSize[1] - 25 - tip.offsetHeight + "px";
   		}
 	
	} else {
   		tip.style.top = cursorPosition[1] + 10 + "px";
 	}
	*/
 	tip.style.visibility = "visible"; // postioning adjusted so lets pimp that tip
 	
	return true;
}


function hideTip(event)
{
	if (typeof event == "undefined") { event = window.event; }

 	var target = getEventTarget(event);

 	while (target.className == null || !/(^| )hastooltip( |$)/.test(target.className)) {
   		target = target.parentNode;
 	}	

 	if (target.tooltip != null) {
   		target.setAttribute("title", target.tooltip.childNodes[0].nodeValue); // reset taget attribute
   		
		// 'target.tooltip.parentNode' will be the 'body' tag
		target.tooltip.parentNode.removeChild(target.tooltip); // remove tooltip
 	}
	return false;
}

function getEvent(event) {
	if(typeof event.relatedTarget == 'undefined' || event.relatedTarget != null) return true;
	return false;
}

function initTooltips()
{
 	// get tooltips array
	var tips = getElementsByAttribute("class", "hastooltip");
 	for (var i = 0; i < tips.length; i++) {
   		attachEventListener(tips[i], "mouseover", showTip2, false);
   		attachEventListener(tips[i], "mouseout", hideTip, false);
 	}
 	return true;
}

//addLoadListener(init);
addLoadListener(initTooltips);