﻿var baseUrl = 'http://dictionary.greenwood.com/';
			
// Remember to add <body ondblclick="getHighlightedTextMozilla()"> for Mozilla.
							
// Even works on Safari 1.3, except the test for empty string.
function getHighlightedText()
{
    var mySelection = '';
    if(window.getSelection)
        mySelection = window.getSelection()+'';
    else if(document.getSelection)
        mySelection = document.getSelection()+'';
    else if(document.selection)
        mySelection = document.selection.createRange().text+'';
    else return;
    // Add +'' concatenates an empty string and forces the object to become a string.
    
    //if(mySelection != '')	alert(mySelection);
    if(mySelection != '') openDictionary(mySelection);
    else openHelp();
}

function openHelp()
{
    var theUrl = baseUrl + '/help/';
    dictWindow = window.open(theUrl,'dictionary','left=10,top=10,width=700,height=350,location=no,toolbar=no,status=yes,scrollbars=yes,resizable=yes,menubar=no');
    dictWindow.focus();
}

function openDictionary(selectedText)
{
    selectedText = selectedText.replace(/\W+$/,""); // Replace last non-word character with nothing: ?, ", etc.
    selectedText = selectedText.replace(/^\W+/,""); // Replace first non-word character with nothing: ?, ", etc.
    if(encodeURIComponent) selectedText = encodeURIComponent(selectedText);
    //else alert("fail");
    
    var theUrl = baseUrl + 'results/?q=' + selectedText;
    //if (window.dictWindow && !window.dictWindow.closed) 
    //    dictWindow.close();
    dictWindow = window.open(theUrl,'dictionary','left=10,top=10,width=700,height=350,location=no,toolbar=no,status=yes,scrollbars=yes,resizable=yes,menubar=no');
    dictWindow.focus();
    //window.location = theUrl;
}

function getHighlightedTextMozilla()
{
    // Prevents duplicate popups with Mozilla body ondblclick in the body tag.
    if(document.ondblclick) return;
    else getHighlightedText();
}

// IE only. For Mozilla add <body ondblclick="getHighlightedTextMozilla()">
document.ondblclick=getHighlightedText 
