﻿//Driving Direction related functions
//
// All rights are reserved. Reproduction or transmission in whole or in part, in 
// any form or by any means, electronic, mechanical or otherwise, is prohibited 
// without the prior written consent of the copyright owner.
// 
// Filename: PDrivingDirections.js
//



//default unit is miles
var DrivingType = "mi";
var DrivingUnit = VEDistanceUnit.Miles;
var isDrivingFrom;

var originalCtr;
var originalZoom;

function GenerateDrivingDirectionPanel(address, pinLat, pinLon)
{
    var drivingDirections="";
    var getDirections="";    
    
    var driveCity=document.getElementById('city').value;    

    getDirections="<br /><div id='getDirections' class='directionsPopup' style='display:none;' onclick='void(0);'>";                       
    getDirections+="<table width='170px' style='color:black;'>";
    getDirections+="<tr><td id='AddressLabel'></td></tr>";
    getDirections+="<tr><td><textarea col='' style='width: 90%' wrap='soft' rows='3' value='' id='inputAddress' value=''></textarea></td></tr>";
    getDirections+="<tr><td><input class='radio' type='radio' id='optRouteS' name='routeType' checked>Shortest</input>";
    getDirections+="<input class='radio' type='radio' id='optRouteQ' name='routeType'>Quickest</input></td></tr>";
   
    var getDirectionButton = "";
    getDirectionButton += "<tr><td><input type='button' value='Get Directions' class='button GetDirectionButton' onclick=\"ShowRoute('"+address+"'," + pinLat + "," + pinLon + ");\">";
    getDirectionButton+=" <input type='button' value='close' class='button' onclick=\"HideDirectionsPopup();\"></td></tr>";
    getDirectionButton+="</table></div>";
    
    return getDirections + getDirectionButton;
}

function getDirections(direction){

    isDrivingFrom = direction;
     //get a reference to the form
    var directionsForm=document.getElementById("getDirections");
    if(direction == 0){
        document.getElementById('AddressLabel').innerHTML="<b>Enter Start Address</b>";        
    }
    else{
        document.getElementById('AddressLabel').innerHTML="<b>Enter End Address</b>";      
    }
    directionsForm.style.display="block";
}

