prolific.extend('tooltips', function () {
    var $tooltip = $('<div id="tooltip" style="position: absolute; left: -9999px;"></div>');

    function show (msg, offsets) {
        $tooltip.stop();
        if (!document.getElementById('tooltip')) {
            $('body').append($tooltip);
        }
        text(msg);
        $tooltip
            .css(offsets)
            .fadeTo(250, .8);
        return this;
    }

    function hide () {
        $tooltip.stop();
        $tooltip
            .fadeTo(250, 0, function () {
                $tooltip
                    .css({
                        top: '0px',
                        left: '-9999px'
                    });
            })
        return this;
    }

    function text (newText) {
        $tooltip.text(newText);
        return this;
    }

    function getPosition (el, offsets) {
        var property,
            position = {},
            handlers = {
                top: function (q) {
                    position.top = $(el).offset().top + parseInt(q) + 'px';
                },
                bottom: function (q) {
                    position.top = $(el).offset().top - parseInt(q) + 'px';;
                },
                left: function (q) {
                    position.left = $(el).offset().left + parseInt(q) + 'px';
                },
                right: function (q) {
                    position.left = $(el).offset().left - parseInt(q) + 'px';;
                }
            };
        for (property in offsets) {
            handlers[property](offsets[property]);
        }
        if (!position.top && !position.bottom) {
            position.bottom = $(el).offset().top + 'px';
        }
        if (!position.left && !position.right) {
            position.left = $(el).offset().left + 'px';
        }
        return position;
    }

    function bind (what, msg, offsets) {
        offsets = offsets || {
            bottom: '3px',
            left: '3px'
        };
        $(what).hover(function () {
            show(msg, getPosition(what, offsets));
        }, hide);
        return this;
    }

    return {
        show: show,
        hide: hide,
        text: text,
        bind: bind,
        init: function () {
            jQuery.fn.extend({
                tooltip: function (msg, offsets) {
                    $(this).each(function () {
                        bind(this, msg, offsets);
                    });
                    return this;
                }
            });
        }()
    }
});
