﻿/// <reference path="jquery126.js" />

//Variables
var sWord = "";
var clientX = 0;
var clientY = 0;
var pageX = 0;
var pageY = 0;
var offsetX = 15;
var offsetY = 15;
var puWidth = 150;
var timeout = null;
var popup = null;

function getWord(isHover, x, y) {
    try {
        //Get word to search for
        if (window.getSelection) {
            var sel = window.getSelection();
            return sel;
        }
        else if (document.selection) {
            var rng = document.selection.createRange();
            if (isHover) {
                rng.moveToPoint(x, y);
                rng.expand("word");
            }
            return rng.text;
        }    
    }
    catch(e) {
        return '';
    }
}

function actionHandler(event) {
    switch(event.type) {
        case "mousemove":
            if (document.selection) {
                if ((clientX != event.clientX) || (clientX != event.clientY)) {
                    clientX = event.clientX;
                    clientY = event.clientY;
                    pageX = event.pageX;
                    pageY = event.pageY;

                    if (timeout != null) {
                        clearTimeout(timeout);
                    }

                    sWord = getWord(true, clientX, clientY);
                    
                    timeout = setTimeout("lookupWord(sWord)", 500);
                }
            }
            break;
        case "mouseup":
            clientX = event.clientX;
            clientY = event.clientY;
            pageX = event.pageX;
            pageY = event.pageY;

            if (timeout != null) {
                clearTimeout(timeout);
            }

            sWord = getWord(false);

            lookupWord(sWord);
            break;
        case "mousedown":
            if (timeout != null) {
                clearTimeout(timeout);
            }
            $("#popup").hide();
            break;
        case "mouseout":
            if (timeout != null) {
                clearTimeout(timeout);
            }
            break;
    }
}

function prettyString(text) {
    var s = text.toString();
    s = s.replace(/[^a-zàáâãäåßçðèéêëæìíîïñòóôõöøœšþùúûüýÿžA-ZÀÁÂÃÄÅÇÐÈÉÊËÆÌÍÎÏÑÒÓÔÕÖØŒŠÞÙÚÛÜÝŽ0-9 _-]+/, '');
    s = s.replace(/^\s+/, '');
    s = s.replace(/\s+$/, '');
    return s;
}

//Function to lookup word in dictionary
function lookupWord(word) {
    $("#popup").hide();
    if (word != null) {
        word = prettyString(word);
        if (word.length > 0) {
            $.get("/Ajax.aspx", { dictionary: word }, callBack, "text");
        } 
    }
}

function callBack(data, textStatus) {
    if (data.indexOf("<error>", 0) < 0) {
        var term = data.substring(data.indexOf("<word>", 0) + 6, data.indexOf("</word>", 0));
        var description = data.substring(data.indexOf("<description>", 0) + 13, data.indexOf("</description>", 0));
        showPopup(pageX, pageY, term, description);
        window.status = "Viser resultat for '" + sWord + "'";
    }
    else window.status = "Ingen resultat for '" + sWord + "'";
}

function showPopup(x, y, term, description) {
    var left = 0;
    var top = 0;
    
    if ((x == 0) && (y == 0)) {
        left = document.body.scrollLeft + document.body.clientWidth - puWidth;
        top = document.body.scrollTop;
    } else {
        if ((document.body.scrollLeft + x + offsetX) > (document.body.scrollLeft + document.body.clientWidth - puWidth)) {
            left = document.body.scrollLeft + x - offsetX - puWidth;
        } else {
            left = document.body.scrollLeft + x + offsetX;
        }
        if ((document.body.scrollTop + y + offsetY) > (document.body.scrollTop + document.body.clientHeight - (offsetY * 3))) {
            top = document.body.scrollTop + y - (offsetY * 6);
        } else {
            top = document.body.scrollTop + y + offsetY;
        }
    }

    $("#popup").html("<p><strong>" + term + "</strong><br/>" + description + "</p>");
    $("#popup").css('left', left);
    $("#popup").css('top', top);
    $("#popup").show();
}

$(document).ready(function()
{
    if (document.cookie.indexOf('supressWordlist=true') != -1) return;
    
    $("#doc_body").append("<div id=\"popup\"></div>");
    $("#popup").css("width", "150px");
    $("#popup").hide();

    $("#doc_body").mousemove(actionHandler);
    $("#doc_body").mouseout(actionHandler);
    $("#doc_body").mousedown(actionHandler);
    $("#doc_body").mouseup(actionHandler);
});
