﻿/*
 * Returns the distance in meters
 */
function GetCourseDistance()
{
    var Total=0;
    for(var i=1;i<PathPoints.length;i++)
        if(PathPoints[i-1]!=null && PathPoints[i]!=null) // thread safety issue
            Total+=PathPoints[i-1].getLocation().distanceFrom(PathPoints[i].getLocation());
    return Total;
}

/*
 * Shows the mile / kilometer markers
 * This function only goes up to 99 miles / kilometers
 */
function RefreshPathMileMarkers()
{
    // If the user doesn't want to see distance markers or there aren't any
    // path points to show, just bail here
    if(!ShowDistanceMarkers || PathPoints.length==0)
        return;
        
    // We need to just get an array of LatLngs
    var LatLngs = new Array();
    for(var i=0;i<PathPoints.length;i++)
        LatLngs.push(PathPoints[i].getLocation());
        
    var Polyline = new GPolyline(LatLngs);
    
    var DistanceInMeters=GetCourseDistance();
    var DistanceInKilometers=parseFloat(DistanceInMeters)/1000;
    var DistanceInMiles=DistanceInMeters*0.000621371192; // 1 meter = 0.000621371192 miles
    
    // We going with miles or kilos?
    var DistanceStepInMeters = UnitOfMeasure == EnglishUnits ? 1609.344 : 1000;
    var TotalDistanceInMeters = Polyline.Distance();
    var NumberOfMarkers = Math.floor(TotalDistanceInMeters / DistanceStepInMeters);
    
    // No way are we showing more than 99 markers
    var MaxNumberOfMarkers=20;
    if(NumberOfMarkers > 20)
        NumberOfMarkers = 20;

    
    // Add each mile / kilo marker to the map
    for(var i=1;i<=NumberOfMarkers;i++)
    {
        var LatLngForDistanceMarker = Polyline.GetPointAtDistance(DistanceStepInMeters * i);

        // Use a numbered mile marker icon. We have 1 - 99
        var MarkerIcon = 'css/app/MileMarkers/' + i.toString() + '.PNG';
        
	    var NewIcon = new GIcon();
        NewIcon.image = MarkerIcon;
        NewIcon.iconSize = new GSize(20, 20);
        NewIcon.iconAnchor = new GPoint(10, 20);
        NewIcon.shadowSize = new GSize(0, 0);
        NewIcon.infoWindowAnchor = new GPoint(9, 2);
        NewIcon.infoShadowAnchor = new GPoint(20, 20);        
	    	    
	    // Add a marker for the vertice
	    var marker = new GMarker(LatLngForDistanceMarker,
	        {
//	            draggable: true,
//	            bouncy: true,
//	            clickable: true,
	            icon: NewIcon
	        });
	    map.addOverlay(marker);
    }
}

/*
 * Updates the distance display
 */
function UpdateDistanceDisplay()
{
    // Compute distance in kilometers and miles
    var DistanceInMeters=GetCourseDistance();
    var DistanceInKilometers=parseFloat(DistanceInMeters)/1000;
    var DistanceInMiles=DistanceInMeters*0.000621371192; // 1 meter = 0.000621371192 miles
    
    // Format a distance string based on the unit type setting
    var DistanceString="";
    if(UnitOfMeasure==EnglishUnits)
        DistanceString=DistanceInMiles.toFixed(2) + ' mi';
    else if(UnitOfMeasure==MetricUnits)
        DistanceString=DistanceInKilometers.toFixed(2) + ' km';

    // Update the distance display on the page
    var DistanceDisplay=$get('DistanceDisplay');
    var NewDistanceDisplay=document.createTextNode(DistanceString);
    while(DistanceDisplay.firstChild!=null)
        DistanceDisplay.removeChild(DistanceDisplay.firstChild);
    DistanceDisplay.appendChild(NewDistanceDisplay);
    DistanceDisplay.appendChild(document.createElement('div'));
    
    // Distance of cursor from last point
    var DistanceFromLastPointDisplay = $get('DistanceFromLastPoint');
    while(DistanceFromLastPointDisplay.firstChild!=null)
        DistanceFromLastPointDisplay.removeChild(DistanceFromLastPointDisplay.firstChild);

    var DistanceFromLastPoint=0;
    if(PathPoints.length>0)
    {
        var LastPathPointIndex=PathPoints.length-1;
        var DistanceFromLastPointInMeters = PathPoints[LastPathPointIndex].getLocation().distanceFrom(LastCursorPosition);
        var DistanceFromLastPointInKilometers = parseFloat(DistanceFromLastPointInMeters)/1000;
        var DistanceFromLastPointInMiles=DistanceFromLastPointInMeters*0.000621371192; // 1 meter = 0.000621371192 miles
    
        if(UnitOfMeasure==EnglishUnits)
            DistanceString=DistanceFromLastPointInMiles.toFixed(2) + ' mi';
        else if(UnitOfMeasure==MetricUnits)
            DistanceString=DistanceFromLastPointInKilometers.toFixed(2) + ' km';
    
        DistanceFromLastPointDisplay.appendChild(document.createTextNode(DistanceString));
    }
    else
    {
        DistanceFromLastPointDisplay.appendChild(document.createTextNode(UnitOfMeasure==EnglishUnits?'0.00 mi':'0.00 km'));
    }
}

var ShowDistanceMarkers = false;

function ToggleShowDistanceMarkers()
{
    // Enable or disable distance markers
    SetShowDistanceMarkers(!ShowDistanceMarkers)
    
    // Refresh the map
    RefreshMap(LastCursorPosition);
}

/*
 * Enables or disables the display of distance markers on the map
 */
function SetShowDistanceMarkers(NewValue)
{    
    // Update checkbox
    if($get('chkShowDistanceMarker').checked!=NewValue)
        $get('chkShowDistanceMarker').click(); // We can't just check the box because we're using an extender
    else
       ShowDistanceMarkers = NewValue; 
}

var ShowMapMarkers = false;

function ToggleShowMapMarkers()
{
    SetShowMapMarkers(!ShowMapMarkers);
}

/*
 * Enables or disables the display of any markers on the map, be they sprites or path points
 */
function SetShowMapMarkers(NewValue)
{    
    // Update checkbox
    if($get('chkShowMapMarkers').checked!=NewValue)
        $get('chkShowMapMarkers').click(); // We can't just check the box because we're using an extender
    else
    {
        ShowMapMarkers = NewValue; 
        RefreshMap(LastCursorPosition);       
    }
}