//Function to retrieve driving directions.
function ShowRoute(address, latitude, longitude){
    var startAddress;
    var endAddress;
     var RouteType;
    var latLong = new VELatLong(latitude, longitude);
    //get a reference to the description form
    var directionsForm=document.getElementById("getDirections");
    
    //hide the form
    directionsForm.style.display="none";      
    
    //get the fields on the form
    var inputAddress =document.getElementById("inputAddress").value;   
    
    //exit if the form is missing an address
    if (inputAddress =="" )
        return;    
    
    //set request pending display, get the current view, and get the route information    
    originalCtr=map.GetCenter();
    originalZoom=map.GetZoomLevel();
    
    
    if (document.getElementById("optRouteS").checked)
    {
        RouteType=VERouteType.Shortest;
    }
    if (document.getElementById("optRouteQ").checked)
    {
        RouteType=VERouteType.Quickest;
    }
    
    if(isDrivingFrom == 0){
        startAddress = inputAddress;
        endAddress = address;
        map.GetRoute(inputAddress,latLong,VEDistanceUnit.Miles,RouteType,onGotRoute);
    }
    else if(isDrivingFrom == 1){
        startAddress = address;
        endAddress = inputAddress;
        map.GetRoute(latLong,inputAddress,VEDistanceUnit.Miles,null,onGotRoute);
    }

    function onGotRoute(route){
           
        //set route summary       
        if (route == null)
        {
            // Failed to generate driving route. Possibly the user entered an invalid address.
            alert("There is no driving direction available.");
            return;
        }
        if (route.StartLocation.Address != "")
            startAddress = route.StartLocation.Address;
        if (route.EndLocation.Address != "")
            endAddress = route.EndLocation.Address;
            
        var summary="<table>";
        summary+= "<tr><td><b>From:</b></td><td>" +startAddress+"</td></tr>";
        summary+= "<tr><td><b>To:</b></td><td>"+endAddress+"</td></tr>";
        summary+= "<tr><td><b>Trip:</b></td><td>"+route.Itinerary.Distance+" "+route.Itinerary.DistanceUnit + ", " + route.Itinerary.Time + "</td></tr>";
        summary+= "</table><br/>";
    
        //build route information
               
        var start="<img src='Images/directions_play_icon.gif'/> ";
        start+="Start at " + startAddress+". ";
        start+=route.Itinerary.Segments[1].Instruction+" -- "; 
        start+=route.Itinerary.Segments[1].Distance+" "+route.Itinerary.DistanceUnit;
        
        var steps="<table id='drivingInstructions' class='drivingInstructions' cellpadding='5px'>";      
        
        var len=route.Itinerary.Segments.length;
        for(var i=2; i<len-1; i++){
            var j=i-1; 
            var bgColor="Transparent";
            if(j%2 == 1)
                bgColor = "#53687B";
            var pushpin = "<img class='pushpinIcon' src='Images/Pins/redcircle" + j + ".GIF'/> ";
            steps+="<tr style='background-color:"+bgColor+";'><td>"+pushpin+" "+route.Itinerary.Segments[i].Instruction+"</td>";
            steps+="<td>"+route.Itinerary.Segments[i].Distance+" "+route.Itinerary.DistanceUnit+"</td></tr>";
        }
        steps+="</table>";
        var end="<img src='images/directions_stop_icon.gif'/> ";
        end+= "Arrive at " + endAddress +" -- ";
        end+=route.Itinerary.Segments[len-1].Distance+" "+route.Itinerary.DistanceUnit;
        
        // set route summary info        
        document.getElementById("routeSummary").innerHTML=summary;  
           
        //set route beginning and end points
        document.getElementById("startRoute").innerHTML=start;   
        document.getElementById("endRoute").innerHTML=end;   
        
        //set route information       
        document.getElementById("directionsSteps").innerHTML=steps;
        
         //hide the search result layer, show the driving instructions
        document.getElementById("SearchPanel").style.display="none";        
      
        document.getElementById("RoutePanel").style.display="block";
        //Zoom out one more level to accommodate the pins covered by the direction panel
        map.SetZoomLevel(map.GetZoomLevel() - 1);
        
        //Set customized pushpins        
        var pinarray = new Array();
        var layer = new VEShapeLayer();
        layer.SetTitle("RoutePushPinLayer");
        layer.SetDescription("This is a route pushpin layer");
        map.AddShapeLayer(layer);
        
        //Track current direction number for multiple routes
		for(var i=0; i<map._dm.veroutecache.length;i++)
		{
			var shape = new VEShape(VEShapeType.Pushpin,map._dm.veroutecache[i].LatLong);			
			var popupBGImage = "<div class='pushpinBG'><img src='Images/pushpinBG.png' height='100%' width='100%' alt=''/></div>";
            var padding = "<div style='background: transparent; height:30px'></div>";
            
            var description = "<div class='routePopupBox'>" + padding+ "<b>" + map._dm.veroutecache[i].Title + "</b><br/><br/>" + map._dm.veroutecache[i].Details + "</div>";
			shape.SetDescription(popupBGImage + description);

			//Place start and end flags, otherwise generate customized pins
			if(i == 0)
			{   
				shape.SetCustomIcon("<img src='Images/directions_play_icon.gif'>");
					
			}
			else if(i == map._dm.veroutecache.length - 1)
			{
				shape.SetCustomIcon("<img src='Images/directions_stop_icon.gif'>");	
			}
			else
			{
			    shape.SetCustomIcon("<img src=\"Images/Pins/" + REDCIRCLE + i +".gif\" alt=''/>");					
			}
			pinarray.push(shape);
		}
		map._dm.RemoveRoutePins();
		for(var i=0; i<pinarray.length;i++)
		{
		    //Finally add the pushpins back
		    layer.AddShape(pinarray[i]);
		}
    }
}


//function keyHandlerDirections(e){
//    var keyNum;

//    if(window.event){
//        keyNum=e.keyCode
//    }
//    else if(e.which){
//        keyNum=e.which
//    }
//    
//    if(keyNum == "13"){
//        ShowRoute();
//    }
//    else{
//        return String.fromCharCode(keyNum);
//    }
//}

function CloseRoutePanel()
{
    map.DeleteRoute();
    var layer = map.GetShapeLayerByIndex(ROUTELAYERINDEX);
    map.DeleteShapeLayer(layer);
    
    document.getElementById('RoutePanel').style.display="none";
    document.getElementById('SearchPanel').style.display="block";
    map.SetCenterAndZoom(originalCtr,originalZoom);
}


//toggle km and miles
function ChangeDistanceType( )
{
	if( DrivingUnit == VEDistanceUnit.Miles )
	{
		DrivingUnit = VEDistanceUnit.Kilometers;
	}
	else
	{
		DrivingUnit = VEDistanceUnit.Miles
	}
	FindRoute();
}


//reset map
function clearPushPins()
{
    map.DeleteAllPushpins();
    map.DeleteRoute();
    map.PanToLatLong(new VELatLong(38.550756,-98.319282));
    map.SetZoomLevel(4);
    HidePanel("eRecruitingPanel");
    HidePanel("dDirectionsPanel");
    HidePanel("DDRoutePanel");
    HidePanel("AmbiguousPanel");
    ShowPanel("locationPanel");
    startExpand("locationPanel");
    ResetAddress();
    polyzipSearchFlag=false;
}


