﻿// Map.js

//our global map object
var map;
//var map = null;
//var MCP = new VELatLong(35.911142, -78.934053);
var MCP_Address = "6409 Fayetteville Road, Suite 120 Box 150, Durham, NC, 27713";

//var RDU = new VELatLong(35.87889716542733, -78.78776550292969);
//var Map_Center = new VELatLong(35.885, -78.86);

function load_VE_API() {
    $(document).ready(function() {
        // This event is fired when the DOM is fully loaded
        // Is this browser supported by VE? If not why bother loading a broken control?
        if (!($.browser.msie) && !($.browser.mozilla) && !($.browser.safari)) {
            $("#div_Location_Map").html("Virtual Earth is not supported by your browser.")
        } else {
            // set a nice animated gif to show the map is loading
            //$("#div_Location_Map").addClass("maploading");
            document.getElementById('div_Location_Map').style.background = "url(http://mycorporateportal.blob.core.windows.net/content/z_0/_media/loading/ajax-loader.gif) center center no-repeat"
            if (!($.browser.msie)) {
                //work around for non ie
                $.getScript("http://ecn.dev.virtualearth.net/mapcontrol/v6.2/js/atlascompat.js", "", true);
            }
            $.getScript("http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&onScriptLoad=onscriptload", "", true);
        }
    });
}
function onscriptload() {
    // get rid of our load animation
    //$("#div_Location_Map").removeClass("maploading");
    document.getElementById('div_Location_Map').style.background = "";
    //load the map
    map = new VEMap('div_Location_Map');
    map.onLoadMap = onMapLoaded;
    map.LoadMap(new VELatLong(35.911142, -78.934053), 16, VEMapStyle.Hybrid);
    map.HideDashboard();    
    //map.LoadMap(new VELatLong(-27.47, 153.03), 16, VEMapStyle.Hybrid);
    //do nothing more to the map here, use the onMapLoaded event.
}
function GetMap() {
    map = new VEMap('div_Location_Map');
    map.LoadMap(new VELatLong(35.911142, -78.934053), 16, VEMapStyle.Hybrid);
    map.HideDashboard();

    //map.SetMapStyle(VEMapStyle.Hybrid)
    //map.SetMapStyle(VEMapStyle.Road)
    //map.SetMapStyle(VEMapStyle.Birdseye)

    Add_MCP_Pushpin();
    return;
}
function onMapLoaded() {
    //here we can do what we want with the map object, add data etc.
    Add_MCP_Pushpin();
}

$(window).unload(function() {
    // This event is fired when the web page is closing
    // Time to dispose our map
    if (map) map.Dispose();
});

$.getScript = function(url, callback, cache) {
    $.ajax({
        type: "GET",
        url: url,
        success: callback,
        dataType: "script",
        cache: cache
    });
};

function DisposeMap() {
    if (map) map.Dispose();
}
function Add_MCP_Pushpin() {
    var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(35.911142, -78.934053));
    shape.SetTitle('My Corporate Portal, Inc');
    shape.SetDescription("6409 Fayetteville Road,<br />" +
                         "Suite 120 Box 150,<br />" +
                         "Durham, NC, 27713<br /><br />" +
                         "<img alt=\"Digital Doorway\" src=\"http://mycorporateportal.blob.core.windows.net/content/z_0/_media/logo/MCP_Door_Logo.gif\" style=\"width: 47px; height: 70px;\" />");

    var icon = "<img alt=\"Digital Doorway\" src=\"http://mycorporateportal.blob.core.windows.net/content/z_0/_media/logo/MCP_Door_Icon.gif\" />";
//  var icon = "<div style='font-size:12px;font-weight:bold;border:solid 2px Black;background-color:Aqua;width:50px;'>Custom</div>";
    
    shape.SetCustomIcon(icon);
    map.AddShape(shape);
    return;
}

function Find() {

    map.DeleteRoute();
    SetDirections("");
    map.HideDashboard();
    
    //map.ZoomOut();
    map.LoadMap(new VELatLong(35.911142, -78.934053), 10, VEMapStyle.Hybrid);
    map.ShowDashboard();
    Add_MCP_Pushpin();
    var find_layer = new VEShapeLayer();
    map.AddShapeLayer(find_layer);

    var looking_for = document.getElementById('txtWhat').value;

    map.Find(looking_for,
             MCP_Address,
             null,
             find_layer,
             0,    // Start of Index for paging results
             10, true, true, true, true, null);

}

