function tooltip_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 tooltip_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;
}

function tooltip_show(tooltipId, parentElement) {
	it = document.getElementById(tooltipId);
	bodyElement = document.documentElement ? document.documentElement : document.body;

	x = tooltip_findPosX(parentElement);
	y = tooltip_findPosY(parentElement);
	scrollY = scrollTop();
	heightY = clientHeight();

	it.style.width = null;
	it.style.height = null;

	// need to fixate default size (MSIE problem)
	it.style.width = it.offsetWidth - 2 + 'px';
	it.style.height = it.offsetHeight - 2 + 'px';

	if (y - it.offsetHeight - 10 > scrollY)
		y = y - it.offsetHeight - 10;
	else if (y +  parentElement.offsetHeight + it.offsetHeight + 10 < scrollY + heightY)
		y = y + parentElement.offsetHeight + 10;
	else {
		x = x - it.offsetWidth - 10;
		y = y - ( (y + it.offsetHeight + 10) - (scrollY + heightY) );
	}

	if (x + it.offsetWidth > bodyElement.offsetWidth - 10) x = x - (x + it.offsetWidth - bodyElement.offsetWidth) - 10;
	if (x < 0 ) x = 0; 

  	it.style.top = y + 'px';
	it.style.left = x + 'px';
	it.style.visibility = 'visible'; 
}

function tooltip_hide(id) {

    it = document.getElementById(id); 
    it.style.visibility = 'hidden';

}

function clientHeight() {
	return filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function scrollTop() {
	return filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

