
var disappeardelay=0  //tooltip disappear delay (in miliseconds)
var verticaloffset=0 //vertical offset of tooltip from anchor link, if any

/////No further editting needed
var ie=document.all
var ns6=document.getElementById&&!document.all


function getposOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	var setleftContainer=0;
	while (parentEl!=null){
		if (parentEl.id=="container")
			setleftContainer=parentEl.offsetLeft;
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	if (offsettype=="left") 
		totaloffset=totaloffset-setleftContainer;
	return totaloffset;
}

function showhide(obj, e){
	dropmenuobj.style.left=dropmenuobj.style.top="-500px"
	
	if (e.type=="click")
		obj.visibility="visible"
}

function iecompattest(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge, balloonPosition){
	if (whichedge=="rightedge"){
		edgeoffsetx=0
		var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
		dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
		
		if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure) {
				edgeoffsetx=dropmenuobj.contentmeasure-obj.offsetWidth
		}
		if (dropmenuobj.x - edgeoffsetx <= 0) {
			edgeoffsetx = dropmenuobj.x - 15;
		}
		return edgeoffsetx
	}

	else{
		edgeoffsety=0
		var topedge=ie && !window.opera? iecompattest().scrollTop : window.pageYOffset
		var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
		dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
		
		if (balloonPosition=="top") //|| windowedge-dropmenuobj.y < dropmenuobj.contentmeasure) //move up?
			edgeoffsety=dropmenuobj.contentmeasure+obj.offsetHeight+(verticaloffset*2)
		
		return edgeoffsety
	}
}

function displayballoontip(obj, e, contextHelpId, balloonPosition){ //main ballooon tooltip function
	
	if (window.event) event.cancelBubble=true
	
	else if (e.stopPropagation) e.stopPropagation()
	
	if (typeof dropmenuobj!="undefined") //hide previous tooltip?
		
		dropmenuobj.style.visibility="hidden"
	
	clearhidemenu()
	
	dropmenuobj=document.getElementById(contextHelpId)

	showhide(dropmenuobj.style, e)

	dropmenuobj.x=getposOffset(obj, "left")

	dropmenuobj.y=getposOffset(obj, "top")+verticaloffset

	dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge", balloonPosition)+"px"

	dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "topedge", balloonPosition)+obj.offsetHeight+"px"	

}

function delayhidemenu(){
	dropmenuobj.style.visibility='hidden'; 
	dropmenuobj.style.left=0; 
}

function clearhidemenu(){
	if (typeof delayhide!="undefined")
		clearTimeout(delayhide)
}

function reltoelement(contextHelpId){ //tests if a link has "rel" defined and it's the ID of an element on page
	return (contextHelpId!=null && contextHelpId!="" && document.getElementById(contextHelpId)!=null && document.getElementById(contextHelpId).className=="balloonstyle")? true : false
}


/* Call this function on the onmouseover event of the link
	 Parameters:
				- obj: object that call the function
				- contextHelpId: ID of the context help (layer) that will be open
				- ballonPosition: position of the layer in relation of the link (top or bottom)
				- refObj (optional): in the case that the balloon must appear in other place (like an input)
				- focusButton (optional): in the case that an element inside the iframe need focus when the contextual help opens
*/
function initalizetooltip(obj,contextHelpId, balloonPosition, refObj, focusButton){

	refObj = refObj || "";	
	focusButton = focusButton || "";
	
	var iframeId = contextHelpId.substring(0,contextHelpId.indexOf("DivId"));
	var iframeObj = document.getElementById(iframeId);
	
	if (reltoelement(contextHelpId)){

		obj.onclick=function(e){
			var evtobj=window.event? window.event : e;
			if(refObj==""){
				displayballoontip(this, evtobj, contextHelpId, balloonPosition);
				if(focusButton != "") {
					iframeObj.contentWindow.focus();
					if(ie)
						iframeObj.contentWindow.document.getElementById(focusButton).focus();
					else
						iframeObj.contentDocument.getElementById(focusButton).focus();
				}
			} else {
				displayballoontip(refObj, evtobj, contextHelpId, balloonPosition);
				if(focusButton != ""){
					iframeObj.contentWindow.focus();
					if(ie)
						iframeObj.contentWindow.document.getElementById(focusButton).focus();
					else
						iframeObj.contentDocument.getElementById(focusButton).focus();
				}
			}
		}
	}
}
