/*
 * Derived from Stan Slaughter's tooltips.js
 * E-mail: Stan_Slaughter@Yahoo.Com
 * Web: http://www.StanSight.Com/
 */

function tooltips(msg, pos, animate, displayseconds, width, autohide) {
	// apply defaults
	if (displayseconds == undefined)
		displayseconds = 0;
	if (width == undefined)
		width = false;
	if (animate == undefined)
		animate = true;
	if (autohide == undefined)
		autohide = true;
	if (pos == undefined)
		pos = "right";

	// Get object reference to element which triggered this event
	var event = (this.event) ? this.event : tooltips.caller.arguments[0]
			|| window.event;
	var obj = event.target ? event.target : event.srcElement;

	// Show Tooltip
	showTooltips(obj, msg, pos, animate, displayseconds, width, autohide);
}

function hideTooltips() {
	$('tip').hide();
	$('shadowTip').hide();
}

var HideTipTimer = 0;
function showTooltips(obj, msg, pos, animate, displayseconds, width, autohide) {

	// Figure out where the input field object is on the screen by adding
	// up all the offsets for all the containing parent objects
	var obj_pos = FindPosition(obj);
	var tip_left = obj_pos[0];
	var tip_top = obj_pos[1];

	// Create DIV tag object through DOM to hold the "tip" message (if it does
	// not already exist)
	tipdiv = $("tip");
	shadow = $("shadowTip");
	if (tipdiv != null) {
		tipdiv.remove();
		shadow.remove();
	}
	if (document.getElementById("tip") == null) {

		// Create tip box DIV
		tipdiv = document.createElement("div");
		document.body.appendChild(tipdiv);
		tipdiv.setAttribute("id", "tip");
		tipdiv.className = "tooltip " + pos;

		// Create pointer image
		img = document.createElement("img");
		tipdiv.appendChild(img);
		img.setAttribute("src", "/images/tooltip_arrow_" + pos + ".gif");

		// Create Paragraph to hold message contents
		tipdivP = document.createElement("p");
		tipdiv.appendChild(tipdivP);
		tipdivP.setAttribute("id", "tipP");

		// Create shadow tip box
		shadowdiv = document.createElement("div");
		shadowdiv.setAttribute("id", "shadowTip");
		document.body.appendChild(shadowdiv);
		shadowdiv.className = "shadow";
		shadowdiv.innerHTML = "&nbsp;"

		// Allow a click on the "tip" div to hide the message.
		tipdiv.onclick = function(aEvent) {
			document.getElementById("shadowTip").style.display = "none";
			document.getElementById("tip").style.display = "none";
		}

	} else {
		// Div Tag allready esists, clear out old timer then get reference to
		// tip box
		clearTimeout(HideTipTimer);
		tipdiv = document.getElementById("tip");
		tipdiv.style.display = "none"; // Hide any old message

		tipdivP = document.getElementById("tipP");
		tipdivP.innerHTML = "";

		shadowdiv = document.getElementById("shadowTip");
		shadowdiv.style.display = "none";
	}

	// Roughly figure how how wide to make tip box DIV based on the number
	// of characters in the message. If max width is exceeded then just use max
	// width
	var iFontSize = 10;
	var iCharWidth = iFontSize / 2; // Assume characters are half as wide as
									// they are tall
	var iMaxWidth = width || 250;
	var iNumChars = msg.stripTags().length;
	var tip_width = width || ((iNumChars * iCharWidth + 6 > iMaxWidth) ? iMaxWidth : iNumChars * iCharWidth + 6);

	// Set tip box width and font size
	tipdiv.style.width = tip_width + "px";
	tipdiv.style.fontSize = iFontSize + "px";

	// If message is not empty then postion and show tip box
	var display_type = "none";
	if (msg != "") {
		display_type = "block";

		// Assign the message to the tip box
		tipdivP.innerHTML = msg;
		var tipdivWidth = parseInt(tipdiv.style.width);

		// Keep tip box from running off right of screen
		var widthPad = 25; // scroll bar and shadow offset
		if (tip_left + widthPad + tipdivWidth > document.body.offsetWidth) {
			var diff = (tip_left + widthPad + tipdivWidth) - document.body.offsetWidth;
			tip_left -= diff;
		}

		// If tip is being displayed then wait 'displayseconds' seconds then
		// hide the tip box DIV
		if (display_type == "block" && displayseconds != 0) {
			var hideTipCmd = 'document.getElementById("shadowTip").style.display="none";' + 'document.getElementById("tip").style.display="none";';
			displayseconds = displayseconds * 1000;
			HideTipTimer = setTimeout(hideTipCmd, displayseconds);
		}

		// Hide the tooltip when the element looses focus
		Event.observe(obj, 'blur', function() {
			$('shadowTip').hide();
			$('tip').hide();
		});

		// Need to do this hack to display the div offscreen so that we can get
		// the
		// calculated height of the div
		saveTop = tipdiv.style.top;
		tipdiv.style.top = "-100px";
		tipdiv.style.display = display_type;

		// Set shadows width and height based in tip box *after* message is
		// assigned
		shadowdiv.style.width = tipdivWidth + "px";
		shadowdiv.style.height = (tipdiv.offsetHeight - 1) + "px";

		// Calculate position
		// tip_top += obj.offsetHeight;
		switch (pos) {
		case "top":
			tip_top -= tipdiv.offsetHeight - 8;
			tip_left -= 16;
			break;

		default:
		case "right":
			tip_left += obj.offsetWidth - 10;
			break;

		case "bottom":
			tip_top += obj.offsetHeight + 6;
			tip_left -= 24;
			break;

		case "left":
			tip_top -= 0;
			tip_left -= tipdiv.offsetWidth + 20;
			break;
		}

		// Hide the tip and set back its original location
		tipdiv.style.display = "none";

		tipdiv.style.top = tip_top + "px";
		tipdiv.style.left = tip_left + "px";

		// Position shadow DIV
		shadowdiv.style.top = (2 + tip_top) + "px";
		shadowdiv.style.left = (2 + tip_left) + "px";

		// Finally, show the tooltip
		shadowdiv.style.display = display_type;
		if(animate)
			Effect.Appear('tip', {
				duration : 0.3
			});
		else
			$('tip').show();
	}

}

function FindPosition(obj) {
	// Figure out where the obj object is in the page by adding
	// up all the offsets for all the containing parent objects
	if (obj == null)
		return [ 0, 0 ];

	// Assign the obj object to a temp variable
	tmpObj = obj;

	// Get the offsets for the current object
	var obj_left = tmpObj.offsetLeft;
	var obj_top = tmpObj.offsetTop;

	// If the current object has a parent (ie contained in a table, div, etc..)
	if (tmpObj.offsetParent) {

		// Loop through all the parents and add up their offsets
		// The while loop will end when no more parents exist and a null is
		// returned
		while (tmpObj = tmpObj.offsetParent) {
			obj_left += tmpObj.offsetLeft;
			obj_top += tmpObj.offsetTop;
		}
	}
	return [ obj_left, obj_top ];
}
