/*
@author: 		SleX / http://www.slex.com.br
#filename:		jquery.title.plus.js
@license: 		GNU General Public License V3 http://www.gnu.org/licenses/gpl.html
@version: 		0.1
@description:	lib para carregar um div flutuante que vai servir
@changelog
  0.1
	- inicializacao do div flutuante
	- busca dos parametros passados
	- adicionou ajustado deslocamento horizontal para o título que caem fora dos limites
	- Enhanced horizontal adjustment to reposition image if it is configured
*/

(function($){
	$.fn.registerTitle = function(options){
		var defaults = {
			idDiv: 'registerTitleDiv',
			classDiv: 'registerTitle',
			widthDiv: '60px',
			tagText: 'title'
		}
		//pega os atributos dos padroes se nao tiver passado
		if (options) {
			var options = $.extend(defaults, options);
		} else {
			var options = defaults;
		}		
		function createDivFlu(param){
			var dir = $('<div></div>')
				.css({
					position: 'absolute',
					border: '2px solid #000000',
					color: '#000000',
					zIndex: '50',
					width: param.widthDiv,
					left: param.left,
					top: param.top
				})
				.attr('id',param.idDiv)
				.addClass(options.classDiv)
				.html(param.html);
			$('body').append(dir);
			return dir;
		}
		return this.each(function(){
			var title = $(this).attr(options.tagText);
			if(title){
				$(this).attr(options.tagText+'p',title);
				$(this).attr(options.tagText,'');
				$(this).mousemove(function(event){
					var html = $(this).attr(options.tagText+'p');
					$('#'+options.idDiv).remove();
					var dir = createDivFlu({
						top:(event.pageY+5)+'px',
						left:(event.pageX+5)+'px',
						html: html,
						idDiv:options.idDiv,
						widthDiv:options.widthDiv
					});
					//Se tiver pra fora da pagina ajusta 
					var t = parseInt(dir.css('height')) + parseInt(dir.css('top'));
					var wt = ($(window).height() - 5) + $(document).scrollTop();
					if(t>wt){
						tn = (wt - parseInt(dir.css('height')));
						dir.css('top',tn+'px');
					}
					//Se tiver pra fora da pagina ajusta 
					var l = parseInt(dir.css('width')) + parseInt(dir.css('left'));
					var wl = ($(window).width()-5); //$(document).scrollLeft()
					if(l>wl){ 
						ln = (event.pageX-parseInt(dir.css('width'))-5);
						dir.css('left',ln+'px');
					}
				});
				$(this).mouseout(function(){
					$('#'+options.idDiv).remove();
				});
			}
		});
	}
})(jQuery);	

