/*
WLDoubleClick.js
Ultralingua WebLex

Created by Ben Kazez on 8/17/08.
Copyright 2008 Ultralingua. All rights reserved.
*/

// Source: http://www.quirksmode.org/js/eventSimple.html
function WLAddEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

// Source: http://www.quirksmode.org/js/eventSimple.html
function WLRemoveEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);

    document.getElementById('WLHeader').style.display = "none";
}

function WLPageDoubleClicked(e)
{
 	if (!e) var e = window.event;	
    
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    
    var targTagName = targ.tagName.toLowerCase();
    if (targTagName != 'a' && targTagName != 'img')
    {

        // Get selection. Source: http://www.quirksmode.org/dom/range_intro.html
        var userSelection;
        if (window.getSelection) {
            userSelection = window.getSelection();
        }
        else if (document.selection) { // should come last; Opera!
            userSelection = document.selection;
            userSelection = userSelection.createRange();
        }
    
        if (userSelection) {
            var selectedText = new String(userSelection);
            if (userSelection.text)
                selectedText = new String(userSelection.text);
            
            // Get up to first non-word character (including accented chars).
            var matches = selectedText.match(/^-*([^-\d\s,.\/'"срут!?ю`*]*)/);
            if (matches.length > 0 && matches[0] != "") {
                var firstWord = matches[1];
                WLPopUpShow(e, firstWord);
            }
            else {
                if (selectedText.search(/\w+/) != -1)
                {
                    var errormsg = "WLERROR_"+selectedText;
                    WLPopUpShow(e, errormsg);
                }
            }
        }
    }
}

// Install event handler.
WLAddEventSimple(document, "dblclick", WLPageDoubleClicked);


