/*
 * Autor: Gabriel Sobrinho <gabriel.sobrinho@gmail.com>
 * Aviso: Proibido cópia total ou parcial!
 */

(function($)
{
	// Função principal.
	$.fn.brTip = function(estilo)
	{
		// Criamos o elemento que exibirá o brTip.
		$('#brTip').remove();
		$('body').append('<div id="brTip"><span>Atenção</span><p><strong>&nbsp;</strong></p></div>');
		
		
		// Selecionamos o elemento.
		$('#brTip').addClass(estilo + '-hint');
		$('#brTip').find('span').addClass(estilo + '-bg');
		// Aplicamos o CSS.
		$('#brTip').css({
			'background': '#FFF',
			'display': 'none',
			'position': 'absolute',
			'width': '200px'
		});
		$('#brTip').find('span').css({
			'color': '#FFF',
			'display': 'block',
			'padding': '3px',
			'text-align': 'center'
		});
		$('#brTip').find('p').css({
			'padding': '5px',
			'color': '#000',
			'text-align': 'center'
		});
		
		// Aplicamos uma opacidade.
		$('#brTip').fadeTo(0, 0.8);
		
		// Aplicamos as funções no elemento.
		return this.each(function()
		{
			// Selecionamos o elemento.
			var elemento = $(this);
			
			// Negamos o link.
			elemento.click(function()
			{
				return false;
			})
			
			// Exibimos o brTip.
			.mouseover(function()
			{
				// Limpamos os tempos.
				clearTimeout($('#brTip').ocultar);
				clearTimeout($('#brTip').exibir);
				
				// Pegamos o texto e colocamos ele no atributo rel.
				if(texto = elemento.attr('title'))
				{
					elemento.attr('rel', texto).removeAttr('title');
				}
				else
				{
					texto = elemento.attr('rel');
				}
				
				// Posicionamos o brTip.
				offset = elemento.offset();
				$('#brTip').css({
					'top': offset.top-2,
					'left': offset.left+25
				});
				
				// Aplicamos o texto no brTip.
				$('#brTip').find('strong').html(texto);
				
				// Definimos um tempo para exibir.
				$('#brTip').exibir = setTimeout(function()
				{
					$('#brTip').fadeIn();
				}
				, 100);
			})
			
			// Ocultamos o brTip.
			.mouseout(function()
			{
				// Limpamos os tempos.
				clearTimeout($('#brTip').ocultar);
				clearTimeout($('#brTip').exibir);
				
				// Definimos um tempo para ocultar.
				$('#brTip').ocultar = setTimeout(function()
				{
					$('#brTip').fadeOut();
				}
				, 500);
			})
			
			// Acessibilidade.
			.focus(function(){elemento.trigger('mouseover');})
			.blur(function(){elemento.trigger('mouseout');})
			
			// Posicionamos de acordo com o mouse.
			.mousemove(function(e)
			{
				$('#brTip').css({
					'top': e.pageY-3,
					'left': e.pageX+15
				});
			});
		});
	}
}(jQuery));