﻿        var map = null;
        var SmartcommShape;
        var MyLocationShape;
        var RouteFound;
        var AllowRouteFinder;
        
        function GetMap()
        {
            map = new VEMap('SmartcommMap');
            map.LoadMap(new VELatLong(51.621886, -0.768229), 15 ,'r' ,false);
            map.AttachEvent("onendpan", AddPushpin);
            
            AddSmartcommPushpin();
            RouteFound = false;
            AllowRouteFinder = false;
        } 

        function AddSmartcommPushpin()
        {
            if (SmartcommShape == null) 
            {
                SmartcommShape = new VEShape(VEShapeType.Pushpin, new VELatLong(51.621886, -0.768229));
                SmartcommShape.SetTitle('SmartComm Ltd');
                SmartcommShape.SetDescription('3 Barnes Wallis Court <br />Wellington Road <br />High Wycombe<br />HP12 3PS<br />U.K.<br />');
                map.AddShape(SmartcommShape);
            }
        }
        
        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 GetRouteToSmartcomm()
        {
            GetRoute(MyLocationShape, SmartcommShape);
        }
        
        function GetRouteFromSmartcomm()
        {
            GetRoute(SmartcommShape, MyLocationShape);
        }
        
        function GetRoute(FromRoute, ToRoute) 
        {
            if (SmartcommShape != null && MyLocationShape != null) 
            {
                ShowWaitingMsg();
                map.GetRoute(FromRoute.GetPoints()[0], ToRoute.GetPoints()[0], null, null, onGotRoute);
                RouteFound = true;
            }
        }
        
        
        function onGotRoute(route)
        {
            
        
            var routeinfo="<div class='map_header'><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 class='" + (i%2 == 0 ? "map_point" : "map_alt_point") + "'><div style='float:right;'>" + route.Itinerary.Segments[i].Distance + " " + route.Itinerary.DistanceUnit + "</div>";
                    steps+=route.Itinerary.Segments[i].Instruction + "</li>";
                }
            }
            routeinfo+="<b>Directions:</b></div>"+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", "<img src='/images/loading.gif' width='56' height='21' alt='Please wait...' />");
        }

        function HideWaitingMsg()
        {
            PrintOnScreen("WaitingMsg", "");
        }

        function StopRouteFinder()
        {
            AllowRouteFinder = false;
            DeleteAllShape();
        }
        
        function StartRouteFinder()
        {
            AllowRouteFinder = true;
        }
