﻿        var map = null;
        var CrestronShape;
        var MyLocationShape;
        var RouteFound;
        var AllowRouteFinder;
        
        function GetMap()
        {
            map = new VEMap('CrestronMap');
            map.LoadMap(new VELatLong(51.340455, -0.397580), 15 ,'r' ,false);
            map.AttachEvent("onendpan", AddPushpin);
            
            AddCrestronPushpin();
            RouteFound = false;
            AllowRouteFinder = false;
        } 

        function AddCrestronPushpin()
        {
            if (CrestronShape == null) 
            {
                CrestronShape = new VEShape(VEShapeType.Pushpin, new VELatLong(51.340455, -0.397580));
                CrestronShape.SetTitle('Crestron UK Ltd');
                CrestronShape.SetDescription('Munro House<br />Portsmouth Road<br />Cobham<br />Surrey<br />KT11 1TF<br />U.K.');
                map.AddShape(CrestronShape);
            }
        }
        
        function AddPushpin()
        {
            if(RouteFound==false && AllowRouteFinder==true)
            {
                DeleteAllShape();
                MyLocationShape = new VEShape(VEShapeType.Pushpin, map.GetCenter());
                MyLocationShape.SetTitle('My Location');
                MyLocationShape.SetDescription('This is my location');
                map.AddShape(MyLocationShape);
            }
        }
        
        function DeleteAllShape()
        {
            if(MyLocationShape != null)
            {
                map.DeleteShape(MyLocationShape);
            }
        }

        function GetRouteToCrestron()
        {
            GetRoute(MyLocationShape, CrestronShape);
        }
        
        function GetRouteFromCrestron()
        {
            GetRoute(CrestronShape, MyLocationShape);
        }
        
        function GetRoute(FromRoute, ToRoute) 
        {
            if (CrestronShape != null && MyLocationShape != null) 
            {
                ShowWaitingMsg();
                map.GetRoute(FromRoute.GetPoints()[0], ToRoute.GetPoints()[0], null, null, onGotRoute);
                RouteFound = true;
            }
        }
        
        
        function onGotRoute(route)
        {
            var routeinfo="<p><div style='float:right;'><b>Total distance:</b> ";
            routeinfo+= route.Itinerary.Distance+" ";
            routeinfo+= route.Itinerary.DistanceUnit + "</div>";

            var steps="";
            var len = route.Itinerary.Segments.length;
            for(var i = 1; i < len ;i++)
            {
                if (i == (len-1))
                {
                    steps+="</ol><div class='map_dest'><div style='float:right;'>" + route.Itinerary.Segments[i].Distance + " " + route.Itinerary.DistanceUnit + "</div>";
                    steps+=route.Itinerary.Segments[i].Instruction + " your destination</div>";
                }
                else if(i == 1)
                {
                    steps+="<div class='map_arriv'><div style='float:right;'>" + route.Itinerary.Segments[i].Distance + " " + route.Itinerary.DistanceUnit + "</div>";
                    steps+=route.Itinerary.Segments[i].Instruction + "</div>";
                    steps+="<ol>";
                }
                else
                {
                    steps+="<li style='background-color:" + (i%2 == 0 ? "#FFFFFF" : "#F9F9F9") + "'><div style='float:right;'>" + route.Itinerary.Segments[i].Distance + " " + route.Itinerary.DistanceUnit + "</div>";
                    steps+=route.Itinerary.Segments[i].Instruction + "</li>";
                }
            }
            routeinfo+="<b>Directions:</b></p>"+steps;
            
            PrintOnScreen("RouteDirections", routeinfo);
            HideWaitingMsg();
        }
        
        
        
        function MakeNewRoute()
        {
            RouteFound = false;
            PrintOnScreen("RouteDirections", "");
            try
            {
                map.DeleteRoute();
            }
            catch (err) { }
            HideWaitingMsg();
        }

        function PrintOnScreen(divID, sText)
        {
            var RouteDirectionsDiv = document.getElementById(divID);
            RouteDirectionsDiv.innerHTML = sText;
        }
        
        function ShowWaitingMsg()
        {
            PrintOnScreen("WaitingMsg", "please wait");
        }

        function HideWaitingMsg()
        {
            PrintOnScreen("WaitingMsg", "");
        }

        function StopRouteFinder()
        {
            AllowRouteFinder = false;
            DeleteAllShape();
        }
        
        function StartRouteFinder()
        {
            AllowRouteFinder = true;
        }
