/*---------------------------------------------
 * tooltipv1.0
 * Vijayakrishnan
 * Phykon Solutions (P) Ltd.
 *---------------------------------------------*/

/**
 * global variables
 *---------------------------------------------*/
var activeTooltip = ''; // id of active tooltip

/**
 * add a tooltip to the specified comnponent
 * p_ElementID - the element to add the tooltip to
 * p_Title - the title of the tooltip, if blank title will not be shown
 * p_Content - the content of the tooltip, can be blank
 * p_ElementToAdd - DOM element to append to tooltip after content, can be null
 * p_AutoHideDelay - no:of milliseconds after which tooltip should be hidden
 *---------------------------------------------*/
function addTooltip(p_ElementID, p_Title, p_Content, p_ElementToAdd, p_AutoHideDelay, p_ShowClose) {
	var elem = document.getElementById(p_ElementID);
	if(!elem) {
		alert("Cannot find component '"+p_ElementID+"'");
		return;
	}
	// create and add tooltip
	var container = document.createElement('div');
	container.id = p_ElementID+'_tooltip1_Container';
	container.innerHTML = '';
	container.className = 'tooltip1_Container';
	container.setAttribute('element', p_ElementID);
	container.setAttribute('autohide', '1');
	container.setAttribute('autohidedelay', p_AutoHideDelay);
	if(p_Title!='' || p_ShowClose==true) {
		var title = document.createElement('div');
		title.className = 'tooltip1_Title';
		title.innerHTML = p_Title;
		if(p_ShowClose==true) {
			var closeBtn = document.createElement('div');
			closeBtn.className = 'tooltip1_Close_OFF';
			closeBtn.innerHTML = '&nbsp;';
			closeBtn.onmouseover = new Function ("this.className='tooltip1_Close_ON';");
			closeBtn.onmouseout = new Function ("this.className='tooltip1_Close_OFF';");
			closeBtn.onclick = new Function ("document.getElementById('"+container.id+"').style.visibility = 'hidden';");
			if(p_Title=='') title.innerHTML = '&nbsp;';
			title.appendChild(closeBtn);
		}
		container.appendChild(title);
	}
	var content = document.createElement('div');
	content.className = 'tooltip1_Content';
	content.innerHTML = p_Content;
	container.appendChild(content);
	if(p_ElementToAdd!=null) {
		container.appendChild(p_ElementToAdd);
	}
	document.body.appendChild(container);
	
	// setup callback to show/hide the tooltip
	elem.onmousemove = _onMouseMove_Element;
	elem.onmouseout = _onMouseOut_Element;
	container.onmousemove = _onMouseMove_Tooltip;
	container.onmouseout = _onMouseOut_Tooltip;
}



/**
 * internal - should NOT be called from outside this file
 *---------------------------------------------*/
function _onMouseMove_Element(e) {
	var elem = this;
	var tooltip = document.getElementById(elem.id+'_tooltip1_Container');
	if(!elem || !tooltip) {
		//alert("Tooltip1.0 internal error!");
		return;
	}
	
	// setup tooltip
	if(tooltip.style.visibility!='visible') {
		
		// get mouse position
		var mouseX;
		var mouseY;
		if (document.all) { // grab the x-y pos.s if browser is IE
			try {
				mouseX = event.clientX + document.body.scrollLeft;
				mouseY = event.clientY + document.body.scrollTop;
			}
			catch(error) {}
		} else {  // grab the x-y pos.s if browser is NS
			mouseX = e.pageX;
			mouseY = e.pageY;
		}
		if (mouseX < 0){mouseX = 0}	// catch possible negative values in NS4
		if (mouseY < 0){mouseY = 0}
	
		tooltip.style.left = (mouseX+15)+'px'; // show the tooltiup near the cursor with a little offset (15, 15)
		tooltip.style.top = (mouseY+15)+'px';
	}
	
	tooltip.setAttribute('autohide', '0');
	tooltip.style.visibility = 'visible';
	
	if(activeTooltip!='' && activeTooltip!=tooltip.id) {
		document.getElementById(activeTooltip).style.visibility = 'hidden';
	}
	activeTooltip = tooltip.id;
}

/**
 * internal - should NOT be called from outside this file
 *---------------------------------------------*/
function _onMouseOut_Element() {
	var elem = this;
	var tooltip = document.getElementById(elem.id+'_tooltip1_Container');
	if(!elem || !tooltip) {
		//alert("Tooltip1.0 internal error!");
		return;
	}
	tooltip.setAttribute('autohide', '1');
	tooltip.setAttribute('autohidestart', (new Date()).getTime());
	setTimeout("_hideTooltip('"+elem.id+"');", tooltip.getAttribute('autohidedelay'));
}

/**
 * internal - should NOT be called from outside this file
 *---------------------------------------------*/
function _onMouseMove_Tooltip(e) {
	var tooltip = this;
	tooltip.setAttribute('autohide', '0');
	tooltip.style.visibility = 'visible';
	if(activeTooltip!='' && activeTooltip!=tooltip.id) {
		document.getElementById(activeTooltip).style.visibility = 'hidden';
	}
	activeTooltip = tooltip.id;
}

/**
 * internal - should NOT be called from outside this file
 *---------------------------------------------*/
function _onMouseOut_Tooltip() {
	var tooltip = this;
	tooltip.setAttribute('autohide', '1');
	tooltip.setAttribute('autohidestart', (new Date()).getTime());
	setTimeout("_hideTooltip('"+tooltip.getAttribute('element')+"');", tooltip.getAttribute('autohidedelay'));
}

/**
 * internal - should NOT be called from outside this file
 *---------------------------------------------*/
function _hideTooltip(p_ElementId) {
	var tooltip = document.getElementById(p_ElementId+'_tooltip1_Container');
	if(tooltip.getAttribute('autohide')=='1') {
		var curTime = (new Date()).getTime();
		if(curTime-tooltip.getAttribute('autohidestart') >= tooltip.getAttribute('autohidedelay')) {
			tooltip.style.visibility = 'hidden';
		}
	}
}

/**
 * internal - should NOT be called from outside this file
 *---------------------------------------------*/
function _getElementPosition(p_Element){
	var e = p_Element;
	if(!e) return {x:0, y:0};
	var left = 0;
	var top  = 0;
	while (e.offsetParent){
		left += e.offsetLeft;// + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		top  += e.offsetTop;//  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		e     = e.offsetParent;
	}
	left += e.offsetLeft;// + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	top  += e.offsetTop;//  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
	return {x:left, y:top};
}
