/*
 * brTip 1.0.0 - jQuery Plugin
 * http://plugins.jquery.com/project/brTip
 *
 * Copyright (c) 2008 Gabriel Sobrinho
 */

/**
 * brTip is a lightweight cross-browser jQuery plugin that provides a simple tooltip to explain a help dialog.
 *
 * 1) Basic usage: Select the element and apply, see:
 *
 * @example $('a[@rel=brTip]').brTip();
 * @example $('img').brTip();
 *
 *
 * 2) Options: It's full customizable with options. The options is explained here:
 *
 * fadeIn: Number or String ('') - Speed of fadeIn
 * fadeOut: Number or String ('') - Speed of fadeOut
 * toShow: Number (100) - Delay time (ms) to show
 * toHide: Number (500) - Delay time (ms) to hide
 * opacity: Number (0.8) - The brTip opacity, from 0 - 1
 * top: Number (1) - The top position of brTip is sum of mouse position with this
 * left: Number (25) - The same of top, but for the left position
 * title: String ('Help') - The title of brTip
 *
 * @example $('a[@rel=brTip]').brTip({fadeIn: 'slow', left: -50, top: 10});
 * @example $('a[@rel=brTip]').brTip({title: 'About this:'});
 *
 *
 * 3) Cross-browser: brTip has been tested in the following browsers:
 * - IE 6, 7
 * - Firefox 2
 */

(function($)
{
	/*
	 * Initialize the brTip.
	 * @param 	opts	(Object)	An optional object containing options overrides
	 * @access	Public
	 * @return	jQuery Object
	 */
	$.fn.brTip = function(opts)
	{
		// Merge default and user options.
		$.brTip.opts = $.extend({
			'fadeIn': '',
			'fadeOut': '',
			'toShow': 100,
			'toHide': 500,
			'opacity': 1,
			'top': 15,
			'left': -15,
			'title': '&nbsp;'
		}, opts);
		
		// Create the box, if not exists.
		if (!$.brTip.box)
		{
			$.brTip.box = $('<div id="brTip"><span>&nbsp;</span><p>&nbsp;</p></div>').appendTo('body');
		}
		
		// Set attributes.
		$.brTip.box.find('span:first').html($.brTip.opts.title);
		$.brTip.box.css('opacity', $.brTip.opts.opacity);
		
		// Init the plugin with internal function.
		return $.brTip(this);
	};
	
	/*
	 * Set the attributes of all matched elements.
	 * @param	elem	(jQuery Object)		The element matched by user
	 * @access	Private
	 * @return	jQuery Object
	 */
	$.brTip = function(elem)
	{
		return elem.each(function()
		{
			// Self is link for actual element.
			var self = $(this);
			
			// Select the text, if necessary.
			if (!self.txt)
			{
				self.txt = self.attr('title');
				//self.attr('title', '');
			}
			
			// Events of element.
			self
				// Show the brTip.
				.mouseover(function()
				{
					$.brTip.show(self.txt);
				})
				
				// Hide the tooltip.
				.mouseout(function()
				{
					$.brTip.hide();
				})
				
				// Accessibility.
				.focus(function()
				{
					// Set position based on element position.
					var pos = self.offset();
					$.brTip.setPos(pos.top + (self.width() / 2), pos.left + (self.height() / 2));
					
					self.trigger('mouseover');
				})
				
				// Accessibility.
				.blur(function()
				{
					self.trigger('mouseout');
				})
				
				// Set position based on mouse position.
				.mousemove(function(e)
				{
					$.brTip.setPos(e.pageY, e.pageX);
				});
		});
	};
	
	/*
	 * Clear the timeouts to show/hide the tooltip.
	 * @access	Private
	 * @return	Void
	 */
	$.brTip.clearTimes = function()
	{
		clearTimeout($.brTip.delayToShow);
		clearTimeout($.brTip.delayToHide);
	};
	
	/*
	 * Show the tooltip based on time to show.
	 * @access	Private
	 * @return	Void
	 */
	$.brTip.show = function(txt)
	{
		// Clear the times.
		$.brTip.clearTimes();
		
		// Set content.
		$.brTip.box.find('p:first').html(txt);
		
		// Show the box.
		$.brTip.delayToShow = setTimeout(function()
		{
			$.brTip.box.fadeIn($.brTip.opts.fadeIn);
		}, $.brTip.opts.toShow);
	};
	
	/*
	 * Hide the tooltip based on time to hide.
	 * @access	Private
	 * @return	Void
	 */
	$.brTip.hide = function()
	{
		// Clear the times.
		$.brTip.clearTimes();
		
		// Hide the box.
		$.brTip.delayToHide = setTimeout(function()
		{
			$.brTip.box.fadeOut($.brTip.opts.fadeOut);
		}, $.brTip.opts.toHide);
	};
	
	/*
	 * Set the position of box.
	 * @access	Private
	 * @return	Void
	 */
	$.brTip.setPos = function(top, left)
	{
		$.brTip.box.css({
			'top': top + $.brTip.opts.top,
			'left': left + $.brTip.opts.left
		});
	};
}(jQuery));