﻿/*Javascript document*/
             var map = null;
             var pinPoint = null;
             var pinPixel = null;
                      
             function GetMap(lat, lon, address, sales, service, token, mapDiv, mapType)
             {  
                
                var LL = new VELatLong(lat,lon, 0, VEAltitudeMode.Default);
                map = new VEMap(mapDiv);
                                
                map.SetClientToken(token);

                if(mapType == "detail")
                {
                    map.LoadMap(LL, 14, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
                }
                else if(mapType == "directions")
                { 
                        $get("PanelGetPostcode").style.display = "none";
                        
                      var userPostcode = $get("userPostcode").value;
                      if(userPostcode != '')
                      {
                        
                       $get("ShowRouteLegsDiv").style.display = "block";
                    
                        map.onLoadMap = function(){GetDirections(lat, lon, userPostcode , sales, service, token, mapDiv, mapType);
                        $get("routeLoading").style.display = "block";
                         };
                       
                        map.LoadMap(LL, 7, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
                         $get("routeMap").style.display = "none";
                      }
                      else
                      {
                      alert("Please enter the postcode from which you will be travelling");
                      return;
                      }
                 }
                    
                    //get the dealer name and address from address
                     title = address.substring(0, address.indexOf(","));
                     address = address.substring(address.indexOf(",")+1, address.length);
                    //Set custom shape for pushpin
                     var shape = new VEShape(VEShapeType.Pushpin,LL);
                     //define which pin to use
                     if(sales == 'True' && service == 'False'){
                    shape.SetCustomIcon("<div class='salesPin'><div class='text'></div></div>"); 
                    }
                    if(service == 'True' && sales == 'False'){
                     shape.SetCustomIcon("<div class='servicePin'><div class='text'></div></div>");
                     }
                     if(sales == 'True' && service == 'True'){
                     shape.SetCustomIcon("<div class='salesServicePin'><div class='text'></div></div>");
                     }
                      shape.SetTitle(title);
                      shape.SetDescription(address);
                      map.AddShape(shape);

                
             }
             
             
             
             function ReturnPage()
             {
             
              document.getElementById("ctl00_screen_css").href = "css/screen.css";
             }
             
             function PrintDetails()
             {
                document.getElementById("ctl00_screen_css").href = "css/print.css";   
              
             //get the new dealerInfo div
             var printDealerInfo = $get("printDealerInfo");
             //get the printRouteMap div
             var printRouteMap = $get("printRouteMap");
             //get the printFeatures div
             var printRouteLegs =  $get("printRouteLegs") ;
                
             //get the details to be written
             var dealerDetails = $get("dealer_info").innerHTML;
             
             
             //check whether maps are visible
             if($get("detailMap") != null)
             {
                 var routeDetails = $get("ShowRouteLegsDiv").innerHTML;
                 //find the popupExtender and show
                 $find("mpePrintPanelExtender").show();
                 var tab1 =  $get("ctl00_ContentPlaceHolder1_DealerDetailsControl1_ctl01_tabContainer1_tabDetailMap");
                 
                 //check if detailMap is visible tab
                 if(tab1.style.visibility == "visible")
                 {
                 var printMap = new VEPrintableMap("detailMap", "printRouteMap", map);
                 printMap.Print();
                 }
                 else
                 {
                  printRouteMap.innerHTML = "";
                 }
                 
                 
                 
                 //check whether a route search has been done
                 if($get("routeMap").className == "MSVE_MapContainer")
                 {
                 //this calls the veprintfix to give us a printable map
                 printMap = new VEPrintableMap("routeMap", "printRouteMap", map);
                 printMap.Print();
                 printRouteLegs.innerHTML = routeDetails;   
                 }

                 //put them all in the corresponding divs inside the modal
                 printDealerInfo.innerHTML = "<h3 class='printHeading'>Alfa Romeo Dealer Details</h3>" + dealerDetails;
                  
                  for(var i = 0; i < $("div").length; i++)
                  {
                   
                   if($("div")[i].id == "printArea")
                   {    
                        
                        alert($("div")[i].id);
                        for(var j = 0; j < $("div")[i].childNodes.length; j++)
                        {
                        $("div").css({display:"none"});
                       
                        }
                   }
//                   else
//                   {
//                    $(this).css({display:"block"});
//                   }
                  }
                     
                window.print();
                
               
                 
             }
             
               else
               {
               window.print();
                
               }
            
             
            } 
            
           
            
            function hideModal()
            {
            $find("mpePrintPanelExtender").hide();
            }         
             
               

               function GetDirections(lat, lon, address, sales, service, token, mapDiv, mapType)
                {
               
                 var LL = new VELatLong(lat,lon, 0, VEAltitudeMode.Default);
                 var myOptions = new VERouteOptions();
                 myOptions.DrawRoute      = true;
                 myOptions.SetBestMapView = true; 
                 myOptions.DistanceUnit   = VERouteDistanceUnit.Mile;
                 myOptions.ShowDisambiguation = false;
                 myOptions.RouteCallback = OnGotRoute;
                 var locations = new Array();
                  
                 locations.push(address);
                 locations.push(LL);
                 
                 map.GetDirections(locations, myOptions);
                
                
          
                }
          
          
            function OnGotRoute(route)         
             {
             $get("routeMap").style.display = "block";
             $get("routeLoading").style.display = "none";
             $get("ShowRouteLegsDiv").className = "routeDetails";           
             $get("ShowRouteLegsDiv").parentNode.style.overflowY = "scroll";           
              // Unroll route           
              var legs  = route.RouteLegs;           
              var turns = "<div class='routeDetailsTop'>";
              turns += "<h3 class='routeDetailsHeading'>Total distance: "  + route.Distance.toFixed(1) + " miles</h3></div>";           
              var numTurns = 0;           
              var leg      = null;           
              // 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[i];  // Leg is a VERouteLeg object                                 
               // Unroll each intermediate leg               
               var turn = null;  // The itinerary leg                                 
               for(var j = 0; j < leg.Itinerary.Items.length; j ++)              
               {                  
                turn =  leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object                  
                numTurns++;                  
                turns += "<div class='routeDirection'><span class='routeTurnNumber'>" + numTurns + ".</span>\t<span class='routeTurnText'>" + turn.Text + " (" + turn.Distance.toFixed(1) + " mi)</span></div>";               
               }            
              } 
                
              
              $get("ShowRouteLegsDiv").innerHTML =  turns ;            
                   

             }

             function getInfo()
             {
                var info;

                if (map.IsBirdseyeAvailable())
                {
                    var be = map.GetBirdseyeScene();

                    info  = 'ID: '          + be.GetID() + '\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
                {
                    var center = map.GetCenter();

                    info  = 'Zoom level:\t' + map.GetZoomLevel() + '\n';
                    info += 'Latitude:\t'   + center.Latitude    + '\n';
                    info += 'Longitude:\t'  + center.Longitude;

                    alert(info);
                }
             }
             
             function AddPin()
             {
                // Add a new pushpin to the center of the map.
                pinPoint = map.GetCenter();
                pinPixel = map.LatLongToPixel(pinPoint);
                map.AddPushpin(pinPoint);
             }