// 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;  

function showMenu(event) 
{ 
  if (document.getElementById) 
  {
    pageW   = document.body.offsetWidth;
    pageH   = document.body.offsetHeight;
    x	    = event.clientX  
    y    	= event.clientY 
 	   	
        var e_out = 0;
      	var ie_var = "srcElement";
      	var moz_var = "target";
      	var prop_var = "myflag";
      	// "target" for Mozilla, Netscape, Firefox et al. ; "srcElement" for IE
      	event[moz_var] ? e_out = event[moz_var][prop_var] : e_out = event[ie_var][prop_var];
        menuobj 	= document.getElementById("menu_"+e_out);
  
      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(i)
{
    menuobj 	= document.getElementById("menu_"+i);
    menuobj.style.visibility="hidden";
}
