    var RightClickMenu_mouse_x = 0;
    var RightClickMenu_mouse_y = 0;
    var distance_to_right_edge = 0;
    var distance_to_bottom = 0;
    var contextmenu = null;
    var RightClickMouseType = null; // for map action (selection/none/zoom)
    var tempRightClickLongLat; // long lat on right click

    function RightClickMenu() {
   
	if(document.getElementById("thecontextmenu"))
	{
      var tmpUL1 = document.getElementById("thecontextmenu");
	    tmpUL1.innerHTML = "";
        } else {
            tmpDiv1 = document.createElement("div");
            tmpDiv1.id = "contextMenuDiv";
            tmpDiv1.style.zIndex = 9999999;
            tmpDiv1.style.position = 'absolute';
			tmpDiv1.style.top = "0px";
            
            tmpDiv1.style.visibility = 'visible';
            var tmpUL1 = document.createElement("ul");
            tmpUL1.id = "thecontextmenu";
            tmpUL1.className = "contextmenu";
	}
        tmpUL1.style.left = "100px";
        tmpUL1.style.top = "100px";
        tmpUL1.style.visibility = "hidden";

	// bookmark menu
/*        var tmpLi1 = document.createElement("li");
        tmpLi1.onclick= bookmarkPosition;
        tmpLi1.onmouseover= function() { context_menuitem_highlight(this) } 
        tmpLi1.onmouseout = function() { context_menuitem_unhighlight(this) }
        tmpLi1.innerHTML = "Bookmark this location";
        tmpUL1.appendChild(tmpLi1);
  */      
	// zoom in menu
        var tmpLiZoomIn = document.createElement("li");
        tmpLiZoomIn.onclick= menuZoomIn;
        tmpLiZoomIn.onmouseover= function() { context_menuitem_highlight(this) } 
        tmpLiZoomIn.onmouseout = function() { context_menuitem_unhighlight(this) }
        tmpLiZoomIn.innerHTML = "Zoom in here";
		if (parseInt(mapObj.getLevel())<16)
		{
			tmpUL1.appendChild(tmpLiZoomIn);  
		}

	// zoom out menu
        var tmpLiZoomOut = document.createElement("li");
        tmpLiZoomOut.onclick= menuZoomOut;
        tmpLiZoomOut.onmouseover= function() { context_menuitem_highlight(this) } 
        tmpLiZoomOut.onmouseout = function() { context_menuitem_unhighlight(this) }
        tmpLiZoomOut.innerHTML = "Zoom out here";
		if (parseInt(mapObj.getLevel())>1)
		{
			tmpUL1.appendChild(tmpLiZoomOut);  
		}

	// center map menu
        var tmpLiCenterMap = document.createElement("li");
        tmpLiCenterMap.onclick= menuCenterMapHere;
        tmpLiCenterMap.onmouseover= function() { context_menuitem_highlight(this) } 
        tmpLiCenterMap.onmouseout = function() { context_menuitem_unhighlight(this) }
        tmpLiCenterMap.innerHTML = "Center map here";
        tmpUL1.appendChild(tmpLiCenterMap);  

	if (mapObj.getMouseType() == "distance") {

            var tmpLiClearDraw = document.createElement("li");
            tmpLiClearDraw.onclick= createDistanceDraw;
        
            tmpLiClearDraw.onmouseover= function() { context_menuitem_highlight(this) } 
            tmpLiClearDraw.onmouseout = function() { context_menuitem_unhighlight(this) }
            tmpLiClearDraw.innerHTML = "Clear distance point";
            tmpLiClearDraw.style.borderTop = "solid 1px #bbb";
            tmpUL1.appendChild(tmpLiClearDraw);
           
           if (GM_ShowDistanceInfo == false) {   
            var tmpLi2 = document.createElement("li");
            tmpLi2.onclick= showDataDistance;
        
            tmpLi2.onmouseover= function() { context_menuitem_highlight(this) } 
            tmpLi2.onmouseout = function() { context_menuitem_unhighlight(this) }
            tmpLi2.innerHTML = "Show distance";
            tmpUL1.appendChild(tmpLi2);  
          }
	}

        tmpUL1.style.backgroundColor = 'white';

        var GM_Opacity = 70;
		if (window.stop) {
		      tmpUL1.style.MozOpacity = GM_Opacity/100;
		} else {
		      tmpUL1.style.filter = 'alpha(opacity='+GM_Opacity+')';
        }
        tmpDiv1.appendChild(tmpUL1);
        document.body.appendChild(tmpDiv1);


    }

    function bookmarkPosition() {
	return tempRightClickLongLat;
    }

    function menuZoomIn (mouseEvent) {
    	mapObj.changeLevel(parseInt(mapObj.getLevel())+1, tempRightClickLongLat[0],tempRightClickLongLat[1]);
    }

    function menuZoomOut (mouseEvent) {
    	mapObj.changeLevel(parseInt(mapObj.getLevel())-1, tempRightClickLongLat[0],tempRightClickLongLat[1]);
    }

    function menuCenterMapHere (mouseEvent) {
	mapObj.recenter(tempRightClickLongLat[0],tempRightClickLongLat[1]);
    }

    function RightClickGetPosition(mouseEvent) {

    	if (!mouseEvent) mouseEvent = window.event;
    	var tmpxRuler = parseInt(mouseEvent.clientX,10);
    	var tmpyRuler = parseInt(mouseEvent.clientY,10);
    	var RulerObj = document.getElementById('mapDiv_' + mapObj.divName);
    	var OS = ShowOffSet('canvasDiv_' + mapObj.divName);
    	var aa = getBodyScroll() ;
    	var RulerX = tmpxRuler - parseInt(RulerObj.style.left+0,10) - parseInt(OS.left+0,10) +  aa[0] + parseInt(mapObj.xStart);
    	var RulerY = tmpyRuler - parseInt(RulerObj.style.top+0,10) - parseInt(OS.top+0,10) + aa[1] +  parseInt(mapObj.yStart);

    	tempRightClickLongLat = getLongLat(parseInt(RulerX), parseInt(RulerY)) ;

    }

    function RightClickMenu_onStart() {

//	drawPointer();
	RightClickMenu();

	var tmpDiv = document.getElementById(mapObj.divName);
	
        tmpDiv.oncontextmenu = function(event) {
    	    RightClickGetPosition(event);
    	    RightClickMenu();
          show_contextmenu(event);
          return false;
        } 

        contextmenu = document.getElementById('thecontextmenu');
        addGenericEvent(document,'mousemove',mousemove);
        addGenericEvent(document,'click',hide_contextmenu);
    }


    function context_menuitem_highlight(element, color) {
        element.className = "highlight";
    }

    function context_menuitem_unhighlight(element) {
        element.className = "";
    }


    function show_contextmenu(e) {

	RightClickMouseType = mapObj.getMouseType();
	this.mapObj.setMouseType("none");
        get_page_boundaries();
           
           
        var tmpDiv1 = document.getElementById('contextMenuDiv');   
        tmpDiv1.style.left = RightClickMenu_mouse_x+"px";
        tmpDiv1.style.top = RightClickMenu_mouse_y+"px";
        contextmenu.style.visibility = "visible";        

        //adjust menu if near window edge
        if (distance_to_right_edge < contextmenu.offsetWidth) 
            tmpDiv1.style.left = 2+RightClickMenu_mouse_x - tmpDiv1.offsetWidth+"px";  // The 2+ is not some dumb kludge - 	
        if (distance_to_bottom < contextmenu.offsetHeight)                    // it places the menu just under the pointer,
            tmpDiv1.style.top = 2+RightClickMenu_mouse_y - tmpDiv1.offsetHeight+"px";  // instead of just outside
        try {
            window.getSelection().collapseToStart();  // try to compensate for tendency to treat right-clicking as text selection
        } catch (e) {} // do nothing
  
        // prevent the event from bubbling up and causing the regular browser context menu to appear.
        
        if (!e) var e = window.event;
            e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();        

        return false;
    }




 function hide_contextmenu() {
    
    if (contextmenu.style.visibility == "visible") {
        	contextmenu.style.visibility = "hidden";
          GM_EnableDistanceClick = true;
          mapObj.setMouseType(RightClickMouseType); 
          
    }

  }


    function addLoadEvent(func) {
        if (window.addEventListener)
            window.addEventListener("load",func,false);
        else if (document.addEventListener)
            document.addEventListener("load",func,false);
        else if (window.attachEvent)
            window.attachEvent("onload",func);
        else if (document.attachEvent)
            document.attachEvent("onload",func);
    }

    function addGenericEvent(source, trigger, func) {
        if (source.addEventListener)
            source.addEventListener(trigger,func,false);
        else if (source.attachEvent)
            source.attachEvent("on"+trigger,func);
        }

    function window_x() {
        if (window.screenX)
            return window.screenX
        else if (window.screenLeft)
            return window.screenLeft;
    }	

    function window_y() {
        if (window.screenY)
            return window.screenY
        else if (window.screenTop)
            return window.screenTop;
    }



    function mousemove(e) { 

        if (e && e.clientX && typeof(window.scrollY) == 'number') { // Moz
            RightClickMenu_mouse_x = e.clientX + window.scrollX -100;
            RightClickMenu_mouse_y = e.clientY + window.scrollY-100;
            event_target = e.target;
        }
        else if (window.event) { // IE
            if (document.documentElement)   // Explorer 6 Strict
            {
                RightClickMenu_mouse_x = window.event.clientX + document.documentElement.scrollLeft - 4;
                RightClickMenu_mouse_y = window.event.clientY + document.documentElement.scrollTop - 4;
            }
            else if (document.body) // all other Explorers
            {
                RightClickMenu_mouse_x=window.event.clientX+document.body.scrollLeft-4;
                RightClickMenu_mouse_y=window.event.clientY+document.body.scrollTop-4;
            }
            mouse_window_x = window.event.clientX;
            mouse_window_y = window.event.clientY;
        }
    }

    function get_page_boundaries()
    {
        if (window.innerWidth) {
            distance_to_right_edge = window.innerWidth-(RightClickMenu_mouse_x - window.scrollX)
            distance_to_bottom = window.innerHeight-(RightClickMenu_mouse_y - window.scrollY);
        } else if (document.body.clientWidth) {
            distance_to_right_edge = document.body.clientWidth-RightClickMenu_mouse_x;
            distance_to_bottom = document.body.clientHeight-RightClickMenu_mouse_y;
        }
    }

