
function getPosition(elem) {
    var x = 0;
    var y = 0;
    var callerElement = elem;

    while (elem.offsetParent) {
        x += elem.offsetLeft + (elem.clientLeft || 0);
        y += elem.offsetTop + (elem.clientTop || 0);
        elem = elem.offsetParent;
    }

    if(callerElement.tagName == "AREA") {
        // if the element trigging this is an image map area: do some magic vodoo
        /*@cc_on
            @if (@_jscript_version <= 6)
                var parentPosition = getPosition(document.getElementById(callerElement.parentNode.id+"Image"));
                y = parentPosition.y;
                x = parentPosition.x;
            @end
        @*/
        var areaCoords = callerElement.coords.split(",");
        x += parseInt(areaCoords[0]);
        y += parseInt(areaCoords[1]);
    }
    return {x: x, y: y};
}


var Tooltip = {
	opened: false,
	keepOpen: true,
	expandIt: null,
	isImageMap: null,
    open: function(caller) {
		if (!Tooltip.opened) {
            Tooltip.keepOpen = false;
			Tooltip.expandIt = (caller.className.indexOf("expandIt") > 0)?1:0;

			if (getElementsByClassName("tooltip").length > 0)
				var box = document.getElementById(caller.id + "Tooltip");
			else
	        	var box = document.getElementById("tooltip"); 
			
			var position = getPosition(caller);
             
             box.style.display = "block";
             
			 if (hasClass(document.getElementsByTagName("BODY")[0], "start"))
			 	box.style.left = position.x - (caller.lastChild.scrollWidth / 2) + 29 - (caller.scrollWidth / 2) + "px";
			 else
			 	box.style.left = position.x - (caller.lastChild.scrollWidth / 2) + 29 - (caller.scrollWidth / 2) + "px";
  
             box.style.top = position.y - caller.lastChild.scrollHeight - 0 + "px";
             
			Tooltip.opened = true;
		}
    },
    close: function(caller) {
		if (!Tooltip.keepOpen) {
			if (getElementsByClassName("tooltip").length > 0)
				var box = document.getElementById(caller.id + "Tooltip");
			else
	        	var box = document.getElementById("tooltip"); 
            box.style.display = "none";
			Tooltip.opened = false;
			return false;
		}
    },
	forceClose: function() {
        var toolTips = getElementsByClassName("tooltip");
        for(var i = 0; i < toolTips.length; i++) {
            toolTips[i].style.display = "none";
        }
        var expandTooltipAreas = getElementsByClassName("expandArea");
        for(var b = 0; b < expandTooltipAreas.length; b++) {
            expandTooltipAreas[b].style.visibility = "hidden";
            expandTooltipAreas[b].style.display = "none";
        }

        Tooltip.opened = false;
		Tooltip.keepOpen = false;
		return false;
	},
	populate: function(caller) {
		Tooltip.expandIt = (hasClass(caller, "expandIt"))?1:0;
		if (Tooltip.expandIt) {
			if (getElementsByClassName("tooltip").length > 0) {
				document.getElementById(caller.id + "TooltipExpanded").style.visibility = "visible";
                document.getElementById(caller.id + "TooltipExpanded").style.display = "block";
            }
            else {
	        	document.getElementById("expandTooltip").style.visibility = "visible";
                document.getElementById("expandTooltip").style.display = "block";
            }
        }
		else
			document.getElementById("showTooltipInfo").style.display = "block";
		Tooltip.keepOpen = true;
		Tooltip.opened = true;
	},
    init: function() {
		var boxes = getElementsByClassName("tooltip").length > 0;
        if (boxes && initCount == 0) {
            /*@cc_on
                @if (@_jscript_version <= 6)
                    // make sure it's not ie7 or later
                    if(!window.XMLHttpRequest) {
                        var boxElements = getElementsByClassName("tooltip");
                        for(var i = 0; i < boxElements.length; i++) {
                            var dropShadowContainer = document.createElement("SPAN");
                            dropShadowContainer.className = "dropShadow";
                            boxElements[i].appendChild(dropShadowContainer);
                        }
                    }
                @end
            @*/
            var anchors = getElementsByClassName("showtip");
            for (var i = 0; i < anchors.length; i++) {
                anchors[i].onmouseover = function() { Tooltip.open(this); return false; };
 				anchors[i].onmouseout = function() { Tooltip.close(this); return false; };
                if(hasClass(anchors[i], "expandIt")) {
                    anchors[i].onclick = function() { Tooltip.populate(this); return false; };
                }
                /*else {
                    anchors[i].onclick = function() { return false; };
                }*/
            }
			var closebtn = getElementsByClassName("closeTooltip");
			for (var i = 0; i < closebtn.length; i++) {
				closebtn[i].onclick = function() { Tooltip.forceClose(); return false; };
			}
        }
        initCount++;
	}
}
var initCount = 0; // this is an ugly temp solution.. why does it init twice?
OnLoadHandler.appendFunction("Tooltip.init()", true);


