/*
 * Remote Tooltip Loader
 * =====================
 * This functionlib provides the functionality to remotly load
 * information through AJAX calls and present them in a tooltip
 * under a specific parent element in the HTML DOM tree.
 * 
 * Required files:
 * 	- tooltip.js
 * 	- prototype.js
 * 	- images/tooltip/load_bar.gif
 * 
 * CSS styling can be applied to the tooltips by adding a special
 * rule for the .tooltip class.
 */

/*
 * Destroys a generated tooltip with the specified _id.
 * Should be called from the onMouseOverEvent of the caller.
 */
function dTooltip(_id) {
	if(!created(_id)) $(document.getElementById(_id)).remove();
}

/*
 * Checks if a tooltip has been created yet with the
 * specified _id.
 * Usefull for preventing duplicates.
 */
function created(_id){
	return (null == document.getElementById(_id));
}

/*
 * Constructs a new tooltip container with the provided
 * _id, and automaticly applied the correct properties
 * to the newly created object.
 * Returns the newly created object.
 */
function buildTooltip(_id, _type) {
	// Create the new HTML element that acts as a tooltip.
	var tooltip = new Element(_type, {'id' : _id});
	
	// Append the element to the DOM tree.
	document.body.appendChild(tooltip);
	
	// Configure the tooltip.
	tooltip.addClassName('tooltip');
	tooltip.setStyle({position: 'absolute', left: 0, right: 0, margin: 0, padding: 0});
	
	// return the object.
	return tooltip
}

/*
 * Relocates the tool tip to the bottom right corner of the parent.
 * The parent in most cases is the caller of the tooltip function.
 */
function relocateTooltip(_tooltip, _parent) {
	// initial offset positions
	var left = $(_parent).getWidth();
	var top = 0;
		
	// position our tooltip witht the calculated values
	$(_tooltip).clonePosition(_parent, {
		'setLeft': true,
		'setTop': true,
		'setWidth': false,
		'setHeight':false,
		'offsetLeft': left,
		'offsetTop': top
	});
}

/*
 * Loads the contents of the remote destination specified
 * by _url into the innerHTML of the provided _tooltip
 * container.
 * Relies on an asynchronous AJAX call.
 */
function loadContent(_tooltip, _url, _caller) {
	new Ajax.Request(_url, {
		method: 'get',
		onSuccess: function(transport) {
			$(_tooltip).innerHTML = transport.responseText;
			relocateTooltip(_tooltip, _caller);
		},
		onCreate: function(transport) {
			$(_tooltip).innerHTML = '<img src="/images/tooltip/load_bar.gif" alt="Loading..." />';
			relocateTooltip(_tooltip, _caller);
		}
	});
}

/*
 * Creates a new tooltip for the specified _caller. The _caller object
 * will become the parent of the tooltip and will be used for calculating
 * the offset position of the new tooltip. The origin will always be the bottom
 * right corner of the parent.
 * 
 * _ id needs to be a unique id in the whole document, or the creation of the tooltip
 * will be aborted.
 * 
 * _type determines the type of HTML object to generate and act as a tooltip container.
 * Defaults to a DIV element.
 * 
 * _url is the remote location to be loaded for the tooltip.
 * 
 * _order should be set to 'true' if the offset needs to calculate
 * a better position to keep the tooltip on the screen
 */
function tooltip(_id, _url, _caller, _type) {
	// Check if a type is provided for type.
	if (_type == null) _type = 'div';
	// Create our container
	if (created(_id) && Ajax.activeRequestCount < 1) {
		var tooltip = buildTooltip(_id, _type);
		loadContent(tooltip, _url, _caller);
	}
}