/*javascript for Bubble Tooltips by Alessandro Fulciniti
- http://pro.html.it - http://web-graphics.com */

var tooltipTimer;
var tooltipOffsetX = 10;
var tooltipOffsetY = 10;

var IFRAME_ID = "bubbletip_iframe";
var BUBBLETIP_ID = "bubbletip";

// check whether Opera and Firefox browser is being used
var is_opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
var is_firefox = (navigator.userAgent.toLowerCase().indexOf("firefox") != -1);
var is_msie = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);

function enableTooltip(opt)
{
	var links, i, h, s;
	var id, tag, cls;
	
	if (!document.getElementById || !document.getElementsByTagName) return;

	AddCss();
	
	if (opt)
	{
		id = opt.id;
		tag = opt.tag;
		cls = opt.cls;
	}
	
	h = document.createElement("div");
	h.id = BUBBLETIP_ID;
	h.setAttribute("id", BUBBLETIP_ID);
	h.style.position = "absolute";
	h.style.visibility = (h.style.visibility == "visible" ? "hidden" : "visible");
	h.style.display = (h.style.display == "block" ? "none" : "block");
	h.style.zIndex = 99;
		
	document.getElementsByTagName("body")[0].appendChild(h);
	
	if (id == null)
		links = document.getElementsByTagName(tag);
	else
		links = document.getElementById(id).getElementsByTagName(tag);
	for (i = 0; i < links.length; i++)
	{
		if (cls != null)
		{
			if (links[i].className == cls)
			{
				Prepare(links[i]);
			}
		} else
		{
			Prepare(links[i]);
		}
	}
}

function Prepare(el)
{
	var tooltip, t, b, s, l;
	
	t = el.getAttribute("help");
	if (t != null && t.length > 0)
	{
		tooltip = CreateEl("div", "tooltip");
		s = CreateEl("div", "top");
		s.innerHTML = t;
		tooltip.appendChild(s);
		b = CreateEl("div", "bottom");
		tooltip.appendChild(b);
		setOpacity(tooltip);
		
		el.tooltip = tooltip;
//		el.onmouseover = showTooltip;
		el.onmousedown = showTooltip;
		el.onmouseout = hideTooltip;
//		el.onmousemove = Locate;	// to allow tooltip to move as mouse moves over target
	}
}

function showTooltip(e)
{
	// these codes are for el.onmouseover delay before showing tooltip
//	var thatTooltip = this.tooltip;
//	tooltipTimer = setTimeout(function() {
//						document.getElementById(BUBBLETIP_ID).appendChild(thatTooltip);
//						Locate(e);
//			}, 200);

	document.getElementById(BUBBLETIP_ID).appendChild(this.tooltip);
	Locate(e);
	AdjustIFrame(document.getElementById(BUBBLETIP_ID));
}

function hideTooltip(e)
{
	tooltipTimer = setTimeout(function() {
								var h = document.getElementById(BUBBLETIP_ID);
								// when remove div.tooltip child content, div.bubbletip will auto-collapse in size to become invisible
								if (h.childNodes.length > 0) h.removeChild(h.firstChild);

								if (is_msie)
								{
									var iFrame = document.getElementById(IFRAME_ID);
									iFrame.style.visibility = "hidden";
									iFrame.style.display = "none";
								}
				   }, 2000);				   
	
	// these codes are to clear el.onmouseover delay if el.onmouseout before the delay
//	clearTimeout(tooltipTimer);
}

function setOpacity(el)
{
	el.style.filter = "alpha(opacity:95)";
	el.style.KHTMLOpacity = "0.95";
	el.style.MozOpacity = "0.95";
	el.style.opacity = "0.95";
}

function CreateEl(t, c)
{
	var x = document.createElement(t);
	
	x.id = c;
	x.className = c;
	x.style.display = "block";
	
	return(x);
}

function AddCss()
{
	var l = CreateEl("link");
	
	l.setAttribute("type", "text/css");
	l.setAttribute("rel", "stylesheet");
	l.setAttribute("href", "js/tooltip/css/bubbletip.css");
	l.setAttribute("media", "screen");
	document.getElementsByTagName("head")[0].appendChild(l);
}

function Locate(e)
{
	var posx=0, posy=0;
	
	if (e == null) e = window.event;
	if (e.pageX || e.pageY)
	{
	    posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY)
	{
	    if (document.documentElement.scrollTop)
		{
	        posx = e.clientX + document.documentElement.scrollLeft;
	        posy = e.clientY + document.documentElement.scrollTop;
	    } else
		{
	        posx = e.clientX + document.body.scrollLeft;
	        posy = e.clientY + document.body.scrollTop;
	    }
	}
	
	document.getElementById(BUBBLETIP_ID).style.top = (posy + tooltipOffsetY) + "px";
	document.getElementById(BUBBLETIP_ID).style.left = (posx + tooltipOffsetX) + "px";
}

/**
  * Use an "iFrame shim" to deal with problems where the tooltip shows up behind selection list elements.
  */
function AdjustIFrame( el )
{
	// we know that Opera and Firefox do not need iFrame shimming around combo-boxes.
	if (!is_msie) return;
  
	// put a try/catch block around the whole thing, just in case
	try {
		if (!document.getElementById(IFRAME_ID))
		{
			// don't use innerHTML to update the body, because it can cause global variables
			// that are currently pointing to objects on the page to have bad references
			//document.body.innerHTML += "<iframe id='" + IFRAME_ID + "' src='javascript:false;' scrolling='no' frameborder='0'>";
			var newNode = document.createElement("iFrame");
			newNode.setAttribute("id", IFRAME_ID);
			newNode.setAttribute("src", "/blank.html");
			newNode.setAttribute("allowtransparency", true);
			newNode.setAttribute("scrolling", "no");
			newNode.setAttribute("frameborder", "0");
			document.body.appendChild(newNode);
		}

		var iFrame = document.getElementById(IFRAME_ID);

		try {
			iFrame.style.position = "absolute";
			iFrame.style.width = el.offsetWidth;
			iFrame.style.height = el.offsetHeight;
			iFrame.style.top = el.style.top;
			iFrame.style.left = el.style.left;
			iFrame.style.zIndex = el.style.zIndex - 2;
			iFrame.style.visibility = el.style.visibility;
			iFrame.style.display = el.style.display;
		} catch(e) { }
	} catch (ee) {}
 
}
