// Map variables
var map = null;
var shape;     
var layer;
// Total number of branches
var numberOfBranches = 1;
// Lat and Long of branches
var branchTitle = "";
var branchPin = new VELatLong(0.00000, 0.00000);
var locationAddress = "";
var htmlAddress = "";
var TARGET = "";


function initMap(t,a1,a2,a3,a4,a5,p){
	branchTitle = t;
	locationAddress = "";
	htmlAddress = "";
	if(a1.length > 1){
		locationAddress = locationAddress + a1;
		htmlAddress = htmlAddress + a1;
	}
	if(a2.length > 1){
		locationAddress = locationAddress + "," + a2;
		htmlAddress = htmlAddress + "<br />" + a2;
	}
	if(a3.length > 1){
		locationAddress = locationAddress + "," + a3;
		htmlAddress = htmlAddress + "<br />" + a3;
	}
	if(a4.length > 1){
		locationAddress = locationAddress + "," + a4;
		htmlAddress = htmlAddress + "<br />" + a4;
	}
	if(a5.length > 1){
		locationAddress = locationAddress + "," + a5;
		htmlAddress = htmlAddress + "<br />" + a5;
	}
	if(p.length > 1){
		locationAddress = locationAddress + "," + p;
		htmlAddress = htmlAddress + "<br />" + p;
	}
	GetMap(locationAddress);
}

function GetMap(x) {
	map = new VEMap('myMap');
	map.SetDashboardSize(VEDashboardSize.Normal); // Normal, Small or Tiny
	map.LoadMap(branchPin, 3 ,'r' ,false);
	map.Find(null,x, null, null, null, null, null, null, null, null, setmap);
	

}
function setmap(layer, resultsArray, places, hasMore, veErrorMessage){
	try{
	branchPin = places[0].LatLong;
	} catch(err){
	}
	map.SetCenterAndZoom(branchPin, 15);
	shape = new VEShape(VEShapeType.Pushpin, branchPin);
	layer = map.GetShapeLayerByIndex(0);
	shape.SetDescription(htmlAddress);
	shape.SetTitle(branchTitle);
	layer.AddShape(shape);
}

var myOptions = new VERouteOptions();
	
function CreateLayer() {         
	layer = new VEShapeLayer();         
	map.AddShapeLayer(layer);     
}

function getRoute() {
	if(document.mapForm.postCode.value != ""){
		var postCode = document.mapForm.postCode.value;
		map.GetShapeLayerByIndex(0).Hide();
		myOptions.SetBestMapView = true;
		myOptions.RouteCallback = myRouteHandler; 
		map.GetDirections([postCode, branchPin], myOptions);
	} else {
		alert('Please enter your post code');
	}
}	

function myRouteHandler(route) {
   var legs          = route.RouteLegs;
   var turns         = "<strong>Turn-by-Turn Directions</strong>";
   var leg           = null;
   var turnNum       = 0;  // The turn #
   var totalDistance = 0;  // The sum of all leg distances

   // Get intermediate legs
   for(var i = 0; i < legs.length; i++)
   {
      // Get this leg so we don't have to dereference 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 += "<li>" + turnNum + ":  " + turn.Text;
         legDistance    = turn.Distance;
         totalDistance += legDistance;

         // Round distances to 1/10ths
         // Note that miles is the default
         turns += " (" + legDistance.toFixed(1) + " miles)</li>";
      }
   }
   turns += "<strong>Total distance:  " + totalDistance.toFixed(1) + " miles</strong>";

   // Show directions
   document.getElementById('directionDetailsSteps').innerHTML = "<ul>" + turns + "</ul>";
}

function resetMyMap(){
		document.getElementById('directionDetailsSteps').innerHTML = "";
		document.mapForm.postCode.value = "";
		GetMap(locationAddress);
}




