﻿/// <reference path="VeJavaScriptIntellisenseHelper.js" />

        var map = null;

         var LA = new VELatLong(34.306234, -118.464177);

        
        function GetMap()
        {
            map = new VEMap('nethercuttMap');
            map.LoadMap(LA, 14, VEMapStyle.Road, false, VEMapMode.Mode2D, false, 1);


            //create a pushpin for Collection
            var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(34.306234, -118.464177));
            pin.SetTitle("The Nethercutt Collection");
            pin.SetDescription("15200 Bledsoe Street <br />Sylmar, CA 91342");
            map.AddShape(pin);
            
             //create a pushpin for Museum
            var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(34.307138, -118.463206));
            pin.SetTitle("The Nethercutt Museum");
            pin.SetDescription("15151 Bledsoe Street <br />Sylmar, CA 91342");
            map.AddShape(pin);
            
            document.getElementById('txtStart').focus();
            

        }
        
        function GetDirections()
         {  
            //Show loading image
            document.getElementById('ctl00_ContentPlaceHolder1_directionsLoading').style.visibility = 'visible';
            document.getElementById('ctl00_ContentPlaceHolder1_directionsLoading').style.display = 'block';
            
            //GetMap();
            
            var start = document.getElementById('txtStart').value;
            var locations = new Array(start,'15200 Bledsoe Street Sylmar, CA 91342');
            var options = new VERouteOptions;
            
            // Otherwise what's the point?
            options.DrawRoute      = true;

            // So the map doesn't change:
            options.SetBestMapView = true;

            // Call this function when map route is determined:
            options.RouteCallback  = ShowTurns;

            // Show as miles
            options.DistanceUnit   = VERouteDistanceUnit.Mile;

            // Show the disambiguation dialog
            options.ShowDisambiguation = true;


            map.GetDirections(locations,options);
            
           
         }  
    
       
        
        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 intermediate legs
               //for(var i = 0; i < legs.length; i++)
               //{
                  // Get this leg so we don't have to derefernce multiple times
                  leg = legs[0];  // Leg is a VERouteLeg object

                  //var legNum = i + 1;
                  //turns += "<br/><b>Distance for leg " + legNum + ":</b> " + leg.Distance.toFixed(1) + " miles" +
                  //         "<br/><b>Time for leg "     + legNum + ":</b> " + GetTime(leg.Time) + "<br/><br/>";

                  // Unroll each intermediate leg
                  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 += "<div class='turn'><hr/><b><u>" + turnNum + ".</u></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 += ")</div>";
                     }
                  }
                  
                  turns += "<br/><br/><div id='destination'>The Nethercutt Collection <br/> 15151 Bledsoe Street Sylmar, CA 91342</div>";
               //}

               // Populate DIV with directions
               SetDirections(turns);
            
         }

         function SetDirections(s)
         {
            var d = document.getElementById('directions');
            var m = document.getElementById('nethercuttMap');
            var i = document.getElementById('map_input');
            var print = document.getElementById('printLink');
            
            d.innerHTML = s;
            d.style.display = 'block';
            
            m.style.width = '400px';
            m.style.height = '300px';
            i.style.width = '400px';
            print.className = 'printDirections_show';
            
            //Hide loading image
            document.getElementById('ctl00_ContentPlaceHolder1_directionsLoading').style.visibility = 'hidden';
            document.getElementById('ctl00_ContentPlaceHolder1_directionsLoading').style.display = 'none';
            
            
         }

         // 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 ClearAll()
         {
            map.DeleteRoute();
            SetDirections("");
            map.LoadMap(SeattleEastside, 8);
         }
         
         function PrintDirections()
         {
            window.print();
         }
