/**
 * jQuery MV_Tooltip plugin
 * Copyright (C) MediaVault International 2009
 */
jQuery.fn.MV_tooltip = function(msg, options)
{
	
	/**
	 * Options
	 * 
	 *  onevent - When to trigger the tooltip. Avaiable
	 *  options are, {focus, hover}
	 *  
	 *  className - The CSS class to use
	 *  
	 *  position - Where to position the tooltip relative
	 *  to the object.
	 *  Posible options are, {top, top_right, bottom, bottom_right}
	 *  
	 *  width - The width of the tooltip. This defaults to
	 *  90% of the object that it is hooked to.
	 */
	var opt = jQuery.extend({

		onevent : 'focus',
		className : 'MV_tooltip',
		position : 'top_right',
		pos_top : 0,
		pos_left : 0,
		pos_top_relative : 0,
		pos_left_relative : 0
		
	}, options);
	
	var obj = $(this);
	
	/**
	 * Option, width
	 */
	if (obj.width() > 100)
	{
		opt.width = opt.width || (obj.width() - (obj.width() * 0.1) );
	}
	else if (obj.width() == 0)
	{
		opt.width = opt.width || 130;
	}
	else
	{
		opt.width = opt.width || obj.width() * 2;
	}
	
	/**
	 * Set the position of the tooltip
	 * 
	 * currentObject - The object
	 * 
	 * topLeft - Which position to return.
	 * Left or top.
	 * 
	 * tooltipHeight - The height of the
	 * tooltip.
	 */
	function tooltipPosition(currentObject, topLeft, tooltipHeight)
	{

		pos = currentObject.offset();

		switch (opt.position)
		{ 

			case "bottom":
				top_position = pos.top + (obj.height() + 10 );
				left_position = pos.left + 3;
			break;
		
			case 'bottom_right':
				top_position = pos.top + (obj.height() + 10);
				left_position = pos.left + (obj.width() - 40);
			break;

			case 'top':
				top_position = pos.top - (tooltipHeight + 22);
				left_position = pos.left + 3;
			break;

			case 'top_right':
				
				top_position = pos.top - ( tooltipHeight + 22);
				
				if (obj.width() > 150)
				{
					left_position = pos.left + (obj.width() - 150);
				}
				else
				{
					left_position = pos.left + (obj.width());
				}
				
			break;
			
		}
		
		if (topLeft == 'top')
		{

			if ( opt.pos_top != 0 )
			{
			
				return opt.pos_top;
				
			}
			else if (opt.pos_top_relative != 0)
			{
			
				return top_position + opt.pos_top_relative;
				
			}
			else
			{
			
				return top_position;
				
			}
			
		}
		else
		{

			if ( opt.pos_left != 0)
			{
			
				return opt.pos_left;
				
			}
			else if (opt.pos_left_relative != 0)
			{
				
				return left_position + opt.pos_left_relative;
				
			}
			else
			{
				
				return left_position;
				
			}
			
		}
		
	}

	/**
	 * Use the corner plugin for the right corners
	 * according to the position of the tooltip.
	 */
	function cornerPosition()
	{

		switch (opt.position)
		{

			case "bottom":

				return "bottom";
				
			break;
		
			case "bottom_right":

				return "br bl tr"
				
			break;

			case "top":

				return "top";
				
			break;

			case "top_right":

				return "tr tl br";
				
			break;
		
		}
		
	}

	/**
	 * Display the tooltip.
	 */
	function showTooltip(currentObject)
	{

		hideTooltip();

		$("body").prepend('<div id="MV_tooltip" class="' + opt.className + '"></div>');

		$("." + opt.className)
					.css("width", opt.width)
					.html( msg )
					.css("top", tooltipPosition(currentObject, 'top', $("." + opt.className).height() ) )
					.css("left", tooltipPosition(currentObject, 'left') )
					.corner( cornerPosition() )
					.fadeIn();
		
	}
	 
	/**
	 * Hide the tooltip.
	 */
	function hideTooltip()
	{

		$("#MV_tooltip")
				.hide()
				.uncorner()
				.remove();
		
	}

	return $(this).each( function () {

		switch (opt.onevent)
		{

			case 'focus':
		
				obj.focus( function() {
					
					showTooltip($(this));
		
				});
		
				obj.blur( function() {

					hideTooltip();
					
				});
		
				$(window).keydown( function() {
		
					if ( obj.val().length > 3 )
					{
						hideTooltip();
					}
					
				});

			break;
			
			case 'click':
			
				obj.click( function() {
					
					showTooltip($(this));
					
				});
				
				obj.blur( function() {

					hideTooltip();
					
				});
				
			break;

			case 'hover':

				obj.hover( function() {

					showTooltip($(this));
					
				}, function() {

					hideTooltip();
					
				});
				
			break;
			
			case 'other':

				showTooltip($(this));
				
			break;


		}
		
	});
	
}

jQuery.fn.MV_killTooltip = function()
{

	$("#MV_tooltip")
		.hide()
		.uncorner()
		.remove();	
	
}
