﻿
//the drive function that calls initializes and calls GetDirections
function Drive() {
	//var DirectionsList = new Array();
	var endAddress = "";
	var startAddress = "";
	var startValue = "";
	var endValue = "";

	//try {
		var options = new VERouteOptions;
		options.DrawRoute = true;
		// Show as miles
		options.DistanceUnit = VERouteDistanceUnit.Mile;
		// Show the disambiguation dialog
		//options.ShowDisambiguation = true;
		options.SetBestMapView = true;
		
		options.RouteOptimize = VERouteOptimize.MinimizeDistance;
		options.RouteCallback = ShowTurns;
		
		//Value is index|Lat,Lon
		/* check for a lat & lon, if found, parse & use instead of the address ( which is probably useless because the data available in listingInfo object is not enough for a fully qualified address (i.e. no state) )*/
		startValue = getComboBoxValue(StartComboBox);
		if ((startValue.indexOf("|") > -1) && (!startValue == "")){
			var myStartLatLonArr = startValue.split("|")[1].split(",");
			startAddress = new VELatLong(myStartLatLonArr[0], myStartLatLonArr[1]);
		} else {
			startAddress = getComboBoxText(StartComboBox);
			//call setcookie here
			_setCookie("HCScript=StartAddr", startAddress, Date() + 31);
		}

		endValue = getComboBoxValue(EndComboBox);
		if ((endValue.indexOf("|") > -1) && (!endValue == "")){
			var myEndLatLonArr = endValue.split("|")[1].split(",");
			endAddress = new VELatLong(myEndLatLonArr[0], myEndLatLonArr[1]);
		}else{
			endAddress = getComboBoxText(EndComboBox);
			//call setcookie here
			_setCookie("HCScript=EndAddr", endAddress, Date() + 31);

		}


		map.GetDirections([startAddress, endAddress], options);
	//} catch(ex) {alert(ex); handleE(ex, "drive")}
}//End Drive Function

function ShowTurns(route) {
	if (!route || !route.RouteLegs) {
		alert("Could not find the directions between '" + getComboBoxText(StartComboBox) + "' and '" + getComboBoxText(EndComboBox) + "'");
		return;
	}
// Unroll route and populate alert text
   var legs          = route.RouteLegs;
   var leg           = null;
   var turnNum       = 0;  // The turn #
   var totalDistance = 0;  // The sum of all leg distances
   //added <center> ghetto resolution
   var saddr = getComboBoxItem(StartComboBox);
   if (saddr == ""){saddr = getComboBoxText(StartComboBox);}
   
   var eaddr = getComboBoxItem(EndComboBox);
   if (eaddr == ""){eaddr = getComboBoxText(EndComboBox);}
   
   var seAddr = "<div style=text-align:left;><b>Start Address: </b>" + saddr + "<br /><b>End Address: </b>" + eaddr + "</div>";
   var turns = seAddr + "<br /><center><h3>Turn-by-Turn Directions</h3></center>";

	try{
		 // 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
				var legDistance = null;  // The distance for this leg

				for(var j = 0; j < leg.Itinerary.Items.length; j ++){
					 turnNum++;
					// turn is a VERouteItineraryItem object
					turn = leg.Itinerary.Items[j];  
					turns += "<b>" + turnNum + "</b>:  " + turn.Text;
					legDistance    = turn.Distance;
					totalDistance += legDistance;

					// Round distances to 1/10ths
					// Note that miles is the default
					turns += " (" + "<b>" + legDistance.toFixed(1) + " miles)</b><br />";
				}
		}
//		turns += "<br /><center><b>(rounding errors are possible)</b>";
		turns += "<br /><b>Approximate distance:  </b>" + totalDistance.toFixed(1) + " miles<br />";
		turns += "<b>Approximate Time:</b> " + GetTime(route.Time) + "";
		//BuildGasLink();
		//$("printwin").style.display='';
		//$("gaslink").style.display = '';
			
} catch(ex) {
	handleE(ex, "ShowTurns");
}
	// Populate DIV with directions
	$j('#DirectionsResult').html(turns);
}

// time is an integer representing seconds
// returns a formatted string
function GetTime(time){
			
			if(time == null){return("");}
			if(time > 60){                                 // if time == 100
					var seconds = time % 60;       // seconds == 40
					var minutes = time - seconds;  // minutes == 60
					minutes     = minutes / 60;    // minutes == 1
					if(minutes > 60){                                     // if minutes == 100
							var minLeft = minutes % 60;        // minLeft    == 40
							var hours   = minutes - minLeft;   // hours      == 60
							hours       = hours / 60;          // hours      == 1
							return(hours + " hour(s), " + minLeft + " minute(s), " + seconds + " second(s)");
					}else{return(minutes + " minutes, " + seconds + " seconds");}
			}else{return(time + " seconds");}
}


