String.prototype.trim = function()
{
    return this.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

var aktiv = false;
var sel = false;

function getSel()
{
	var txt = '';
	if (window.getSelection)
	{
		txt = window.getSelection();
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
	}
	else return;
    return txt;
}

function removeSel()
{
    if (window.getSelection)
    {
        window.getSelection().removeAllRanges();
    }
    else
    {
        document.selection.empty();
    }
    sel = false;
}

function mDown(e)
{
    b = (document.all) ? event.button : e.which;
    if (b==2||b==3)
    {
        if (getSel()!="" && !sel) 
        {
            sel = true;
            str = getSel().toString().trim();
            if (str.split(" ").length > 1)
            { 
                alert("Bitte immer nur ein Wort markieren.");
            }
            else
            {            
                x = (document.all) ? event.clientX : e.clientX;
                y = (document.all) ? event.clientY : e.clientY;
                with($('menu'))
                {         
                    innerHTML = "";
                    style.display = "none";
                    style.left = x+"px";
                    style.top = y+"px";
                }
                sendIt("t="+escape(str));
            }
        }
    }
}

function sendIt(q)
{
    new Ajax.Request(
        "dbQuery.php?"+q+"&"+new Date().getTime(),
        {
            method:'get',
            onSuccess:function(r)
            {
                if (r.responseText.match(/form/)) { aktiv = true; }
                $("menu").innerHTML = r.responseText;
                $("menu").style.display = "block";
            }
        }
    );  
}

function mUp()
{
    if (!aktiv)
    {
        if (sel) removeSel();
        document.getElementById("menu").style.display = 'none';
    }
}

function stopContext()
{
    return false;
}

window.onload = function()
{
    if (!window.opera)
    { 
        new Draggable("menu");
        document.onmousedown=mDown;
        document.onmouseup=mUp;
        document.oncontextmenu=stopContext;
    }
    else alert("Leider unter Opera nicht kompatibel");
}