// JavaScript Document
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");
	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));
	var scrollingPosition = getScrollingPosition();
	var cursorPosition = [0, 0];
	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];
	}
	tip.style.position = "absolute";
	tip.style.left = cursorPosition[0] + 10 + "px";
	tip.style.top = cursorPosition[1] + 10 + "px";
	tip.style.visibility = "hidden";
	document.getElementsByTagName("body")[0].appendChild(tip);
	var viewportSize = getViewportSize();
	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";
	}
	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";
	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);
		target.tooltip.parentNode.removeChild(target.tooltip);
	}
	return false;
}

function initTooltips() {
	var tips = getElementsByAttribute("class", "hastooltip");
	for (var i = 0; i < tips.length; i++){
		addEvent(tips[i], "mouseover", showTip, false);
		addEvent(tips[i], "mouseout", hideTip, false);
	}
	return true;
}

function getEventTarget(event){
	var targetElement = null;
	if (typeof event.target != "undefined"){
		targetElement = event.target;
	}else{
		targetElement = event.srcElement;
	}
	while (targetElement.nodeType == 3 && targetElement.parentNode != null){
		targetElement = targetElement.parentNode;
	}
	return targetElement;
}

function getViewportSize(){
	var size = [0, 0];
	if (typeof window.innerWidth != 'undefined'){
		size = [window.innerWidth, window.innerHeight];
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
		size = [document.documentElement.clientWidth,document.documentElement.clientHeight];
	}else{
		size = [document.getElementsByTagName('body')[0].clientWidth,document.getElementsByTagName('body')[0].clientHeight];
	}
	return size;
}

function getScrollingPosition(){
	var position = [0, 0];
	if (typeof window.pageYOffset != 'undefined'){
		position = [
		window.pageXOffset,
		window.pageYOffset
		];
	}
	else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0){
		position = [
		document.documentElement.scrollLeft,
		document.documentElement.scrollTop
		];
	}
	else if (typeof document.body.scrollTop != 'undefined'){
		position = [
		document.body.scrollLeft,
		document.body.scrollTop
		];
	}
	return position;
}

function getElementsByAttribute(attribute, attributeValue){
	var elementArray = new Array();
	var matchedArray = new Array();
	if (document.all){
		elementArray = document.all;
	}else{
		elementArray = document.getElementsByTagName("*");
	}
	for (var i = 0; i < elementArray.length; i++){
		if (attribute == "class"){
			var pattern = new RegExp("(^| )" +
			attributeValue + "( |$)");
			if (pattern.test(elementArray[i].className)){
				matchedArray[matchedArray.length] = elementArray[i];
			}
		}
		else if (attribute == "for"){
			if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for")){
				if (elementArray[i].htmlFor == attributeValue){
					matchedArray[matchedArray.length] = elementArray[i];
				}
			}
		}
		else if (elementArray[i].getAttribute(attribute) == attributeValue){
			matchedArray[matchedArray.length] = elementArray[i];
		}
	}
	return matchedArray;
}

function addEvent(elm, evType, fn, useCapture)
	// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
	// By Scott Andrew
	{
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

addEvent(window, 'load', initTooltips, false);