﻿function ReturnContextMenuDiv(mapVar){
    // === create the context menu div ===
    contextmenu = document.createElement("div");
    contextmenu.style.visibility="hidden";
    contextmenu.style.background="#ffffff";
    contextmenu.style.border="1px solid #8888FF";

    contextmenu.innerHTML = '<a href="javascript:zoomInToStreet(' + mapVar + ')"><div class="context">&nbsp;&nbsp;Zoom In To Street Level&nbsp;&nbsp;</div></a>'
    + '<a href="javascript:centerMapHere(' + mapVar + ')"><div class="context">&nbsp;&nbsp;Center Map Here&nbsp;&nbsp;</div></a>'
    return contextmenu;
}

function SetupContextMenuEventListeners(mapVar){
    // === listen for singlerightclick ===
    GEvent.addListener(mapVar,"singlerightclick",function(pixel,tile) {
        // store the "pixel" info in case we need it later
        // adjust the context menu location if near an egde
        // create a GControlPosition
        // apply it to the context menu, and make the context menu visible
        clickedPixel = pixel;
        var x=pixel.x;
        var y=pixel.y;
        if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }
        if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }
        var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
        pos.apply(contextmenu);
        contextmenu.style.visibility = "visible";
    });

    // === If the user clicks on the map, close the context menu ===
    GEvent.addListener(mapVar, "click", function(overlay,point) {
        contextmenu.style.visibility="hidden";
    });
}

// === functions that perform the context menu options ===
function zoomIn(mapVar) {
    // perform the requested operation
    mapVar.zoomIn();
    // hide the context menu now that it has been used
    contextmenu.style.visibility="hidden";
}      
function zoomInToStreet(mapVar) {
    // perform the requested operation
    var point = mapVar.fromContainerPixelToLatLng(clickedPixel)
    mapVar.setCenter(point);
    mapVar.setZoom(16);
    // hide the context menu now that it has been used
    contextmenu.style.visibility="hidden";
}      
function zoomOut(mapVar) {
    // perform the requested operation
    mapVar.zoomOut();
    // hide the context menu now that it has been used
    contextmenu.style.visibility="hidden";
}      
function zoomInHere(mapVar) {
    // perform the requested operation
    var point = mapVar.fromContainerPixelToLatLng(clickedPixel)
    mapVar.zoomIn(point,true);
    // hide the context menu now that it has been used
    contextmenu.style.visibility="hidden";
}      
function zoomOutHere(mapVar) {
    // perform the requested operation
    var point = mapVar.fromContainerPixelToLatLng(clickedPixel)
    mapVar.setCenter(point,map.getZoom()-1); // There is no map.zoomOut() equivalent
    // hide the context menu now that it has been used
    contextmenu.style.visibility="hidden";
}      
function centerMapHere(mapVar) {
    // perform the requested operation
    var point = mapVar.fromContainerPixelToLatLng(clickedPixel)
    mapVar.setCenter(point);
    // hide the context menu now that it has been used
    contextmenu.style.visibility="hidden";
}