function init_tooltip(){
    if(!$('.tooltip').length){
        return;
    }
    $('body').append('<div id="tooltip_outer"><div id="tooltip_inner">&nbsp;</div></div>');
    
    var $tt_title, $tt_alt;
    
    var $tt = $('#tooltip_outer');
    var $tt_i = $('#tooltip_inner');
    
    $('.tooltip').hover(function()
    { 
        if($(this).attr('title')){
            $tt_title = $(this).attr('title');
            $(this).attr('title','');
        }
        
        if($(this).attr('alt')){
            $tt_alt = $(this).attr('alt');
            $(this).attr('alt','');
        }
        $tt_i.html($tt_title);
        $tt.show();
    },
    function(){
        $tt.hide();
        $tt_i.html('');
        
        if($tt_title){
            $(this).attr('title', $tt_title);
        }
        if($tt_alt){
            $(this).attr('alt', $tt_alt);
        }
    }).mousemove(function(ev) {
        
        var $ev_x = ev.pageX;
        var $ev_y = ev.pageY;
        
        var $tt_x = $tt.outerWidth();
        var $tt_y = $tt.outerHeight();
        
        var $bd_x = $('body').outerWidth();
        var $bd_y = $('body').outerHeight();
        
        $tt.css({ 
            'top' : $ev_y + $tt_y > $bd_y ? $ev_y - $tt_y : $ev_y,
            'left': $ev_x + $tt_x + 20 > $bd_x ? $ev_x - $tt_x -10 : $ev_x + 15
        });
    });
}

$(document).ready(function(){
    init_tooltip();
});
