function isBrowserCompatible() {
    if (document.getElementById) { return true; }
    else { return false; }
}



// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of
// current session)
// [path] - path for which the cookie is valid (defaults to path of
// calling document)
// [domain] - domain for which the cookie is valid (defaults to domain
// of calling document)
// [secure] - Boolean value indicating if the cookie transmission
// requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments

function setCookie(name, value, expires, path, domain, secure) {
    var expiredate = new Date ();
    expiredate.setTime(expiredate.getTime() + (expires * 24 * 3600 * 1000));
    var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expiredate.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if
// cookie does not exist

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
	begin = dc.indexOf(prefix);
	if (begin != 0) return null;
    } else
	begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
	end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to
// create cookie)
// * path and domain default if assigned null or omitted if no
// explicit argument proceeds
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
	document.cookie = name + "=" + 
	    ((path) ? "; path=" + path : "") +
	    ((domain) ? "; domain=" + domain : "") +
	    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


var currentTooltip=null;
var fadeTimeoutId=null;
var fadeOpacity=0;
var STARTING_OPACITY=90;
var OPACITY_STEP=10;
var START_FADE_TIMEOUT=200;
var FADE_TIMEOUT=50;

function showTooltip(id, anchorId) {
    if (!isBrowserCompatible()) { return; }
    if (currentTooltip) {
	hideTooltipNow();
    }
    currentTooltip = document.getElementById(id);
    if (!currentTooltip) return;
    if (anchorId) {
	var anchor = document.getElementById(anchorId);
	if (anchor) {
	    var x = getPositionInDocument(anchor, "x");
	    var y = getPositionInDocument(anchor, "y");
	    var dw= document.getElementsByTagName('body')[0].offsetWidth;
	    x -= 10;

	    /* need to use this kludgy thing to determine the actual
	     * width of the tooltip object; 
	     * I tried always leaving the display set to "block" and
	     * using "visibility" to turn on/off the tooltip, but that 
	     * caused the tip to jump around on firefox. */
	    currentTooltip.style.visibility = 'hidden';
	    currentTooltip.style.display = 'block';
	    var w = currentTooltip.offsetWidth;
	    currentTooltip.style.display = 'none';
	    currentTooltip.style.visibility = 'visible';

	    if (x + w > dw-10) {
		x = dw-10-w;
		if (x < 0) x = 0;
	    }
	    currentTooltip.style.left = x + "px";
	    currentTooltip.style.top  = (y + anchor.offsetHeight + 5) + "px";
	}
    }
    setOpacity(currentTooltip, STARTING_OPACITY);
    currentTooltip.style.display = 'block';
}


function hideTooltip() {
    if (!isBrowserCompatible()) return;
    if (!currentTooltip) return;

    fadeOpacity = STARTING_OPACITY;
    fadeTimeoutId = setTimeout("fadeTooltip()", START_FADE_TIMEOUT);
}

function fadeTooltip() {
    fadeOpacity -= OPACITY_STEP;
    if (fadeOpacity > 0) {
	setOpacity(currentTooltip, fadeOpacity);
	fadeTimeoutId = setTimeout("fadeTooltip()", FADE_TIMEOUT);
    } else {
	fadeTimeoutId = null;
	hideTooltipNow();
    }
}

function setOpacity(obj, val) {
    if (obj.filters) {
	try {
	    obj.filters['alpha'].opacity = val;
	} catch(e) { }
    }
    else {
	/* hmm, for some reason even though we can set obj.style.opacity,
	 * I can't seem to be able to read it back; so hack around it */
	if (obj.style) {
	    obj.style.opacity = val/100;
	}
    }
}

function hideTooltipNow() {
    if (fadeTimeoutId) {
	clearTimeout(fadeTimeoutId);
	fadeTimeoutId = null;
    }
    currentTooltip.style.display = 'none';
    currentTooltip = null;
}

//get document position method
function getPositionInDocument(obj, xOrY) {
    var pos = (xOrY == 'x') ? obj.offsetLeft : obj.offsetTop;
    var tmp = obj.offsetParent;
    while (tmp != null) {
	pos += (xOrY == 'x') ? tmp.offsetLeft : tmp.offsetTop;
	tmp = tmp.offsetParent;
    }
    return pos;
}