// for pointer in the center of map

    function drawPointer() {
    	var Pointer = document.getElementById('canvasDiv_' + mapObj.divName);

        if (document.getElementById("GM_MyPointer_mapDiv_" + mapObj.divName)) {
            var tmpImg = document.getElementById("GM_MyPointer_mapDiv_" + mapObj.divName);
            tmpImg.style.left = ((mapObj.width/2)-(tmpImg.width/2)) + "px"; 
            tmpImg.style.top = ((mapObj.height/2)-(tmpImg.height/2))+ "px";      

        } else {
            var tmpDiv = document.createElement("div");
            tmpDiv.id = "pointer"+mapObj.divName;
            tmpDiv.style.position="absolute";
            var tmpImg = document.createElement("img");
            tmpImg.style.position="absolute";
            tmpImg.style.zIndex = "99999999999";
            tmpImg.id = "GM_MyPointer_mapDiv_" + mapObj.divName;
            tmpImg.src = "http://192.168.0.101/data/australia/point.gif"
//            tmpImg.src = imageURL + "images/icon.gif"
            tmpImg.onload = function () { 
                this.style.left = (mapObj.width/2)-(this.width/2) + "px"; 
                this.style.top = (mapObj.height/2)-(this.height/2)+ "px";      
            }

            tmpDiv.appendChild(tmpImg);
            Pointer.appendChild(tmpDiv);        
        }
    }


