﻿// JScript File
var map = null;
var operationText = "";
var findPlacesResultLayer = null;

//get map function
function getMap()
{
    map = new VEMap('myMap');
    var psvrcLatLong = new VELatLong(47.20587967809949, -122.48811721801758);
    map.SetCredentials('Ajpr2oRpJakqFzZzLJa03nAHJQ6xM7cFouZxIcAuv9x4zle4YMQobEGko56ieAYh');
    map.LoadMap(psvrcLatLong, 13, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 0);
    var pinPsvrc = new VEShape(VEShapeType.Pushpin, psvrcLatLong);
    map.AddShape(pinPsvrc);
    pinPsvrc.SetTitle('Animal Emergency Clinic');
    pinPsvrc.SetDescription = ('5608 S Durango St, Tacoma, WA 98409 | 253-474-0791');
    findPlacesResultLayer = new VEShapeLayer();
    map.AddShapeLayer(findPlacesResultLayer);
}

//show traffic function
function ShowTraffic(boolShow)
{ 
    if(boolShow)
    {
        map.LoadTraffic(true); 
        map.ShowTrafficLegend(50,50); 
        map.SetTrafficLegendText('Traffic'); 
    }
    else
    {
        map.ClearTraffic();
    }
}

 //find nearby
function FindNearby(searchString) 
{
    try 
    {
    
        map.SetCenterAndZoom(new VELatLong(47.20587967809949, -122.48811721801758), 13);
        if(findPlacesResultLayer) findPlacesResultLayer.DeleteAllShapes();
         
        var results = map.Find(searchString, '98409', null, findPlacesResultLayer, 0, 10, true, true, true, true, ShowFindResults);
        
        document.getElementById('divDirections').style.display = 'block';
        document.getElementById('divPrintDirections').style.display = 'block'; 
        $('#divDirections').addClass('loading');
        scrollDown(); 
    }
    catch(e)  { alert(e.message); }
}

//clear directions
function ResetMap()
{ 
try{
    map.SetCenterAndZoom(new VELatLong(47.20587967809949, -122.48811721801758), 13); 
    map.DeleteRoute(); 
    SetDirections('', '');
    if(findPlacesResultLayer) findPlacesResultLayer.DeleteAllShapes();
    document.getElementById('divDirections').style.display = 'none';
    document.getElementById('divPrintDirections').style.display = 'none'; }
    catch(e) {alert(e.message);}
}


// time is an integer representing seconds
// returns a formatted string
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 ShowTurns(route)
{
    

    var turns = "<p><b>Distance:</b> " + route.Distance.toFixed(1) + " miles";

    turns += "<br/><b>Time:</b> " + GetTime(route.Time) + "</p>";

    
    // Unroll route and populate DIV
    var legs          = route.RouteLegs;
    var leg           = null;
    var turnNum       = 0;  // The turn #

    // Get this leg so we don't have to derefernce multiple times
    leg = legs[0];  // Leg is a VERouteLeg object

    var legNum = i + 1;
            
    var turn        = null;  // The itinerary leg
       
    turns += "<table>";
    
    for(var j = 0; j < leg.Itinerary.Items.length; j ++)
    {
         turnNum++;
         
         turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object

         turns += "<tr><td style='vertical-align: top'><b>" + turnNum + "</b></td><td style='width: 300px'>" + turn.Text + "</td><td>";

         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)";
         }
         turns += "</td></tr>";
    }

    turns += "</table>";
       

    // Populate DIV with directions
    SetDirections(operationText, turns);
}

function ShowFindResults(layer, results, places, moreResults, errorMsg)
{
    operationText = 'Search results:';

    var resultHtml = "<div class='hNav' style='cursor: pointer'><ul>";
    
    // Unroll results and populate DIV  
    for(var i = 0; i < results.length; i++)
    {
        resultHtml += '<li style="text-align: left">';
        resultHtml += '<a onclick="ShowRoute(\'5608 S Durango St, Tacoma, WA\', \'' + results[i].Description + '\'); operationText=\'Directions from The Animal Emergency Clinic to ' + results[i].Name.replace("'", "") + '<p>' + results[i].Name.replace("'", "") + '<br>' + results[i].Description + '</p>\';">';
        resultHtml += '<strong>' + (i + 1) + ". " + results[i].Name.replace("'", "\'") + "</strong><br/>";
        resultHtml += results[i].Description + "<br/>";
        resultHtml += results[i].Phone + "<br/>";
        resultHtml += "</a></li>";
    }
    resultHtml += "</ul></div>";
    // Populate DIV with results
    SetDirections(operationText, resultHtml);
}

function ShowRoute(sAddress, eAddress) {
    try 
    { 
        document.getElementById('divDirections').style.display = 'block';
        document.getElementById('divPrintDirections').style.display = 'block';
        $('#divDirections').addClass('loading');
        
        var options = new VERouteOptions; 
        options.DrawRoute = true; 
        options.SetBestMapView = true; 
        options.RouteCallback  = ShowTurns;
        options.DistanceUnit   = VERouteDistanceUnit.Mile; 
        options.ShowDisambiguation = true;
        map.GetDirections([sAddress, eAddress], options);
        
        scrollDown();
        
       
    } 
    catch(e) { alert(e.message + " " + sAddress + " " + eAddress); }
}

function SetDirections(head, dir) 
{
    var d = document.getElementById('divDirections');
    
    d.innerHTML = "<p style='font-weight: bold'>" + head + "</p>" + dir;
    $('#divDirections').removeClass('loading');
}

//directions to us from any sAddress
function GetHere(sAddress)
{
    operationText='<p><strong>Directions to The Animal Emergency Clinic</strong></p><p>5608 S Durango St, Tacoma, WA 98409<br>253-474-0791</p>'
    ShowRoute(sAddress, '5608 S Durango St, Tacoma, WA');
}