function Restaurant() {
    
    map.DeleteRoute();
    SetDirections("");
    map.HideDashboard();
    
    map.ZoomOut();
    map.LoadMap(new VELatLong(35.911142, -78.934053), 16, VEMapStyle.Hybrid);
    map.ShowDashboard();
    Add_MCP_Pushpin();
    var find_layer = new VEShapeLayer();
    map.AddShapeLayer(find_layer);

    var looking_for = "Restaurant";
    
    map.Find(looking_for,
             MCP_Address,
             null,
             find_layer,
             null,    // Index
             10, true, true, true, true, null);

}

function Hotel() {

    map.DeleteRoute();
    SetDirections("");
    map.HideDashboard();
    
    map.ZoomOut();
    map.LoadMap(new VELatLong(35.911142, -78.934053), 16, VEMapStyle.Hybrid);
    map.ShowDashboard();
    Add_MCP_Pushpin();
    var find_layer = new VEShapeLayer();
    map.AddShapeLayer(find_layer);

    var looking_for = "hotel";
    
    map.Find(looking_for,
             MCP_Address,
             null,
             find_layer,
             null,    // Index
             10, true, true, true, true, null);

}

function Gas() {

    map.DeleteRoute();
    SetDirections("");
    map.HideDashboard();
    
    map.ZoomOut();
    map.LoadMap(new VELatLong(35.911142, -78.934053), 16, VEMapStyle.Hybrid);
    map.ShowDashboard();
    Add_MCP_Pushpin();
    var find_layer = new VEShapeLayer();
    map.AddShapeLayer(find_layer);

    var looking_for = "gasoline";
    
    map.Find(looking_for,
             MCP_Address,
             null,
             find_layer,
             null,    // Index
             10, true, true, true, true, null);

}

function ClearAll() {
    map.DeleteRoute();
    SetDirections("");
    document.getElementById('txtWhat').value = "";
        
    map.HideDashboard();
    map.LoadMap(new VELatLong(35.911142, -78.934053), 16, VEMapStyle.Hybrid);
    Add_MCP_Pushpin();
}

