    var popupMenuAfterClick;
    
    document.onmousedown = function( e ) 
    {
        e = e || event;
        var target = e.target || e.srcElement;
        if( target.parentNode != document.getElementById('popupmenudiv') )
        {
            hidePopupMenu();
        }
    }
    
    function popupMenuClicked ()
    {
        hidePopupMenu();
        if (popupMenuAfterClick && typeof popupMenuAfterClick == 'function')
            popupMenuAfterClick(parseInt(this.Id.substring(13),10));
    }
    
    function showPopupMenu(params)
    {
        var divX;
        var divY;
        var newdiv;
        var pudiv;
        
        hidePopupMenu();
        if (!params || !params.x || !params.y || !params.resultfunc || !params.items || !params.items.length ) return false;
        
        popupMenuAfterClick = params.resultfunc;
        
        pudiv = document.getElementById('popupmenudiv');
        
        if (!pudiv)
        {
            pudiv = document.createElement('div');
            pudiv.id = 'popupmenudiv';
            pudiv.className = 'popupmenu';
            pudiv.style.position = 'absolute';
            pudiv.style.display = 'none';
            pudiv.style.zIndex = '200';
            document.body.appendChild(pudiv);
        }
        
        while (pudiv.hasChildNodes())
        {
            pudiv.removeChild(pudiv.lastChild);
        }
        
        if (params.title)
        {
            newdiv = document.createElement('div');
            newdiv.innerHTML = params.title;
            newdiv.className = 'popupmenutitle';
            pudiv.appendChild(newdiv);
        }
        
        for (var i = 0; i < params.items.length; i++)
        {
            newdiv = document.createElement('div');
            newdiv.Id = 'popupmenuitem' + i;
            newdiv.innerHTML = params.items[i];
            newdiv.className = (i == 0) ? 'popupmenufirstitem' : 'popupmenuitem';
            newdiv.onclick = popupMenuClicked;
            newdiv.onmouseover = (i == 0) ? function () {this.className = 'popupmenufirsthighlight'} : function () {this.className = 'popupmenuhighlight'};
            newdiv.onmouseout = (i == 0) ? function () {this.className = 'popupmenufirstitem'} : function () {this.className = 'popupmenuitem'};
            pudiv.appendChild(newdiv);
        }
        pudiv.style.display = 'block';
        divX = params.x + pudiv.offsetWidth + 1 < getClWidth() ? params.x + 1 : params.x - pudiv.offsetWidth - 1;
        divY = params.y + pudiv.offsetHeight + 1 < getClHeight() ? params.y + 1 : params.y - pudiv.offsetHeight - 1;
        pudiv.style.top = divY + 'px';
        pudiv.style.left = divX + 'px';
        
        
    }
    
    function testpopupclicked(num)
    {
        alert ('Выбран пункт ' + num);
    }
    
    function shTreePopup(target, e)
    {
        showPopupMenu({
            x: e.clientX,
            y: e.clientY,
            items: ['Только один элемент','Все элементы'],
            title: target.src ? 'Меню картинки:' : 'Тестовое меню:',
            resultfunc: testpopupclicked
        });
    }
    
    function hidePopupMenu()
    {
        var pudiv = document.getElementById('popupmenudiv');
        if (pudiv) pudiv.style.display = 'none';
    }