﻿jQuery.fn.tooltip = function(options) {

    var defaults = { xo: 0, yo: 0, close: true };

    var options = $.extend(defaults, options);

    var tipHtml = '<div id="tooltip">' +
        '<div class="arrow"></div>' +
        '<div class="content"><p></p></div>';

    if (close)
        tipHtml += '<div class="close"><a href="#" title="close"></a></div>';

    tipHtml += '</div>';    

    $('body').prepend(tipHtml);

    $('#tooltip .close a').click(function() { $('#tooltip').hide() });

    return $(this).each(function() {
        var tooltip = $('#tooltip');
        var content = $('#tooltip .content p');
        var contentHtml = this.title;
        this.title = '';

        var target = $(this);

        if (target.hasClass('nofade'))
            var nofade = true;

        if (target.hasClass('icon')) {
            target.after('<span class="tticon">&nbsp;</span>');
            target = target.next();
        }

        var offset = target.offset();
        var top = offset.top + options.yo;
        var left = offset.left + target.width() + options.xo;

        target.hover(function() {
            content.html(contentHtml);
            tooltip.css({ top: top + "px", left: left + "px" });
            tooltip.fadeIn('fast');
        }, function() {
            if (!nofade)
                tooltip.hide();
        });
    });
};
