function getPosition(obj) {
	var objPosition = new Object({'top':obj.offsetTop,'left':obj.offsetLeft});
	if(obj.offsetParent.tagName!="BODY" && obj.offsetParent.tagName!="HTML") {
		var parentPosition = getPosition(obj.offsetParent);
		objPosition.top += parentPosition.top;
		objPosition.left += parentPosition.left;
	}
	return objPosition;
}

var curSpan;

function parse_helptips() {
	var spans = document.getElementsByTagName("span");
	for(var i=0;i<spans.length;i++) {
		if(spans[i].className=="helptip") {
			spans[i].onmouseover = show_helptip;
			spans[i].onmouseout = hide_helptip_ok;
		}
	}
	
	var reg = /helptip/;
		
	var divs = document.getElementsByTagName("div");
	for(var i=0;i<divs.length;i++) {
		if(reg.exec(divs[i].className)) {
			divs[i].onmouseover = show_helptip;
			divs[i].onmouseout = hide_helptip_ok;
		}
	}
	
}

function build_helptips() {
	
	var helptipBackground = document.createElement("div");
	helptipBackground.innerHTML = '<iframe id="helptipBackground" frameborder="0"></iframe>';
	document.body.appendChild(helptipBackground);
	
	var helptip_container = document.createElement("div");
	helptip_container.className = "helptip_container";
	helptip_container.id = "helptip_container"; 
	helptip_container.innerHTML = ''+
			'<table cellspacing="0" cellpadding="0" border="0" class="helptip_table">'+
				'<tr>'+
					'<td class="topleft">&nbsp;</td>'+
					'<td class="top">&nbsp;</td>'+
					'<td class="topright">&nbsp;</td>'+
				'</tr>'+
				'<tr>'+
					'<td class="left"><div></div></td>'+
					'<td class="center" id="helptip_content">&nbsp;</td>'+
					'<td class="right"><div></div></td>'+
				'</tr>'+
				'<tr>'+
					'<td class="bottomleft">&nbsp;</td>'+
					'<td class="bottom">&nbsp;</td>'+
					'<td class="bottomright">&nbsp;</td>'+
				'</tr>'+
			'</table>'+
		'<div class="pointer"></div>';
	document.body.appendChild(helptip_container);
	parse_helptips();	
}


function show_helptip(event) {
	var span = window.event ? window.event.srcElement : event.currentTarget;
	var helptip_container = document.getElementById("helptip_container");
	var helptip_content = document.getElementById("helptip_content");
	helptip_content.innerHTML = span.title;
	span.title = "";
	curSpan = span;
	
	
	var pageWidth = window.innerWidth || self.innerWidth || (document.documentElement&&document.documentElement.clientWidth) || document.body.clientWidth;
	var pageHeight = window.innerHeight || self.innerHeight || (document.documentElement&&document.documentElement.clientHeight) || document.body.clientHeight;
	var scrollTop = parseInt(document.getElementsByTagName("html")[0].scrollTop);
	var top = parseInt(window.event ? window.event.clientY : event.clientY);
	var left = parseInt(window.event ? window.event.clientX : event.clientX);
	var height = parseInt(helptip_container.offsetHeight);
	var width = parseInt(helptip_container.offsetWidth);
	
	
	
	var verticalAlign = 'top';
	var horizontalAlign = 'left';
	
	var obj = span;
	var spanPosition = new Object({'top':obj.offsetTop,'left':obj.offsetLeft});	
	while(obj.offsetParent.tagName!="BODY" && obj.offsetParent.tagName!="HTML") {
		obj = obj.offsetParent;
		spanPosition.top +=obj.offsetTop;
		spanPosition.left +=obj.offsetLeft;
		if(!obj.offsetParent) break;
	}
	
	var helptipBackground = document.getElementById("helptipBackground");
	helptipBackground.style.height = height + "px";
	helptipBackground.style.width = width + "px";
	
	
	
	if(top+height>pageHeight) {
		helptip_container.style.top = spanPosition.top - height + "px";
		helptipBackground.style.top = spanPosition.top - height + "px";
		verticalAlign = 'bottom';
	}
	else {
		helptip_container.style.top = spanPosition.top + span.offsetHeight + "px";
		helptipBackground.style.top = spanPosition.top + span.offsetHeight + "px";
	}
	
	if(left+width>pageWidth) {
		helptip_container.style.left = spanPosition.left - width + "px";
		helptipBackground.style.left = spanPosition.left - width + "px";
		horizontalAlign = 'right';
	}
	else {
		helptip_container.style.left = spanPosition.left + span.offsetWidth + "px";
		helptipBackground.style.left = spanPosition.left + span.offsetWidth + "px";
	}
	
	((helptip_container.childNodes)[1]).className = verticalAlign+horizontalAlign+"Pointer";
	
	
	helptipBackground.style.display = "block";
	helptip_container.style.visibility = "visible";
}


function hide_helptip(event) {
	setTimeout(hide_helptip_ok,250);
}

function hide_helptip_ok(event) {
	if(curSpan) {
		curSpan.title = document.getElementById("helptip_content").innerHTML;
		document.getElementById("helptip_container").style.visibility = "hidden";
		document.getElementById("helptip_content").innerHTML = "&nbsp;";
		document.getElementById("helptipBackground").style.display = "none";
		curSpan = false;
	} 
}

if (window.addEventListener) window.addEventListener("load", build_helptips, false);
if (window.attachEvent) window.attachEvent("onload", build_helptips);

