﻿var defaultMenuWidth = "250px";  //set default menu width.

var ie5 = document.all && !window.opera;
var ns6 = document.getElementById;

if (ie5 || ns6)
    document.write('<div id="popitmenu" onMouseover="clearhidemenu();" onMouseout="dynamichide(event)"></div>');

function iecompattest() {
    return (document.compatMode && document.compatMode.indexOf("CSS") != -1) ? document.documentElement : document.body;
}

function showmenu(e, id, name, add, optWidth) {
    if (!document.all && !document.getElementById)
        return;

    var menu = '';
    menu += "<b>"+name + ":</b><br>";
    if (add == 1) {
        if (id == 0) {
            menu += '<a href=\"/BewerkMenu/1/a/1\">Item toevoegen<br>';
        } else {
            menu += '<a href=\"/BewerkMenu/' + id + '/a\">Item toevoegen<br>';
        }
    }
    if (id != 0) {
        menu += '<a href=\"/BewerkMenu/' + id + '/e\">Bewerken</a>';
        menu += '<a href=\"/BewerkMenu/' + id + '/d\">Verwijderen</a>';
    }
    

    clearhidemenu();
    menuobj = ie5 ? document.all.popitmenu : document.getElementById("popitmenu");
    menuobj.innerHTML = menu;
    menuobj.style.width = (typeof optWidth != "undefined") ? optWidth : defaultMenuWidth;
    menuobj.contentwidth = menuobj.offsetWidth;
    menuobj.contentheight = menuobj.offsetHeight;
    eventX = ie5 ? event.clientX : e.clientX;
    eventY = ie5 ? event.clientY : e.clientY;
    eventX += 5;
    eventY += 5;
    //Find out how close the mouse is to the corner of the window
    var rightedge = ie5 ? iecompattest().clientWidth - eventX : window.innerWidth - eventX;
    var bottomedge = ie5 ? iecompattest().clientHeight - eventY : window.innerHeight - eventY;
    //if the horizontal distance isn't enough to accomodate the width of the context menu
    if (rightedge < menuobj.contentwidth)
    //move the horizontal position of the menu to the left by it's width
        menuobj.style.left = ie5 ? iecompattest().scrollLeft + eventX - menuobj.contentwidth + "px" : window.pageXOffset + eventX - menuobj.contentwidth + "px";
    else
    //position the horizontal position of the menu where the mouse was clicked
        menuobj.style.left = ie5 ? iecompattest().scrollLeft + eventX + "px" : window.pageXOffset + eventX + "px";
    //same concept with the vertical position
    if (bottomedge < menuobj.contentheight)
        menuobj.style.top = ie5 ? iecompattest().scrollTop + eventY - menuobj.contentheight + "px" : window.pageYOffset + eventY - menuobj.contentheight + "px";
    else
        menuobj.style.top = ie5 ? iecompattest().scrollTop + event.clientY + "px" : window.pageYOffset + eventY + "px";
    menuobj.style.visibility = "visible";
    return false;
}

function contains_ns6(a, b) {
    //Determines if 1 element in contained in another
    while (b.parentNode)
        if ((b = b.parentNode) == a)
            return true;
    return false;
}

function hidemenu() {
    if (window.menuobj)
        menuobj.style.visibility = "hidden";
}

function dynamichide(e) {
    if (ie5 && !menuobj.contains(e.toElement))
        hidemenu();
    else if (ns6 && e.currentTarget != e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget))
        hidemenu();
}

function delayhidemenu() {
    delayhide = setTimeout("hidemenu()", 500);
}

function clearhidemenu() {
    if (window.delayhide)
        clearTimeout(delayhide);
}

if (ie5 || ns6)
    document.onclick = hidemenu;
