$(document).ready(function() {

	$('a[rel=tooltip]').mouseover(function(e) {
		var tip = $(this).attr('title');
		$(this).attr('title','');
    $(this).append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div></div>');
	}).mousemove(function(e) {
        
        tip2 = $(this).find('#tooltip');
        var mousex = e.pageX + 10;
        var mousey = e.pageY + 10;
        var tipWidth = tip2.width();
        var tipHeight = tip2.height();
 
        var tipVisX = $(window).width() - (mousex + tipWidth);
        var tipVisY = $(window).height() - (mousey + tipHeight);
 
        if ( tipVisX < 20 ) {
            mousex = e.pageX - tipWidth - 10;
        } if ( tipVisY < 20 ) {
            mousey = e.pageY - tipHeight - 10;
        }
        
    $('#tooltip').css('top', mousey );
		$('#tooltip').css('left', mousex );
    		
	}).mouseout(function() {

		$(this).attr('title',$('.tipBody').html());

		$(this).children('div#tooltip').remove();		
	});
});
