// Menu & item background color and text color
var sContextMenu_bg = "#ffcc00";
var sMenuItem_cl = "#000080";

// Menuitem background color and text color on mouse over
var sMenuItem_bgmov = "#006699";
var sMenuItem_clmov = "white";

// Sidebar color
var sSideBar_bg = "#003366";

// Sidebar filter color
var sSideBarFilter_bg = "#6699cc";

// Sidebar text color
var sSideBarText_cl = "white";

var ie55 = window.createPopup();
var bContextKey = false;
var bMenuStatus = false;
var sMenu_width = "100px";
var sMenuTitle = "Képek letöltése";
var sMenuItemsHTML = "";
var sMenuitem = "menuitem";
var sComponent = "component";
var sBrowsermsg = "";

function fnContextMenu() {
   bContextKey = true;
   fnDetermine();
   bContextKey = false;
   return false;
}

function AddMenuitem(txt,ico,lnk) {
    if (ie55) {
        if (!ico) ico = 'ico.gif';
        sMenuItemsHTML += "<DIV class='menuitem' style='color:"+sMenuItem_cl+"' link='"+lnk+"' component='"+sMenuitem+"'><IMG src='"+ico+"' class='icon'>"+txt+"</DIV>";
    }
}

function fnDetermine() {
    if (ie55) {
        var oWorkItem = event.srcElement;
        var oMenu = document.getElementById('oContextMenu');
        var oShadowLayer = document.getElementById('oShadow');
        if (bContextKey) {
            // Continue if the menu is closed.
            if (!bMenuStatus) {
                // Give the menu mouse capture.
                oMenu.setCapture();
                oMenu.style.cursor = "default";
                // Set styles
                oMenu.style.backgroundColor = sContextMenu_bg;
                oMenu.style.borderColor = sContextMenu_bg;
                oMenu.style.width = sMenu_width;
                oShadowLayer.style.width = sMenu_width;

                // Set the menu content.
                var sHTML="<DIV class='sidebar' style='background-color:"+sSideBar_bg+";border-color:"+sContextMenu_bg+"'><DIV class='sidebar-filter' style='background-color:"+sSideBarFilter_bg+"'></DIV><DIV class='sidebar-text' style='color:"+sSideBarText_cl+"'>"+sMenuTitle+"</DIV></DIV>";
                oMenu.innerHTML = sHTML + sMenuItemsHTML;

                // Set display for getting the height, but the menu is still not visible
                oMenu.style.display = "block";
                var iMenu_height = oMenu.offsetHeight;

                // Move the menu ten pixels lower than the mouse inside the client area.
                var imaxY = document.body.clientHeight;
                var imY = event.clientY + 10;
                var iscrollTop = document.body.scrollTop;

                // Ten more pixels for the shadow
                if ((imY + iMenu_height + 10) >= imaxY) {
                    oMenu.style.pixelTop = iscrollTop + imaxY - iMenu_height - 10;
                    oShadowLayer.style.pixelTop = iscrollTop + imaxY - iMenu_height - 10;
                }
                else {
                    oMenu.style.pixelTop = iscrollTop + imY;
                    oShadowLayer.style.pixelTop = iscrollTop + imY;
                };

                // Move the menu ten pixels right of the mouse.
                var imaxX = document.body.clientWidth;
                var imX = event.clientX + 10;
                var iscrollLeft = document.body.scrollLeft;
                var iMenu_width = parseInt(sMenu_width);

                if ((imX + iMenu_width + 10) >= imaxX) {
                    oMenu.style.pixelLeft = iscrollLeft + imaxX - iMenu_width - 10;
                    oShadowLayer.style.pixelLeft = iscrollLeft + imaxX - iMenu_width - 10;
                }
                else {
                    oMenu.style.pixelLeft = iscrollLeft + imX;
                    oShadowLayer.style.pixelLeft = iscrollLeft + imX;
                };

                // Display the menu.
                oMenu.filters.item("DXImageTransform.Microsoft.Alpha").Enabled = false;
                oShadowLayer.style.pixelHeight = iMenu_height;
                oShadowLayer.style.display = "block";
                // Indicate that the menu is now open.
                bMenuStatus = true;
                event.returnValue = false;
            }
            else { event.returnValue = false;
            }
        }   
        else { 
            // Continue if the menu is open.
            if (bMenuStatus) {
                // Continue if a menu item was clicked.
                var oParent = oWorkItem.parentElement;
                var sId = "oContextMenu";
                var bItemClicked = ((oParent.id == sId) && (oWorkItem.getAttribute(sComponent) == sMenuitem));
                var bIconClicked = ((oParent.parentElement != null) && (oParent.parentElement.id == sId) && (oParent.getAttribute(sComponent) == sMenuitem));
                if (bItemClicked || bIconClicked) {
                    if (bItemClicked) {
                        parent.location.href = oWorkItem.getAttribute("link");
                    }
                    else {
                        parent.location.href = oParent.getAttribute("link"); 
                    }
                }
 
                // Hide the menu.
                oMenu.style.display = "none";
                oMenu.style.cursor = "auto";
                oMenu.filters.item("DXImageTransform.Microsoft.Alpha").Enabled = true;
                oShadowLayer.style.display = "none";
                // Indicate the menu is closed.
                bMenuStatus = false;
                // Remove mouse capture from the menu.
                oMenu.releaseCapture();
                oMenu.innerHTML = "";
                // Stop processing the event.
                event.returnValue = false;
            }
        }
    }
    else {
        if (bContextKey) {
            alert(sBrowsermsg);
        }
    }
}

function fnHighlightArea() {
    // Continue if the mouse is inside the client area.
    return ((event.clientX > 0) && (event.clientY > 0) && (event.clientX < document.body.offsetWidth) && (event.clientY < document.body.offsetHeight));
}

function fnSetItemColor(bg,cl) {
      var oWorkItem = event.srcElement;
      var oParentItem = oWorkItem.parentElement;
      var bMenuItem = (oWorkItem.getAttribute(sComponent) == sMenuitem);
      var bIcon = (oParentItem.getAttribute(sComponent) == sMenuitem);
      if (bMenuItem || bIcon) {
          if (bMenuItem) {
              oWorkItem.style.backgroundColor = bg;
              oWorkItem.style.color = cl;
          }
          else {
              oParentItem.style.backgroundColor = bg;
              oParentItem.style.color = cl;
          }
      }
}

function fnHighlightOn() {
   if (fnHighlightArea()) {
       fnSetItemColor(sMenuItem_bgmov, sMenuItem_clmov);
   }
}

function fnHighlightOff() {
   if (fnHighlightArea()) {
       fnSetItemColor(sContextMenu_bg, sMenuItem_cl);
   }
}

