﻿/*
    The functions contained within this script are used for 
    functionality in the Trip Time Calculator mini-application.
    
    The Virtual Earth SDK is used under license from Microsoft.
*/
var map = null;

function mapInit(){
    map = new VEMap('myMap');
    map.LoadMap();
    map.HideDashboard();
}

function GetMap(origin,destination){            
    if(map!=null){ mapInit() }
    else{ map.Clear() };
    var options = new VERouteOptions();            
    options.RouteCallback = onGotRoute;            
    map.GetDirections([origin, destination],options);         
}

function onGotRoute(route){
    // Unroll route
    var routeDistance = route.Distance.toFixed(1);
    var legs     = route.RouteLegs;
    var turns    = "Total distance: " + routeDistance + " mi<br />";
    var numTurns = 0;
    var leg      = null;

    // Get intermediate legs
    for(var i = 0; i < legs.length; i++){
        // Get this leg so we don't have to derefernce multiple times
        leg = legs[i];  // Leg is a VERouteLeg object

        // Unroll each intermediate leg
        var turn = null;  // The itinerary leg

        for(var j = 0; j < leg.Itinerary.Items.length; j ++){
            turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
            numTurns++;
            turns += numTurns + ".\t" + turn.Text + " (" + turn.Distance.toFixed(1) + " mi)<br />";
        }
    }

    // The following are JavaScript calls made using the jQuery framework. 
    $("#myDirections").html(turns); 
    
    $.post("../Tools/_ajax_TripCalculator.aspx",{TotalMiles:routeDistance},function(data){
        $("#tripSummary").html(data);
    });
}