/*dependencies
    <dependencies>
        <item note = "relies on check browser" domain = "sabreindustriesinc.com" name = "CheckBrowser.js"></item>
    </dependencies>
dependencies*/

/*Implementation: to implement this functionality, just link to this script file anywhere within the body tag.*/

document.body.onmouseup = new Function("HandleLookupClick()");
var Lookup_Selection, Lookup_SelectionText, Lookup_SelectionButton, Lookup_NewRange;

function HandleLookupClick() {
    if (Lookup_SelectionButton) { CleanUpLookup(); }

    Lookup_Selection = GetLookup_Selection();
    Lookup_SelectionText = (Lookup_Selection && Lookup_Selection.toString());
    if (Lookup_SelectionText) {
        window.setTimeout(InsertLookupButton, 0);
    }
}

function GetLookup_Selection() {
    if (window.getSelection) { return window.getSelection(); }
    else if (document.getSelection) { return document.getSelection(); }
    else {
        try {
            var sel = document.selection && document.selection.createRange();
            sel.toString = function() { return this.text };
            return sel;
        } catch (x) { return false; }
    }

}

function InsertLookupButton() {

    var btnID = "btnLookupDefinition";
    Lookup_SelectionButton = document.createElement("span");
    Lookup_SelectionButton.setAttribute("id", btnID);
    Lookup_SelectionButton.setAttribute("title", "Lookup \"" + Lookup_SelectionText + "\"");
    Lookup_SelectionButton.style.position = "absolute";
    Lookup_SelectionButton.style.cursor = "pointer";

    Lookup_SelectionButton.innerHTML = "<img src = \"http://www.SabreIndustriesInc.com/img/sharedimg/icons/helpicon.png\" />";

    if (bIsIE) {
        var tmp = document.createElement("div");
        tmp.appendChild(Lookup_SelectionButton);
        Lookup_NewRange = Lookup_Selection.duplicate();
        Lookup_NewRange.setEndPoint("StartToEnd", Lookup_Selection);
        Lookup_NewRange.pasteHTML(tmp.innerHTML);
        Lookup_SelectionButton = document.getElementById(btnID);
    }
    else {
        var range = Lookup_Selection.getRangeAt(0);
        Lookup_NewRange = document.createRange();
        Lookup_NewRange.setStart(Lookup_Selection.focusNode, range.endOffset);
        Lookup_NewRange.insertNode(Lookup_SelectionButton);
    }

    Lookup_SelectionButton.onmouseup = new Function("ExportLookup_Selection()");

}

function CleanUpLookup() {

    Lookup_Selection = null;
    Lookup_NewRange && Lookup_NewRange.pasteHTML && Lookup_NewRange.pasteHTML('');
    Lookup_NewRange = null;
    Lookup_SelectionButton.parentNode.removeChild(Lookup_SelectionButton);
    Lookup_SelectionButton = null;
    Lookup_SelectionText = "";

}

function ExportLookup_Selection() {

    var url = "http://www.answers.com/topic/" + encodeURIComponent(Lookup_SelectionText);
    var newwin = window.open(url, "answersdotcom", "height=450,width=820,location=false,menubar=false,toolbar=false,status=false,resizable, scrollbars");
    if (newwin) { newwin.focus(); }
}