function createBalloonTable(left, top, balloonData)
{

    var parent = "mapDiv_" + mapObj.divName ;
    var child = "Balloon_" + mapObj.divName ;    
    if (!document.getElementById(child))
    {
        createNewLayer(parent,child) ;
        var layer_div = document.getElementById(child) ;
        layer_div.style.zIndex = 999999 ;
        layer_div.style.position = "absolute" ;     
    }  	

    
    var balloonDivParent = document.getElementById("Balloon_" + mapObj.divName);
//    balloonDivParent.style.position   = 'absolute';
//    balloonDivParent.style.visibility = 'visible';
    balloonDivParent.innerHTML        = "";
    balloonDivParent.style.zIndex     = 9999999;

    balloonDivParent.style.left = parseInt(left)+ 120 + 'px' ;
    balloonDivParent.style.top = parseInt(top)+ 60 + 'px' ;


    var balloonWidth = 200;
    var balloonHeight = 200;
    var balloonColor = "white";
    var balloonTable = document.createElement("TABLE");
    var balloonTHead = document.createElement("THEAD");
    var balloonTBody = document.createElement("TBODY");
    var oRow, oCell;

    balloonTable.id = "balloonTable";
    balloonTable.cellPadding=0;
    balloonTable.cellSpacing=0;

    // top row of table
    oRow = document.createElement("TR");
    balloonTBody.appendChild(oRow);

    // top left 
    oCell = document.createElement("TD");
    oCell.width = "9px";
    oCell.height = "9px"
    oCell.style.backgroundImage="url(images/balloon-1.png)";    
    oRow.appendChild(oCell);

    // top middle
    oCell = document.createElement("TD");
    oCell.width = balloonWidth + "px";
    oCell.style.backgroundImage="url(images/balloon-2a.png)";
    oRow.appendChild(oCell);

    // top right
    oCell = document.createElement("TD");
    oCell.width = "9px";
    oCell.height = "9px"
    oCell.style.backgroundImage="url(images/balloon-2.png)";
    oRow.appendChild(oCell);

    // close button (second row of table)
    oRow = document.createElement("TR");
    balloonTBody.appendChild(oRow);
    oCell = document.createElement("TD");
    oCell.style.backgroundImage="url(images/balloon-1a.png)";
    oRow.appendChild(oCell);    
    oCell = document.createElement("TD");

    oCell.style.backgroundColor=balloonColor;
    tmpImg = document.createElement("IMG");
    tmpImg.src=imageURL + "images/balloon-close.png";
    tmpImg.onclick = function () {
            balloonDivParent.removeChild(document.getElementById("balloonTable"));
    	}

    oCell.appendChild(tmpImg);
    oRow.appendChild(oCell);
    oCell = document.createElement("TD");
    oCell.style.backgroundImage="url(images/balloon-3a.png)";
    oRow.appendChild(oCell);    


    // middle of table
    oRow = document.createElement("TR");
    balloonTBody.appendChild(oRow);

    // middle left
    oCell = document.createElement("TD");
//    oCell.width = "9px";
    oCell.height = balloonHeight + "px";
    oCell.style.backgroundImage="url(images/balloon-1a.png)";
    oRow.appendChild(oCell);

    // center of table
    oCell = document.createElement("TD");
    oCell.id = "balloonData";
    oCell.width = balloonWidth + "px";
    oCell.height = balloonHeight + "px";
    oCell.style.backgroundColor=balloonColor;
    oCell.innerHTML = balloonData;
    oRow.appendChild(oCell);

    // middle right
    oCell = document.createElement("TD");
//    oCell.width = "9px";
    oCell.height = balloonHeight + "px";
    oCell.style.backgroundImage="url(images/balloon-3a.png)";
    oRow.appendChild(oCell);


    // end of table
    oRow = document.createElement("TR");
    balloonTBody.appendChild(oRow);

    // bottom left
    oCell = document.createElement("TD");
    oCell.width = "9px";
    oCell.height = "9px"
    oCell.style.backgroundImage="url(images/balloon-4.png)";
    oRow.appendChild(oCell);

    // bottom middle
    oCell = document.createElement("TD");
    oCell.style.backgroundImage="url(images/balloon-4a.png)";
    oRow.appendChild(oCell);

    // bottom right
    oCell = document.createElement("TD");
//    oCell.width = "9px";
//    oCell.height = "9px";
    oCell.style.backgroundImage="url(images/balloon-3.png)";
    oRow.appendChild(oCell);

    // arrow
    oRow = document.createElement("TR");
    balloonTBody.appendChild(oRow);

    oCell = document.createElement("TD");
    oRow.appendChild(oCell);
    oCell = document.createElement("TD");
    oCell.align = "center";
    oCell.colSpan = "2";
    tmpImg = document.createElement("IMG");
    tmpImg.src=imageURL + "images/balloon-arrow.png";
    oCell.appendChild(tmpImg);
    oRow.appendChild(oCell);


    balloonTable.appendChild(balloonTHead);
    balloonTable.appendChild(balloonTBody);
    balloonDivParent.appendChild(balloonTable);

}
