//
// Functions to support tooltips.  Callers should use
// onmouseover="showToolTip(event, 'text for tooltip')" onmouseout="scheduleHideToolTip()"
// parameters on tags.  Somewhere on the page there should be a div with id="tooltip".
//

var timeoutId;

function showToolTip(evt, text) {
	showToolTipParams(evt, text, "tooltip", 5, 20);
}

function showToolTipWithCloseButton(evt, text) {
	showToolTipParams(evt, text, "tooltip", null, null, "closebutton");
}

function toggleToolTipWithCloseButton(evt, text) {
	showToolTipParams(evt, text, "tooltip", null, null, "closebutton", "true");
}

function showContextHelp(evt, text) {
	var styleId = "contexthelptip";
	var offsettop = 15;
	var offsetleft = -320;
	var tooltip = getElement(styleId);

	if (text && tooltip && tooltip.style.visibility != "visible") {
		var top;
		var left;
		if (evt.pageX) { // DOM event model
			left = evt.pageX;
			top = evt.pageY;
		} else { // IE event model
			evt = window.event;
			if (ie7) {
				left = evt.clientX + document.documentElement.scrollLeft;
				top = evt.clientY + document.documentElement.scrollTop;
			} else {
				left = evt.clientX + document.body.scrollLeft;
				top = evt.clientY + document.body.scrollTop;
			}
		}
		if (tooltip.style.visibility.toString() != "visible") {
			
			tooltip.innerHTML = text;
			if (offsettop) {
				tooltip.style.top = (top + offsettop) + "px";
			} else {
				tooltip.style.top = "50%";
			}
			if (offsetleft) {
				tooltip.style.left = (left + offsetleft) + "px";
			} else {
				tooltip.style.left = "50%";
			}
			tooltip.style.visibility = "visible";
		}
	} 
}

function showButtonTip(evt, text) {
	var styleId = "buttontip";
	var offsettop = 10;
	var offsetleft = -30;
	var tooltip = getElement(styleId);
	var buttontiparrow = getElement("buttontiparrow");

	if (text && tooltip && tooltip.style.visibility != "visible") {
		var top;
		var left;
		if (evt.pageX) { // DOM event model
			left = evt.pageX;
			top = evt.pageY;
		} else { // IE event model
			evt = window.event;
			if (ie7) {
				left = evt.clientX + document.documentElement.scrollLeft;
				top = evt.clientY + document.documentElement.scrollTop;
			} else {
				left = evt.clientX + document.body.scrollLeft;
				top = evt.clientY + document.body.scrollTop;
			}
		}
		if (tooltip.style.visibility.toString() != "visible") {
			
			tooltip.innerHTML = text;
			if (offsettop) {
				tooltip.style.top = (top + offsettop) + "px";
			} else {
				tooltip.style.top = "50%";
			}
			if (offsetleft) {
				tooltip.style.left = (left + offsetleft) + "px";
			} else {
				tooltip.style.left = "50%";
			}
			tooltip.style.visibility = "visible";
		}
		if (buttontiparrow) {
			buttontiparrow.style.top = (top + offsettop - 11) + "px";
			buttontiparrow.style.left = (left + offsetleft + 25) + "px";
			buttontiparrow.style.visibility = "visible";
		}
	} else if (toggle == "true" && tooltip && tooltip.style.visibility == "visible") {
		tooltip.style.visibility = "hidden";
		tooltip.style.left = "-1000px";
		tooltip.style.top = "-1000px";
		if (buttontiparrow) {
			buttontiparrow.style.visibility = "hidden";
			buttontiparrow.style.left = "-1000px";
			buttontiparrow.style.top = "-1000px";
		}
    }
}

function showLevelTipClose(evt, text) {
	showToolTipParams(evt, text, "leveltip", 5, 5);
}



function showLevelTip(evt, text) {
	showToolTipParams(evt, text, "leveltip", 40, 20);
}

function showLevelSelectTip(evt, text) {
	showToolTipParams(evt, text, "levelselecttip", 15, 20);
}

