// Copyright 2002 Eddie Traversa, etraversa@dhtmlnirvana.com, http://dhtmlnirvana.com
// Free to use as long as this copyright notice stays intact.
// Courtesy of SimplytheBest.net - http://simplythebest.net/scripts/

// usage: onContextMenu="return showMenu(event);"

var ie5 = (document.getElementById&&document.all); 
var n6 	= (document.getElementById&&!document.all); 
var x,y,el,pageW,pageH;  

document.getElementById("image").oncontextmenu=function() {return false;}

if(document.getElementById("sgNavMap") != null)
{
  document.getElementById("sgNavMap").oncontextmenu=function() {return false;}
}

document.getElementById("menu").oncontextmenu=function() {return false;}

if (n6)  
{
  document.getElementById("image").addEventListener("mouseup",showMenu,false);
  if(document.getElementById("sgNavMap") != null)
  {
    document.getElementById("sgNavMap").addEventListener("mouseup",showMenu,false);
  }
}
if (ie5)
{
  document.getElementById("image").attachEvent("oncontextmenu",showMenu); 
  document.getElementById("image").attachEvent("onclick",showMenu); 
  if(document.getElementById("sgNavMap") != null)
  {
    document.getElementById("sgNavMap").attachEvent("oncontextmenu",showMenu); 
    document.getElementById("sgNavMap").attachEvent("onclick",showMenu); 
  }
}



function showMenu(event) 
{ 
  if (document.getElementById) 
  {
    pageW   = document.body.offsetWidth;
    pageH   = document.body.offsetHeight;
    x	    = event.clientX  
    y     	= event.clientY 
    menuobj 	= document.getElementById("menu");
    
    if ((ie5&&event.type=="contextmenu")||(n6 && event.which>1)) 
    {
      
      /*if ((x+parseInt(menuobj.offsetWidth))>=pageW) {
        x		= parseInt(menuobj.offsetWidth);
        y		= parseInt(menuobj.offsetHeight);
      }
      menuobj.style.top=y+"px";
      menuobj.style.left=x+"px";*/
      
        //Find out how close the mouse is to the corner of the window
        var rightedge=ie5? document.body.clientWidth-event.clientX : window.innerWidth-event.clientX;
        var bottomedge=ie5? document.body.clientHeight-event.clientY : window.innerHeight-event.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+"px" : window.pageXOffset+e.clientX-menuobj.offsetWidth+"px";
        }
        else
        {
          //position the horizontal position of the menu where the mouse was clicked
          menuobj.style.left=ie5? document.body.scrollLeft+event.clientX+"px" : window.pageXOffset+event.clientX+"px";
        }
        
        //same concept with the vertical position
        if (bottomedge<menuobj.offsetHeight)
        {
          menuobj.style.top=ie5? document.body.scrollTop+event.clientY-menuobj.offsetHeight+"px" : window.pageYOffset+event.clientY-menuobj.offsetHeight+"px";
        }
        else
        {
          menuobj.style.top=ie5? document.body.scrollTop+event.clientY+"px" : window.pageYOffset+event.clientY+"px";
        }

        menuobj.style.visibility = 'visible';
    }
    
    if ((ie5&&event.type=="click")||(n6 && event.which==1)) 
    {
      menuobj.style.visibility="hidden";
    }
  }
  
  return false;
}

function hideMenu()
{
   menuobj = document.getElementById("menu");
   menuobj.style.visibility="hidden";
}