function GetRouteMap() {        // Route on the Map

    map.LoadMap(new VELatLong(35.885, -78.86), 12, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
    Add_MCP_Pushpin();

    var r_from = new VELatLong(35.87889716542733, -78.78776550292969);
    var r_to = new VELatLong(35.911142, -78.934053);

    var trip_route = new Array(r_from, r_to);

    var options = new VERouteOptions;

    options.DrawRoute = true;
    options.SetBestMapView = false;                          // Can the map adjust to route           
    options.RouteCallback = ShowTurns;                       // Call this function when map route is determined:            
    options.DistanceUnit = VERouteDistanceUnit.Mile;         // Show as miles            
    options.ShowDisambiguation = true;                       // Show the disambiguation dialog

    map.GetDirections(trip_route, options);
}

function ShowTurns(route)      // Text Below the Map
{
    var turns = "<p><b>Directions from Raleigh Durham Intl Airport (RDU)</b>";

    turns += "<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Distance:</b> " + route.Distance.toFixed(1) + " miles";
    turns += "<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time:</b> " + GetTime(route.Time) + "</p><br/>";

    // Unroll route and populate DIV
    var legs = route.RouteLegs;
    var leg = null;
    var turnNum = 0;  // The turn #

    // Get intermediate legs
    for (var i = 0; i < legs.length; i++) {
        leg = legs[i];  // Leg is a VERouteLeg object

        var legNum = i + 1;
        turns += "";

        var turn = null;  // The itinerary leg
        var legDistance = null;  // The distance for this leg

        for (var j = 0; j < leg.Itinerary.Items.length; j++) {
            turnNum++;
            turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
            turns += "<b>" + turnNum + "</b>\t" + turn.Text;
            legDistance = turn.Distance;

            // So we don't show 0.0 for the arrival
            if (legDistance > 0) {
                // Round distances to 1/10ths
                turns += " (" + legDistance.toFixed(1) + " miles";

                // Append time if found
                if (turn.Time != null) {
                    turns += "; " + GetTime(turn.Time);
                }
                turns += ")<br/>";
            }
        }

        turns += "<br/>";
    }

    // Populate DIV with directions
    SetDirections(turns);

}
function GetRouteMap_2() {        // Route on the Map

    map.LoadMap(new VELatLong(35.885, -78.86), 12, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
    Add_MCP_Pushpin();

    var r_from = document.getElementById('txtWhat').value;
    var r_to = new VELatLong(35.911142, -78.934053);

    var trip_route = new Array(r_from, r_to);

    var options = new VERouteOptions;

    options.DrawRoute = true;
    options.SetBestMapView = false;                          // Can the map adjust to route           
    options.RouteCallback = ShowTurns_2;                       // Call this function when map route is determined:            
    options.DistanceUnit = VERouteDistanceUnit.Mile;         // Show as miles            
    options.ShowDisambiguation = true;                       // Show the disambiguation dialog

    map.GetDirections(trip_route, options);
}

function ShowTurns_2(route)      // Text Below the Map
{
    var turns = "<p><b>Directions from your Location</b>";

    turns += "<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Distance:</b> " + route.Distance.toFixed(1) + " miles";
    turns += "<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time:</b> " + GetTime(route.Time) + "</p><br/>";

    // Unroll route and populate DIV
    var legs = route.RouteLegs;
    var leg = null;
    var turnNum = 0;  // The turn #

    // Get intermediate legs
    for (var i = 0; i < legs.length; i++) {
        leg = legs[i];  // Leg is a VERouteLeg object

        var legNum = i + 1;
        turns += "";

        var turn = null;  // The itinerary leg
        var legDistance = null;  // The distance for this leg

        for (var j = 0; j < leg.Itinerary.Items.length; j++) {
            turnNum++;
            turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
            turns += "<b>" + turnNum + "</b>\t" + turn.Text;
            legDistance = turn.Distance;

            // So we don't show 0.0 for the arrival
            if (legDistance > 0) {
                // Round distances to 1/10ths
                turns += " (" + legDistance.toFixed(1) + " miles";

                // Append time if found
                if (turn.Time != null) {
                    turns += "; " + GetTime(turn.Time);
                }
                turns += ")<br/>";
            }
        }

        turns += "<br/>";
    }

    // Populate DIV with directions
    SetDirections(turns);

}
function SetDirections(s) {
    var d = document.getElementById("directions");
    d.innerHTML = s;
}

// time is an integer representing seconds
// returns a formatted string (hr-min)
function GetTime(time) {
    if (time == null) {
        return ("");
    }

    if (time > 60) {                          // if time == 100
        var seconds = time % 60;              // seconds == 40
        var minutes = time - seconds;         // minutes == 60
        minutes = minutes / 60;               // minutes == 1


        if (minutes > 60) {                    // if minutes == 100
            var minLeft = minutes % 60;        // minLeft    == 40
            var hours = minutes - minLeft;     // hours      == 60
            hours = hours / 60;                // hours      == 1

            return (hours + " hour(s), " + minLeft + " minute(s), " + seconds + " second(s)");
        }
        else {
            return (minutes + " minutes, " + seconds + " seconds");
        }
    }
    else {
        return (time + " seconds");
    }
}

function OnPageLoad() {
    var i;
    GetMap();
    loadXML('test.xml');

    var size = xmlObj.childNodes[0].childNodes;
    for (i = 0; i < size.length; i++) {
        var title = xmlObj.childNodes[0].childNodes[i].childNodes[0].childNodes[7].childNodes[0].nodeValue;
        var name = xmlObj.childNodes[0].childNodes[i].childNodes[0].childNodes[4].childNodes[0].nodeValue;
        var pic = "<img src='" + name + "'width='40' height='40'>";
        var lat = xmlObj.childNodes[0].childNodes[i].childNodes[0].childNodes[0].childNodes[0].nodeValue;
        var lon = xmlObj.childNodes[0].childNodes[i].childNodes[0].childNodes[1].childNodes[0].nodeValue;
        AddPushpin(lon, lat, title, pic);
    }
}

function AddPushpin(lat, lon, title, description) {
    var latlong = new VELatLong(lat, lon);
    var shape = new VEShape(VEShapeType.Pushpin, latlong);
    shape.SetTitle(title);
    shape.SetDescription(description);
    map.AddShape(shape);
}

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

