;(function($) {
 
$.fn.tooltip = function(options) {
 
var defaults = {
    distance: 10,
    time: 250,
    delay: 300,
    delay_show: 300,
    x: 0,
    y: 0,
    use_content: false
    // content: 'your content here'
    // tooltip: selector or element to show when this is hovered over
};
    
var s = $.extend(defaults, options);
 
return this.each(function() {
    var hideDelayTimer = false,
        showDelayTimer = false,
        beingShown = false,
        shown = false;
    
    $(this).mouseover(function() {
        if (hideDelayTimer)
            clearTimeout(hideDelayTimer);

        if (beingShown || shown) {
            return;
        } else {
        
            if (showDelayTimer)
                clearTimeout(showDelayTimer);            
            
            var $this = this;
            showDelayTimer = setTimeout(function() {

                beingShown = true;

                showDelayTimer = false;

                var doneMouseOver = function() {
                    beingShown = false;
                    shown = true
                };
                
                var offset = $($this).offset();
                 
                if (undefined != s['content'] && s['content'] && s['content'].length)
                    $(s.tooltip + '>p').html(s['content']);
                else if ('title' == s.use_content)
                    $(s.tooltip + '>p').html($this.title);
                else if ('alt' == s.use_content)
                    $(s.tooltip + '>p').html($this.alt);                

                $(s.tooltip)
                    .show()                
                    .css({position: "absolute",
                          top: s.y,
                          left: s.x})
                    .animate({top: '-='+s.distance+'px', opacity:1}, s.time, 'swing', doneMouseOver);

            }, s.delay_show);
        }
        
    }).mouseout(function() {
        if (hideDelayTimer)
            clearTimeout(hideDelayTimer);
         if (showDelayTimer)
            clearTimeout(showDelayTimer);

        var doneMouseOut = function() {
            shown = false;
            $(s.tooltip).hide();
        };
        
        hideDelayTimer = setTimeout(function() {
            hideDelayTimer = false;
            $(s.tooltip).animate({top:'-='+s.distance+'px', opacity: 0}, s.time, 'swing', doneMouseOut)
        }, s.delay);
 
    });
});
 
};
 
})(jQuery);

