var g_tooltip = null;
var g_tooltip_showing = 0;
var g_tooltip_link = null;
var g_tooltip_previous_click = null;


var _get_event_src = function(e) { 
	if (e){ return e.target; }
	if (window.event){ return window.event.srcElement; }
	return null;
}

function doc_mousedown(e){
	if (_get_event_src(e) == g_tooltip_link){
		g_tooltip.onmousedown = function(){};
	}else{
		hide_tooltip();
	}
}

function hide_tooltip(){
	

	if (!g_tooltip){
		return false;
	}
	g_tooltip.onmousedown = function(){};

	g_tooltip_showing = 0;
	g_tooltip.style.display = 'none';
	g_tooltip_link = 'null';


	return true;
}

//function move_children(e_from, e_to){
//
//	while(e_from.childNodes.length){
//		e_to.appendChild(e_from.removeChild(e_from.childNodes[0]));
//	}
//}




// _find_x & _find_y courtesy PPK
// http://www.quirksmode.org/js/findpos.html
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 init_tooltip(){
//	g_tooltip_elm = document.createElement('DIV');
//	g_tooltip_elm.className = 'ToolTip';
//	g_tooltip_elm.style.display = 'none';
//	document.body.appendChild(g_tooltip_elm);
//}

function show_tooltip(link, text){

//	if (!g_tooltip_elm){
//		init_tooltip();
//	}

	if (g_tooltip_showing){
		if (g_tooltip_link == link){
			hide_tooltip();
			return;
		}
		hide_tooltip();
	}

	g_tooltip = document.getElementById (text);
	if (!g_tooltip) {
		return;
	}
	

	var x = tooltip_findPosX(link);
	var y = tooltip_findPosY(link);

	g_tooltip.style.left = (x+50)+'px';
	g_tooltip.style.top = (y-20)+'px';


	g_tooltip_showing = 1;
	g_tooltip.style.display = 'inline';
	g_tooltip_link = link;

	//document.onmousedown = doc_mousedown;
	g_tooltip.onmousedown = doc_mousedown;
}