﻿var LastCursorPosition=new GLatLng(0,0);

/*
 * This should be called by the Initialize method. It hooks up the event handler routines 
 */
function InitializeEventHandlers()
{
    // Add each event handler
    GEvent.addListener(map,"click",Map_OnClick);
    GEvent.addListener(map,"dblclick",Map_OnDoubleClick);    
    GEvent.addListener(map,"singlerightclick",Map_OnSingleRightClick);    
    GEvent.addListener(map,"movestart",Map_OnMoveStart);
    GEvent.addListener(map,"move",Map_OnMove);
    GEvent.addListener(map,"moveend",Map_OnMoveEnd);
    GEvent.addListener(map,"zoomend",Map_OnZoomEnd);
    GEvent.addListener(map,"maptypechanged",Map_OnTypeChanged);
    GEvent.addListener(map,"infowindowopen",Map_OnInfoWindowOpen);
    GEvent.addListener(map,"infowindowbeforeclose",Map_OnInfoWindowBeforeClose);
    GEvent.addListener(map,"infowindowclose",Map_OnInfoWindowClose);
    GEvent.addListener(map,"mouseover",Map_OnMouseOver);
    GEvent.addListener(map,"mouseout",Map_OnMouseOut);
    GEvent.addListener(map,"mousemove",Map_OnMouseMove);
    GEvent.addListener(map,"dragstart",Map_OnDragStart);
    GEvent.addListener(map,"drag",Map_OnDrag);
    GEvent.addListener(map,"dragend",Map_OnDragEnd);
    GEvent.addListener(map,"load",Map_OnLoad);
}

/*
 * This event is fired when the map is clicked with the mouse. If the click was on a marker, then the marker is
 * passed to the event handler in the overlay argument, and a click event is also fired on the marker. Otherwise,
 * the geographical coordinates of the point that was clicked are passed in the point argument. 
 */
function Map_OnClick(overlay, point)
{
    //FadeAllTopMenus();

    // If the user's in map options mode and is clicking the map, they want out
    //if(MapMode==MapOptionsMode && overlay==null)
        //FadeAllTopMenus();

    // If the context help is not being shown that means an info window was popped up and the user may
    // be clicking away from the info window not meaning to insert a sprite or path point.
    // So: we need to check and see if the context help is hidden and if it is, we'll ignore this click
    // if(!IsContextSensitiveHelpShown())
   //     return false;

    // If we're in path edit mode, insert a path point
    if(MapMode==PathEditMode && overlay==null && !InfoWindowBeingDisplayed)
    {
        
        if(PathPoints[PathPoints.length-1] != undefined) {
            var DistanceInMeters = PathPoints[PathPoints.length-1].getLocation().distanceFrom(point);
            var DistanceInKilometers=parseFloat(DistanceInMeters)/1000;
            var DistanceInMiles=DistanceInMeters*0.000621371192; // 1 meter = 0.000621371192 miles
            if(DistanceInMiles > 50) {
                alert("Your point is too far from previous point.");
                return false;   
            }
        }     
       
        PathPoints[PathPoints.length] = new PathPoint(point,'');
            
        // If the snap-to-road feature is enabled, snap this new point to the road
        if(SnapToRoadEnabled)
            SnapLatLngToRoad(PathPoints.length-1, point, true);

        RefreshMap(point);

        // Recenter the map at the click
        map.panTo(point);
    }
    
    // If we're in sprite insert mode, insert a sprite of the selected sprite type
    if(MapMode==SpriteInsertMode && overlay==null)
    {
        // Create a new sprite and add it to our list of sprites
        var NewSprite = new Sprite(SpriteTypeBeingInserted,point,'');
        Sprites[Sprites.length] = NewSprite;
        
        // Return the user to doing whatever it was they were doing
        LeaveSpriteInsertMode(point);
        
        // Recenter the map at the click
        map.panTo(point);
    }
    
    /*
    if(MapMode==IntroductionMode)
    {
        var x = $find('StartLinkPulse');
        x.get_OnLoadBehavior().play();
    }*/
}