function showLevelTipAbove(evt, text) {
	var tooltip = getElement("leveltip");
	    
	if (text && tooltip && tooltip.style.visibility != "visible") {
		var top;
		var left;
		if (evt.pageX) { // DOM event model
			left = evt.pageX;
	        top = evt.pageY;
		} else { // IE event model
			evt = window.event;
			if (ie7) {
				left = evt.clientX + document.documentElement.scrollLeft;
				top = evt.clientY + document.documentElement.scrollTop;
			} else {
				left = evt.clientX + document.body.scrollLeft;
		        top = evt.clientY + document.body.scrollTop;
	        }
		}

	    if (tooltip.style.visibility.toString() != "visible") {
        	tooltip.innerHTML = text;
        	if (tooltip.offsetHeight) {
        		tooltip.style.top = (top - 30 - tooltip.offsetHeight) + "px";
        	} else {
        		// offsetHeight not supported, revert to putting tip below cursor
        		tooltip.style.top = (top + 40) + "px";
        	}
        	tooltip.style.left = (left + 20) + "px";
	        tooltip.style.visibility = "visible";
	    }
    } 
}


function showToolTipParams(evt, text, styleId, offsettop, offsetleft, closeButtonStyleId, toggle) {
	var tooltip = getElement(styleId);
	    
	if (text && tooltip && tooltip.style.visibility != "visible") {
		var top;
		var left;
		if (evt.pageX) { // DOM event model
			left = evt.pageX;
	        top = evt.pageY;
		} else { // IE event model
			evt = window.event;
			if (ie7) {
				left = evt.clientX + document.documentElement.scrollLeft;
				top = evt.clientY + document.documentElement.scrollTop;
			} else {
				left = evt.clientX + document.body.scrollLeft;
		        top = evt.clientY + document.body.scrollTop;
	        }
		}

	    if (tooltip.style.visibility.toString() != "visible") {
	    	if (closeButtonStyleId) {
	    		var closeButton = getElement(closeButtonStyleId);
	        	tooltip.innerHTML = closeButton.innerHTML + text;
	        } else {
	        	tooltip.innerHTML = text;
	        }
	        if (offsettop) {
	        	tooltip.style.top = (top + offsettop) + "px";
	        } else {
	        	tooltip.style.top = "50%";
	        }
	        if (offsetleft) {
	        	tooltip.style.left = (left + offsetleft) + "px";
	        } else {
	        	tooltip.style.left = "50%";
	        }
	        tooltip.style.visibility = "visible";
	    }
    } else if (toggle == "true" && tooltip && tooltip.style.visibility == "visible") {
    	tooltip.style.visibility = "hidden";
    	tooltip.style.left = "-1000px";
    	tooltip.style.top = "-1000px";
    }
}



function scheduleHideToolTip() {
	scheduleHideToolTipById("tooltip");
}

function scheduleHideLevelTip() {
	hideToolTipById("leveltip");
}

function scheduleHideButtonTip() {
	hideToolTipById("buttontip");
}

function scheduleHideLevelSelectTip() {
	hideToolTipById("levelselecttip");
}

function scheduleHideContextHelp() {
	scheduleHideToolTipById("contexthelptip");
}

function scheduleHideToolTipById(styleId) {
	window.clearTimeout(timeoutId);
    timeoutId = window.setTimeout("hideToolTipById('" + styleId + "')", 0);
}



function hideToolTip() {
	hideToolTipById("tooltip");
}

function hideToolTipById(styleId) {
	var tooltip = getElement(styleId);
	if (tooltip) {
	    tooltip.style.visibility = "hidden";
	    tooltip.left = "-1000px";
	    tooltip.top = "-1000px";
	    tooltip.innerHTML = "";
    }
    
    var buttontiparrow = getElement("buttontiparrow");
    if (buttontiparrow) {
    	buttontiparrow.style.visibility = "hidden";
		buttontiparrow.style.left = "-1000px";
    	buttontiparrow.style.top = "-1000px";
    }
}
