﻿// JScript File
// microsoft virtual earth support
         var map = null;
         var pinstart = null;
         var pinend = null;
         var pinPoint = null;
         var pinPoint2 = null;
         var pinPoint3 = null;
         var pinPoint4 = null;
         var pinPoint5= null;
         var pinPoint6 = null;
         var pinPixel = null;
         var latLong = null;
         var zoomLevel = 10;
         var routeType = null;
         var index = 0;
         var results = null;
         var showfind = false;
         var showmini = true;
         var showDash = true;
         var i = 0;
         var layer;

  
         function GetMap()
         {
            map = new VEMap('myMap');
            
            map.SetDashboardSize(VEDashboardSize.Normal);
          
            map.LoadMap(new VELatLong(38.688725613296725, -121.18743896484378), 10 );
//            AddPin();   // start
//            AddPin2();  // stop
//            AddPin3();  //lances place
//            AddPin4();  // milestone
//            AddPin5();  // coloma
            map.SetZoomLevel(zoomLevel);
            //txtZoom.value = zoomLevel;
            map.AttachEvent("onclick", GetLatLong);
            latLong = map.GetCenter();
            divInfo.innerHTML = latLong;
            
            ShowMiniMap();
             ShowMiniMap();

            CheckEnableButtons();
 //           AddControl();
         //Creating the layer
//         layer = new VEShapeLayer();
//         map.AddShapeLayer(layer);
            map.SetMapStyle(VEMapStyle.Hybrid);

         }
         function ResetMap()    // go back to center
         {
         // artificial setpoint
            //map.Clear();
            if(pinstart != null)
                map.DeleteShape(pinstart);
            if(pinend != null)
                map.DeleteShape(pinend);
                
            map.DeleteRoute();
                
            pinstart = null;
            pinend = null;
                               
            latLong = new VELatLong(38.688725613296725, -121.18743896484378);
         // set initial zoom   
            zoomLevel = 10;
            //txtZoom.value = zoomLevel;
            map.SetCenterAndZoom(latLong, zoomLevel);
            map.SetMapStyle(VEMapStyle.Hybrid);
            CheckEnableButtons();
          }

        function TextChanging(x)
        {
 //       Button3.SetFocus();
        document.getElementById(x).style.background="gray";
 //       alert("test changed");
        document.getElementById("Button3").Focus();
        }
        
        function KeyBoard(event1)
        {
        //var y = document.getElementById(x).value;
        if(event1.keyCode == 13)
            FindLoc(1);
        }
                  
        function GetLatLong(e)
        {
            //Get the pixel coordinates from the click event, convert to LatLong value
            var x = e.mapX;
            var y = e.mapY;
            pixel = new VEPixel(x, y);
            latLong = map.PixelToLatLong(pixel);
            divInfo.innerHTML = latLong;
            Alert("stop");
        }
        
        function SetCenterAndZoom()
        {   
            map.SetCenterAndZoom(latLong, zoomLevel);
        }
        
        function SetCenter()
        {
            map.SetCenter(latLong);
        }
        
        function SetZoom()
        {
            map.SetZoomLevel(zoomLevel);
        }
        
        function ZoomIn()
        {
            //Increase zoom level by a factor of 1
            map.ZoomIn();
            //txtZoom.value = map.GetZoomLevel();
        }
        
        function ZoomOut()
        {
            //Decrease zoom level by a factor of 1
            map.ZoomOut();
            //txtZoom.value = map.GetZoomLevel();
        }
        
        function ValidateEntry()
        {
           //Check to make sure zoom level is within range
           if (txtZoom.value > 0 && txtZoom.value <= 19) zoomLevel = txtZoom.value;
            else alert("Enter a value between 1 and 19.");
        }
         function getInfo()
         {
            if (map.IsBirdseyeAvailable())
            {
                var be = map.GetBirdseyeScene();
                var info = "ID: " + be.GetID() + "\n";
                info += "thumbnail: " + be.GetThumbnailFilename()+ "\n";
                info += "orientation: " + be.GetOrientation()+ "\n";
                info += "height: " + be.GetHeight() + "\n";
                info += "width: " + be.GetWidth() + "\n";
                var pixel = be.LatLongToPixel(map.GetCenter(), map.GetZoomLevel());
                info += "LatLongToPixel: " + pixel.x + ", " + pixel.y + "\n";

                // Check to see if the current birdseye view contains the pushpin pixel point.
                info += "contains pixel " + pinPixel.x + ", " + pinPixel.y + ": " + be.ContainsPixel(pinPixel.x, pinPixel.y, map.GetZoomLevel()) + "\n";
                
                // Check to see if the current view contains the pushpin LatLong.
                info += "contains latlong " + pinPoint + ": " + be.ContainsLatLong(pinPoint) + "\n";
                
                // This method may return null, depending on the selected view and map style.
                info += "latlong: " + map.PixelToLatLong(pixel);
                alert(info);
            }
            else
            {
                alert("Bird's eye images are not available at this location and zoom level.");
            }
         }

        function FindLoc(numResults)
        {
            try
            {
                results = map.Find(txtWhat.value,txtWhere.value,null,layer,index,numResults,true,true,true,true,MoreResults);
                index = parseInt(index)+9;
 //               shape = new VEShape(VEShapeType.Pushpin, map.GetCenter());
 //               shape.SetTitle('My pushpin');
 //               shape.SetDescription('This is a pushpin.');
 //               layer.AddShape(shape); 
            }
            catch(e)
            {
                alert(e.message);
            }
        }
        
        function MoreResults(a,b,c,d,e)
        {
            if(d!=false)
            {
                var r = "<a href='#' onclick='javascript:FindLoc(parseInt(txtNumResults.value));'>Click for More Results</a>";
                document.getElementById('results').innerHTML = r;
            }
            else
            {
                index=0;
                number=Number(txtNumResults.value);    
                document.getElementById('results').innerHTML = "";
                document.getElementById('results').innerHTML = "No More Results Available";
            }
        }
 
         function CheckEnableButtons()
         {
            if(pinstart != null && pinend != null)
            {
            Button1.disabled = ""; //disabled="disabled"
            Button2.disabled = ""; //disabled="disabled"
            }
            else
            {
            Button1.disabled = "disabled";
            Button2.disabled = "disabled";
            }
        
         }
         
        function SetStartPoint()
        {
//            latLong = new VELatLong(38.70426686648779, -120.84317207336427);
            pinPoint = latLong;
            if(pinstart != null)
                map.DeleteShape(pinstart);
            pinstart = map.AddPushpin(pinPoint);
            pinstart.SetTitle('Start Point');
            pinstart.SetDescription('Push pin 1');
            CheckEnableButtons();
        }
        function SetEndPoint()
        {
//            latLong = new VELatLong(38.90332398953275, -120.91827392578125);
            pinPoint2 = latLong;
            if(pinend != null)
                map.DeleteShape(pinend);
            pinend = map.AddPushpin(pinPoint2);
            pinend.SetTitle('End Point');
            pinend.SetDescription('Push pin 2');
            CheckEnableButtons();
        }
         
         function AddPin()
         {
            // Add a new pushpin to the center of the map.
            latLong = new VELatLong(38.70426686648779, -120.84317207336427);
            pinPoint = latLong;
            pinPixel = map.LatLongToPixel(pinPoint);
            var pin = map.AddPushpin(pinPoint);
            pin.SetTitle('PJs bar - final stop');
            pin.SetDescription('Biker road house party.');
         }
         function AddPin2()
         {
            // Add a new pushpin to the center of the map.
            latLong = new VELatLong(38.90599569999115, -120.838623046875);
            pinPoint2 = latLong;
            pinPixel = map.LatLongToPixel(pinPoint2);
            var pin = map.AddPushpin(pinPoint2);
            pin.SetTitle('Miners Club Bar - Starting Point');
            pin.SetDescription('Georgetown,CA ');
         }
         function AddPin3() //lances
         {
            // Add a new pushpin to the center of the map.
            latLong = new VELatLong(38.90332398953275, -120.91827392578125);
            pinPoint3 = latLong;
            pinPixel = map.LatLongToPixel(pinPoint3);
            var pin = map.AddPushpin(pinPoint3);
            pin.SetTitle('Lances Place Restaurant - stop 1');
            pin.SetDescription('Greenwood,CA');
         }
         function AddPin4() //milestone
         {
            // Add a new pushpin to the center of the map.
            latLong = new VELatLong(38.89370499941828, -121.0150909423828);
            pinPoint4 = latLong;
            pinPixel = map.LatLongToPixel(pinPoint4);
            var pin = map.AddPushpin(pinPoint4);
            pin.SetTitle('Milestone Saloon - stop 2');
            pin.SetDescription('Cool,CA');
         }
         function AddPin5() // coloma club
         {
            // Add a new pushpin to the center of the map.
            latLong = new VELatLong(38.80065426993302, -120.89973449707031);
            pinPoint5 = latLong;
            pinPixel = map.LatLongToPixel(pinPoint5);
            var pin = map.AddPushpin(pinPoint5);
            pin.SetTitle('Coloma Club - stop 3');
            pin.SetDescription('Coloma,CA');
         }
         function AddPin6()
         {
            // Add a new pushpin to the center of the map.
            latLong = new VELatLong(38.90599569999115, -120.838623046875);
            pinPoint6 = latLong;
            pinPixel = map.LatLongToPixel(pinPoint6);
            var pin = map.AddPushpin(pinPoint6);
            pin.SetTitle('Georgetown - This is the title text');
            pin.SetDescription('Miners Club Bar - This is the description text.');
         }

        function GetQuickestRoute()
        {
        routeType = VERouteType.Quickest;
        map.GetRoute(pinPoint, pinPoint2, VEDistanceUnit.Miles, routeType, myCallback);
        }
        function GetShortestRoute()
        {
        routeType = VERouteType.Shortest;
        map.GetRoute(pinPoint // start
            , pinPoint2 //stop
            , VEDistanceUnit.Miles, routeType, myCallback2);
        }
         
        function DeleteRoute()
        {
            map.DeleteRoute();
        }
        
        function myCallback(route)
        {
           // alert("The callback quickest route function has been called.");
           var routeinfo="Route info:\n\n";
           routeinfo+="Total distance: ";
           routeinfo+= route.Itinerary.Distance+" ";
           routeinfo+=   route.Itinerary.DistanceUnit+"\n";
           var steps="";
           var len = route.Itinerary.Segments.length;
           for(var i = 0; i < len ;i++)
              {
              steps+=route.Itinerary.Segments[i].Instruction+" -- (";
              steps+=route.Itinerary.Segments[i].Distance+") ";
              steps+=route.Itinerary.DistanceUnit+"\n";
              }
              routeinfo+="Steps:\n"+steps;
  
           divRoute1.innerHTML="Quickest route calculated";
           TextArea1.value = "Quickest route calculated - " + routeinfo;
             // alert(routeinfo);   
        }   
        function myCallback2(route)
        {
           //alert("The callback shortest route function has been called.");
           var routeinfo="Route info:\n\n";
           routeinfo+="Total distance: ";
           routeinfo+= route.Itinerary.Distance+" ";
           routeinfo+=   route.Itinerary.DistanceUnit+"\n";
           var steps="";
           var len = route.Itinerary.Segments.length;
           for(var i = 0; i < len ;i++)
              {
              steps+=route.Itinerary.Segments[i].Instruction+" -- (";
              steps+=route.Itinerary.Segments[i].Distance+") ";
              steps+=route.Itinerary.DistanceUnit+"\n";
              }
              routeinfo+="Steps:\n"+steps;
//              alert(routeinfo);   
            TextArea1.value = "Shortest route calculated - " + routeinfo;
           divRoute1.innerHTML="Shortest route calculated";
        }   
        
        function ShowDisambiguationDialog(v)
        {
            map.ShowDisambiguationDialog(v);
        }  
         // Find Control hide & show calls
         function HideFindControl()
         {
            map.HideFindControl();
         }
         
         function ShowFindControl()
         {

         if(showfind)
            {
            showfind = false;
            map.HideFindControl();
            }
         else
            {
            showfind = true;
            map.ShowFindControl();
            }
         }
         
         // Mini Map hide & show calls
         function HideMiniMap()
         {
            map.HideMiniMap();
         }
         
         function ShowMiniMap()
         {
         if(!showmini)
            {
            if (map.GetMapMode() == 2)
                {
                    map.SetMapMode(VEMapMode.Mode2D);
                }
            map.ShowMiniMap(480, 80);
            showmini = true;
            }
           else
           {
           HideMiniMap();
           showmini = false;
          }            
         }

         function ShowDashBoard()
         {
         if(!showDash)
            {
            map.ShowDashboard();
            showDash = true;
            }
           else
           {
           map.HideDashBoard();
           showDash = false;
          }            
         }
         function ToggleDashBoard()
         {
         if(!showDash)
            {
            map.ShowDashboard();
            showDash = true;
            }
           else
           {
           map.HideDashboard();
           showDash = false;
          }            
         }
         
                  // Mini Map hide & show calls
         function HideDashBoard()
         {
            map.HideDashBoard();
         }

         function LoadNormal()
         {
            //Dispose the map if it was previously loaded.
            if (map!=null)
            {
                 map.Dispose();
            }
            map = new VEMap('myMap');
            map.SetDashboardSize(VEDashboardSize.Normal);
            map.LoadMap();
         }
         function LoadSmall()
         {
            //Dispose the map if it was previously loaded.
            if (map!=null)
            {
                 map.Dispose();
            }
            map = new VEMap('myMap');
            map.SetDashboardSize(VEDashboardSize.Small);
            map.LoadMap();
         }
         function LoadTiny()
         {
            //Dispose the map if it was previously loaded.
            if (map!=null)
            {
                 map.Dispose();
            }
            map = new VEMap('myMap');
            map.SetDashboardSize(VEDashboardSize.Tiny);
            map.LoadMap();
         }

         function ShowDashboard()
         {
            map.ShowDashboard();
         }
         function HideDashboard()
         {
            map.HideDashboard();
         }

        function AddControl()
         {
            var el = document.createElement("div"); 
            el.id = "myControl" + i; 
            el.style.top = 100 + (i * 10); 
            el.style.left = 100 + (i * 10);            
            el.style.border = "2px solid black";
            el.style.background = "White";
            el.innerHTML = el.id;  
            map.AddControl(el);
            addShim(el);
            counter.value = i;
            i++;
         }
         
         function addShim(el)
         {
            var shim = document.createElement("iframe");
            shim.id = "myShim" + i;
            shim.frameBorder = "0";
            shim.style.position = "absolute";
            shim.style.zIndex = "1";
            shim.style.top  = el.offsetTop;
            shim.style.left = el.offsetLeft;
            shim.width  = el.offsetWidth;
            shim.height = el.offsetHeight;
            el.shimElement = shim;
            el.parentNode.insertBefore(shim, el);
         }
         
         function DeleteControl()
         {
            var myControl = document.getElementById("myControl" + counter.value);
            if (myControl!=null)
            {
                var myControlID = myControl.id;
                map.DeleteControl(myControl);
                alert("Control " + myControlID + " has been deleted.");
            }
            else
            {
                alert("No control with ID " + counter.value + " was found.");
                return;
            }
            
            var myShim = document.getElementById("myShim" + counter.value);
            if (myShim!=null) myShim.parentNode.removeChild(myShim);
            myShim = null;
            i--;
            counter.value = i;
         }
         
         function HideControl()
         {
            var myControl = document.getElementById("myControl" + counter.value);

            if (myControl!=null)
            {
                map.HideControl(myControl);
                alert(myControl.id + " is now hidden from view.");
            }
            else
            {
                alert("No control with ID " + counter.value + " was found.");
                return;
            }
         }
         
         function ShowControl()
         {
            var myControl = document.getElementById("myControl" + counter.value);
            if (myControl!=null)
            {
                map.ShowControl(myControl);
                alert(myControl.id + " has been restored.");
            }
            else
            {
                alert("No control with ID " + counter.value + " was found.");
                return;
            }
         }
    


