//Based on Context menu Script-© Dynamic Drive (www.dynamicdrive.com)
//adapted by Lev Sudakov for ivrit-online.com, 2004

var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all

if (ie5||ns6)
var menuObj=null;

function showMenu(e)
{
        var firingobj=ie5? event.srcElement : e.target;
//        alert (firingobj.getAttribute("menucode"))
        var mcode=firingobj.getAttribute("menucode");
        var mtype=firingobj.getAttribute("menutype");
        if (mcode == null || mtype == "left")
        {
                hideMenu();
                return true; // is not our case
        }
        menuObj = document.getElementById("__cont_menu");
        if (menuObj == null) return true; // the page is not well configured: __cont_menu is absent

        var x = "";
        var trn = firingobj.getAttribute("trans");
        if (trn != null) x += "<div class='menuTrans'>" + trn + "</div>";

        var a = mcode.split("|");
        var i;
        for (i in a)
        {
                var b = a[i].split("/");
                if ((b[0] != null) && (b[1] != null))
                {
                        var sWin = "";
                        var regexp = /~/g;
                        b[1] = b[1].replace(regexp,"/");
                        if ((b.length > 2) && !isNaN(b[2]) && !isNaN(b[3])) sWin = " winWidth='" + b[2] + "' winHeight='" + b[3] + "'";
                        x += "<div class='menuItem' url='" + b[1] +"'" + sWin + ">" + b[0] + "</div>";
                }
        }
        menuObj.innerHTML = x;

        //Find out how close the mouse is to the corner of the window
        var rightedge=ie5? document.body.clientWidth-event.clientX : window.innerWidth-e.clientX
        var bottomedge=ie5? document.body.clientHeight-event.clientY : window.innerHeight-e.clientY
        //if the horizontal distance isn't enough to accomodate the width of the context menu
        if (rightedge<menuObj.offsetWidth)
        //move the horizontal position of the menu to the left by it's width
        menuObj.style.left=ie5? document.body.scrollLeft+event.clientX-menuObj.offsetWidth : window.pageXOffset+e.clientX-menuObj.offsetWidth
        else
        //position the horizontal position of the menu where the mouse was clicked
        menuObj.style.left=ie5? document.body.scrollLeft+event.clientX : window.pageXOffset+e.clientX

        //same concept with the vertical position
        var top;
        if (bottomedge<menuObj.offsetHeight)
                top=ie5? document.body.scrollTop+event.clientY-menuObj.offsetHeight : window.pageYOffset+e.clientY-menuObj.offsetHeight
        else
                top=ie5? document.body.scrollTop+event.clientY : window.pageYOffset+e.clientY

        menuObj.style.top = top+7;
        menuObj.style.visibility="visible"
        return false;
}

function hideMenu()
{
        if (menuObj)
        {
                menuObj.style.visibility="hidden";
                menuObj=null;
        }
}

function highlight(e)
{
        var firingobj=ie5? event.srcElement : e.target
        //alert(firingobj.parentNode.className + ' ' + firingobj.innerText)
        if (firingobj.className=="menuItem"||ns6&&firingobj.parentNode.className=="menuItem")
        {
                if (ns6&&firingobj.parentNode.className=="menuItem") firingobj=firingobj.parentNode //up one node
                firingobj.style.backgroundColor="highlight"
                firingobj.style.color="HighlightText"
        }
        //else if (parentNode.className=="menuItem")
}

function lowlighti(e)
{
        var firingobj=ie5? event.srcElement : e.target
        if (firingobj.className=="menuItem"||ns6&&firingobj.parentNode.className=="menuItem")
        {
                if (ns6&&firingobj.parentNode.className=="menuItem") firingobj=firingobj.parentNode //up one node
                firingobj.style.backgroundColor=""
                firingobj.style.color="activecaption"
        }
}

function jumpto(e)
{
        var firingobj=ie5? event.srcElement : e.target
        if (firingobj.className=="menuItem"||ns6&&firingobj.parentNode.className=="menuItem")
        {
                if (ns6&&firingobj.parentNode.className=="menuItem") firingobj=firingobj.parentNode;
                var sUrl = firingobj.getAttribute("url");
                var w = firingobj.getAttribute("winWidth");
                if (w)
                {
                        var h = firingobj.getAttribute("winHeight");
                        var l = eval((screen.width - w) / 2);
                        var t = eval((screen.height - h) / 2);
                        Popup(sUrl,"_blank","left="+l+",top="+t+",width="+w+",height="+h+",menubar=no,scrollbars=yes")
                }
                else
                        window.location = sUrl;
        }
}

document.onclick=showMenu;