/****************************************\

	Part of GEO (JavaScript)

	Copyright (c) 2003-2006 by Landgraph

	               http://www.landgraph.ru/
\****************************************/
var landMenus=new Array();
var landOwner=null;
var landMenuFlag=false;
var landMenuContext=false;
var landStrict;

function CreateMenu(){
    document.writeln('<table id="menu" style="position:absolute; display: none; background-color: #f5f5f5; border-bottom: 1px solid #C6C8CA; border-top: 1px solid #C6C8CA; border-left: 1px solid #C6C8CA; border-right: 1px solid #C6C8CA;"></table>');
    var menu=document.getElementById('menu');
    menu.cellPadding=0;
    menu.cellSpacing=0;
    landStrict=document.body.clientWidth==document.body.offsetWidth;
}

function MakeMenu(data){
    if(data==null)return;
    var menu=document.getElementById("menu");
    while(menu.rows.length>0)
        menu.deleteRow(0);
    for(i=0;i<data.length;i++){
        var tr=menu.insertRow(-1);
        var td=tr.insertCell(-1);
        if(data[i][0]=='-'){
            var td=tr.insertCell(-1);
            td.style.paddingRight='2px';
            td.style.borderLeft='1px solid #E1E3E5';
            var hr=document.createElement('HR');
            hr.size='1';
            hr.color='#E1E3E5';
            td.appendChild(hr);
            continue;
        }
        if(data[i][2]!=''){
            var img=document.createElement('IMG');
            img.src=data[i][2];
            td.style.paddingLeft='3px';
            td.style.paddingRight='5px';
            td.appendChild(img);
            img.width=16;
            img.height=16;
        }
        var td=tr.insertCell(-1);
        td.style.borderLeft='1px solid #E1E3E5';
        td.style.paddingTop='1px';
        td.style.paddingBottom='1px';
        td.style.paddingRight='2px';
        var a=document.createElement('A');
        a.href=data[i][1];
        a.title=data[i][0];
        a.appendChild(document.createTextNode(data[i][0]));
        a.style.color='Black';
        a.style.display='block';
        a.style.fontFamily='Verdana, Arial';
        a.style.fontSize='8pt';
        a.style.textDecoration='none';
        a.style.borderWidth='1px';
        a.style.borderStyle='solid';
        a.style.borderColor='#f5f5f5';
        a.style.paddingRight='4px';
        a.style.paddingLeft='3px';
        if(!landStrict) {
            a.style.paddingTop='2px';
            a.style.paddingBottom='2px';
        }
        a.onmouseover=MenuItemOver;
        a.onmouseout=MenuItemOut;
        td.appendChild(a);
    }
}

function MenuItemOver(e){
    if(ie)
        var obj=window.event.srcElement;
    else
        var obj=e.target;
    obj.style.backgroundImage='url("'+engineURL+'include/images/a_item_bg.gif")';
    obj.style.borderColor='#AAD0D9';
}

function MenuItemOut(e){
    if(ie) var obj=window.event.srcElement;
    else var obj=e.target;obj.style.backgroundImage='';
    obj.style.borderColor='#f5f5f5';
}

function InitMenu(obj,menuid){
    if(document.getElementById('menu')==null)
        CreateMenu();
    if(obj==null) return false;
    obj.onmouseup=new Function("e","landMenuContext=false; ShowMenu(e); return true;");
    obj.setAttribute('landMenu',menuid);
    return true;
}

function InitContextMenu(obj,menuid){
    if(window.navigator.appName.indexOf('Op')>-1)
        return InitMenu(obj,menuid);
    if(document.getElementById('menu')==null)
        CreateMenu();
    if(obj==null) return false;
    obj.oncontextmenu=new Function("e","landMenuContext=true; ShowMenu(e); return false;");
    obj.setAttribute('landMenu',menuid);
    return true;
}

function ShowMenu(e){
    var scrollY=(window.scrollY)?window.scrollY :document.documentElement.scrollTop ?document.documentElement.scrollTop :document.body.scrollTop;
    var scrollX=(window.scrollX)?window.scrollX :document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft;
    var wnd_width=document.body.clientWidth+scrollX;
    var wnd_height=document.body.clientHeight+scrollY;

    if(ie){
        var obj=window.event.srcElement;
        var left=window.event.clientX+scrollX;
        var top=window.event.clientY+scrollY;
        var btn=window.event.button;
        if(landStrict) {
            wnd_width  += strToInt(document.body.currentStyle.marginRight)+strToInt(document.body.currentStyle.marginLeft);
            wnd_height += strToInt(document.body.currentStyle.marginTop)+strToInt(document.body.currentStyle.marginBottom);
        }
        window.event.cancelBubble=true;
        if(window.event.ctrlKey) return true;
    } else {
        var obj=e.target;
        var left=e.clientX+scrollX;
        var top=e.clientY+scrollY;
        if(netscape&&!safari)top+=2;
        var btn=e.button;
        var stl = document.defaultView.getComputedStyle(document.body, null);
        if(landStrict) {
            wnd_width  += strToInt(stl.getPropertyValue('margin-right'))+strToInt(stl.getPropertyValue('margin-left'));
            wnd_height += strToInt(stl.getPropertyValue('margin-top'))+strToInt(stl.getPropertyValue('margin-bottom'));
        }
        e.cancelBubble=true;
        if(e.ctrlKey) return true;
    }
    if(!landMenuContext&&btn!=2) return true;
    if(netscape&&!safari)landMenuFlag=true;
    while(!obj.getAttribute('landMenu')&&obj.parentNode!=null)
        obj=obj.parentNode;
    if(!obj.getAttribute('landMenu')) return true;
    var menu=document.getElementById('menu');
    if(!menu.getAttribute('landMenu')||menu.getAttribute('landMenu')!=obj.getAttribute('landMenu'))
        MakeMenu(landMenus[obj.getAttribute('landMenu')]);
    menu.setAttribute('landMenu',obj.getAttribute('landMenu'));
    menu.style.display='';
    var ow=wnd_width-menu.offsetWidth;
    if(!landMenuContext){
        menu.style.left='';
        menu.style.right=(wnd_width-left)+'px';
        if(left<menu.offsetWidth){
            menu.style.left=0+'px';
            menu.style.right='';
        }
        if(top+menu.offsetHeight<wnd_height)
            menu.style.top=top+'px';
        else
            menu.style.top=(wnd_height-menu.offsetHeight)+'px';
    } else {
        if(left+menu.offsetWidth<wnd_width)
            menu.style.left=left+'px';
        else
            menu.style.left=(wnd_width-menu.offsetWidth)+'px';
        if(top+menu.offsetHeight<wnd_height)
            menu.style.top=top+'px';
        else
            menu.style.top=(wnd_height-menu.offsetHeight)+'px';
    }
    document.onclick=HideMenu;
    landOwner=obj;
    return false;
}

function HideMenu(){
    if(!landMenuContext&&landMenuFlag){
        landMenuFlag=false;
        return true;
    }
    var menu=document.getElementById('menu');
    menu.style.display='none';
    document.onclick=null;
}

function strToInt(str)
{
    return parseInt('0'+str, 10);
}
