﻿var clientX = 0;
var clientY = 0;
var pageX = 0;
var pageY = 0;
var lastX = 0;
var lastY = 0;
var timeout = null;

$(document).ready(function () {
    if (document.cookie.indexOf('showWordlist=true') == -1) return;

    $("#fw-content").mousemove(function (e) {
        if (document.selection) {
            if ((clientX != e.clientX) || (clientX != event.clientY)) {
                clientX = e.clientX;
                clientY = e.clientY;
                pageX = e.pageX;
                pageY = e.pageY;

                if (timeout != null) { clearTimeout(timeout); }

                timeout = setTimeout("lookupWord(true)", 500);
            }
            else {
                $('#popup').fadeOut('fast');
            }
        } else {
            $('#popup').fadeOut('fast');
        }
    });
    $("#fw-content").mouseup(function (e) {
        clientX = e.clientX;
        clientY = e.clientY;
        pageX = e.pageX;
        pageY = e.pageY;

        if (timeout != null) { clearTimeout(timeout); }

        lookupWord(false);
    });
    $("#fw-content").mousedown(function (e) {
        if (timeout != null) { clearTimeout(timeout); }
        $('#popup').fadeOut('fast');
    });
    $("#fw-content").mouseout(function (e) {
        if (timeout != null) { clearTimeout(timeout); }
    });
    $("#fw-content").scroll(function () { $('#popup').fadeOut('fast'); });
});

function lookupWord(hover) {
    var word;

    try {
        if (window.getSelection) { word = window.getSelection().toString(); }
        else if (document.selection) {
            if (hover) {
                var range = document.selection.createRange();
                range.moveToPoint(clientX, clientY);
                range.expand("word");
                word = range.text;
            }
            else {
                word = document.selection.createRange().text;
            }
        }

        if (word.length > 25) {
            var l = word.indexOf(" ", 25);
            if (l > 25) {
                word = word.substring(0, l);
            } else {
                word = word.substring(0, 25);
            }
        }

        word = jQuery.trim(word);
        word = encodeURI(word);
    }
    catch (n) {
        word = null;
    }

    if (word) {
        lastX = pageX;
        lastY = pageY;
        if ($("#popup").length == 0) $("#doc_body").append('<div id="popup"></div>');
        $("#popup").load("/Dictionary.aspx?term=" + word, function (response, status, xhr) {
            if (status != "error" && $("#popup").html().length > 0) {
                var x = lastX - $(window).scrollLeft();
                var y = lastY - $(window).scrollTop();
                if (x + $('#popup').width() > $('#fw-content').outerWidth()) x -= (20 + $('#popup').width()); else x += 20;
                if (y + $('#popup').height() > $(window).height()) y -= (20 + $('#popup').height()); else y += 20;
                $("#popup").css('left', x);
                $("#popup").css('top', y);
                $('#popup').fadeIn('fast');
            }
            else {
                $('#popup').fadeOut('fast');
            }
        });
    } else {
        $('#popup').fadeOut('fast');
    }
}