/*
 * This event is fired when a double click is done on the map. Notice that this event will not be fired if the 
 * double click was on a marker. The geographical coordinates of the point that was double clicked are passed in 
 * the point argument. The overlay argument is always set to null. (Since 2.83) 
 */
function Map_OnDoubleClick(overlay, point)
{
}

/*
 * This event is fired when the DOM contextmenu event is fired on the map container. If the right click was on 
 * a marker, then the marker is passed to the event handler in the overlay argument. The pixel coordinates (in 
 * the DOM element that holds the map) of the point that was right clicked and the source element of the DOM 
 * event are passed in the point and src arguments respectively. Note that if it is a double right click and 
 * double click to zoom is enabled, then the map zooms out and no singlerightclick event is fired. If, however,
 * double click to zoom is disabled, two singlerightclick events will be fired. (Since 2.84) 
 */
function Map_OnSingleRightClick(point, src, overlay)
{
}

/*
 * This event is fired when the map view starts changing. This can be caused by dragging, in which case a dragstart 
 * event is also fired, or by invocation of a method that changes the map view. 
 */
function Map_OnMoveStart()
{
    //FadeAllTopMenus();
}

/*
 * This event is fired, possibly repeatedly, while the map view is changing.
 * This is not the handler for the mousemove event
 */
function Map_OnMove()
{
}

/*
 * This event is fired when the change of the map view ends. 
 */
function Map_OnMoveEnd()
{
}

/*
 * This event is fired when the map reaches a new zoom level. The event handler receives the previous and the new 
 * zoom level as arguments. 
 */
function Map_OnZoomEnd(oldLevel,newLevel)
{
}

/*
 * This event is fired when another map type is selected. 
 */
function Map_OnTypeChanged()
{
}

/*
 * This event is fired when the info window opens. 
 */
function Map_OnInfoWindowOpen()
{
}

/*
 * This event is fired before the info window closes. (Since 2.83) 
 */
function Map_OnInfoWindowBeforeClose()
{
}

/*
 * This event is fired when the info window closes. The event infowindowbeforeclose is fired before this event.
 * If a currently open info window is reopened at a different point using another call to openInfoWindow*(), 
 * the events infowindowbeforeclose, infowindowclose and infowindowopen are fired in this order. 
 */
function Map_OnInfoWindowClose()
{
}

/*
 * This event is fired when the user moves the mouse over the map from outside the map. 
 */
function Map_OnMouseOver(LatLng)
{
}

/*
 * This event is fired when the user moves the mouse off the map. 
 */
function Map_OnMouseOut(LatLng)
{
}

/*
 * This event is fired when the user moves the mouse inside the map. 
 */
function Map_OnMouseMove(LatLng)
{
    // Remember the location on the map of the cursor
    LastCursorPosition=LatLng;

    // If we're in map edit mode show only the closest path point marker (draggable icon)
    //      - We need detect whether a mouse drag is in progress and not change things if this is so
    //      - If context-sensitive help is not visible we need to ignore this event
    if(MapMode == PathEditMode && !DragIsInProgress && !InfoWindowBeingDisplayed)
    {
        ShowOnlyThePathPointMarkerClosestToTheCursor(LatLng);
    }
    
    UpdateDistanceDisplay();
}

/*
 * This event is fired when the user starts dragging the map. 
 */
function Map_OnDragStart()
{
}

/*
 * This event is repeatedly fired while the user drags the map. 
 */
function Map_OnDrag()
{
}

/*
 * This event is fired when the user stops dragging the map. 
 */
function Map_OnDragEnd()
{
}

/*
 * This event is fired when the map setup is complete, and isLoaded() would return true. This means 
 * position, zoom, and map type are all initialized, but tile images may still be loading. (Since 2.52) 
 */
function Map_OnLoad()
{
}

