
$.fn.tooltip = function(msg) {
	
	if ( (typeof msg != 'undefined') ) {
		
		$(this).attr('tooltip',msg);
		
	}
	//var msg =  ? msg : $(this).attr('tooltip');
	
	$(this).mouseover(function(event) {
							   
		$('#tooltip').html($(this).attr('tooltip'));
		
		posX = event.pageX + 10;
		posY = event.pageY + 10;
		
		//alert($(document).width());
		
		//Se posição X estiver no limite do documento, 
		//exibe o tooltip na posição inversa do cursor
		
		if ( ($(document).width() - posX) < 400 ) {
			posX = (posX- 20) - ($('#tooltip').width());
		}
		
		$('#tooltip').css({
			left: posX + 'px',
			top: posY + 'px'
		});
		
		$('#tooltip').show();

	});
	
	$(this).mousemove(function(event) {
							   
		$('#tooltip').html($(this).attr('tooltip'));
		
		posX = event.pageX + 10;
		posY = event.pageY + 10;
		
		//Se posição X estiver no limite do documento, 
		//exibe o tooltip na posição inversa do cursor
		
		if ( ($(document).width() - posX) < 400 ) {
			posX = (posX- 20) - ($('#tooltip').width());
		}
		
		$('#tooltip').css({
			left: posX + 'px',
			top: posY + 'px'
		});
		
		$('#tooltip').show();
							   
	});
	
	$(this).mouseout(function() {
							   
		$('#tooltip').hide();
							   
	});

	$(this).click(function() {
							   
		$('#tooltip').hide();
		$(this).unbind('mousemove');
							   
	});

	$(this).dblclick(function() {
							   
		$('#tooltip').hide();
		$(this).unbind('mousemove');
							   
	});

	$(this).keyup(function() {
							   
		$('#tooltip').hide();
		$(this).unbind('mousemove');
							   
	});

};