var lwdtext = "";
var lwdviewerpop;
var timeid;
var sURL = document.location.href;
var idmUrl = 'http://pewebdic2.cw.idm.fr/display/wapopup.html';
// Dimensions of the dictionary popup
var iPopupWidth = 637;
var iPopupHeight = 475;
var nav4 = window.Event ? true : false;
if (nav4) {
    document.captureEvents(Event.DBLCLICK);
}
document.ondblclick = lwdGetActiveText;

function asGetActive(e){
    var txt = '';
    var foundIn = '';
    if (window.getSelection)
    {
        txt = window.getSelection();
        foundIn = 'window.getSelection()';
    }
    else if (document.getSelection)
    {
        txt = document.getSelection();
        foundIn = 'document.getSelection()';
    }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
        foundIn = 'document.selection.createRange()';
    }
    else return;
    lwdtext = txt;

    if (lwdtext != null && lwdtext != "")
        lwdShowMain(idmUrl, lwdtext);
    
    return true;

}
// Called when double-clicking, displays the LDOCE popup
function lwdGetActiveText(e) { 
    asGetActive();
}

// Called from the LDOCE search forms 
function doSearch(lwdtext) {
    if (lwdtext != null && lwdtext != "")
        lwdShowMain(idmUrl, lwdtext);
}

function lwdShowMain(url, word) 
{
    var sURL = ''
    var sFeatures = 'dependent=no,z-lock=yes,titlebar=1,location=no,toolbar=yes,' +
        'directories=no,menubar=0,resizable=1,scrollbars=1,status=1,' +
        'width=' + iPopupWidth +',height=' + iPopupHeight;
    var winX = (screen.width/2-302);
    var winY = (screen.width/2-337);
    // Create centered window
    sFeatures +=  ",screenX=" + winX + ",left=" + winX + ",screenY=" + winY + ",top=" + winY ;
    if (lwdviewerpop != null) {
        try    {
            lwdviewerpop.close();
        }
        catch (e) {}
        lwdviewerpop = null;

    }

    if (lwdviewerpop == null) {
        lwdviewerpop = window.open(url + "?search_str=" + word, 'ldoceonline', sFeatures);
    }

    //check if still null
    if (lwdviewerpop == null) {
        alert('We are sorry, popups seem to be disabled. Please enable popups and try again.') 
    }
    else {
        lwdviewerpop.focus();
    }
}
