﻿(function($) {
    $.fn.timeoutHover = function (fnOver, fnOut, timeout) {
        var isOver = false;
        
        var modifiedFnOver = function(e) {
            var thisObj = this;
            isOver = true;
            fnOver.call(thisObj, e);
        };
        
        var modifiedFnOut = function(e) {
            isOver = false;
            var thisObj = this;
            var invokeCallback = function() {
                if(!isOver) {
                    fnOut.call(thisObj, e);
                }
            }
            setTimeout(invokeCallback, timeout);
        };
        
        return $(this).hover(modifiedFnOver, modifiedFnOut);
    };
})(jQuery);

$(function() {
    $('a.glossary').timeoutHover(
        function(e) {
            if ($('#glossaryPopup').length == 0) {
                $('body').append('<div id="glossaryPopup" class="glossaryPopup"></div>');
            }
            if ($('#glossaryPopup').data('obj') == this) return;

            $('#glossaryPopup').empty()
                               .append($(this).html() + ':<br/>')
                               .append($(this).attr('title'))
                               .append($(this).attr('definition'))
                               .data('obj', this)
                               .css({
                                   'top': e.pageY + 10,
                                   'left': e.pageX
                               });
        },
        function(e) {
            $('#glossaryPopup').remove();
        },
        500);
    });
