// Map variables
var map = null;
var shape;     
var layer;
var branchSelect = "<input name=\"branch\" type=\"radio\" id=\"branchAll\" value=\"all\" checked=\"checked\" onClick=\"mapPin(999);\" />Show all branches<br />";
var mapFormText = "For driving instruction on how to find us enter your post code below and hit search";
// mini map offset values
var miniMapX = 460;
var miniMapY = 200;
// Total number of branches
var numberOfBranches = 2;
// Lat and Long of branches
var branchPins = new Array(
	new VELatLong(51.07144, -0.36564),
	new VELatLong(51.13614, -0.1681)
);
// Branch names
var branchTitles = new Array(
	"Stevens at Horsham",
	"Stevens at Crawley"
);
// Branch address
var branchAddress = new Array(
	"Billingshurst Road<br />Broadbridge Heath<br />Horsham, Sussex<br />RH12 3LP",
	"Fleming Way Industrial Estate<br />Crawley<br />Sussex<br />RH10 9NS"
);

function addBranches(){
	
	for (var i = 0; i < numberOfBranches; i++){
		shape = new VEShape(VEShapeType.Pushpin, branchPins[i]);
		layer = map.GetShapeLayerByIndex(i);
		try{
			if(moreInfo != null){
				shape.SetDescription(branchAddress[i] + "<br />For more information <a href=\""+ moreInfo[i] +"\" target=\"_blank\">click here</a>");
			}
		}
		catch(error){
			shape.SetDescription(branchAddress[i]);
		}
		shape.SetTitle(branchTitles[i]);
		layer.AddShape(shape);
		if(numberOfBranches > 1){
			branchSelect = branchSelect + "<input name='branch' type='radio' id='branch" + i + "' value='" + i + "'  onClick='mapPin(" + i + ");' />" + branchTitles[i] + "<br />";
		}
	}
	if(numberOfBranches > 1){
		document.getElementById('branchSelect').innerHTML =	branchSelect;
		mapFormText = "For driving instruction please select a branch from the list below";
		
	} else {
		document.mapForm.postCode.disabled = false;
	}
	document.getElementById('mapFormText').innerHTML = mapFormText;
	map.SetMapView(branchPins);
}

function GetMap() {
	map = new VEMap('myMap');
	map.SetDashboardSize(VEDashboardSize.Normal); // Normal, Small or Tiny
	
	map.LoadMap();
	map.ShowMiniMap(miniMapX, miniMapY);
	
	for (var i = 0; i < numberOfBranches; i++){
		layer = "layer" + i;
		layer = new VEShapeLayer();
		map.AddShapeLayer(layer);
	}
	
	addBranches();
}


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(1).Hide();
		map.GetShapeLayerByIndex(2).Hide();

		myOptions.SetBestMapView = true;
		myOptions.RouteCallback = myRouteHandler; 
		map.GetDirections([postCode, TARGET], 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 mapPin(x){
	
	if(x == 999){
		for (var i = 0; i < numberOfBranches; i++){
			map.GetShapeLayerByIndex(i).Show();	
		}
		mapFormText = "For driving instruction please select a branch from the list below";
		document.getElementById('mapFormText').innerHTML = mapFormText;
		document.mapForm.postCode.value = "";
		document.mapForm.postCode.disabled = true;
		document.mapForm.btnGetInfo.disabled = true;
		document.mapForm.btnGetInfo.value = "Disabled";
		map.SetMapView(branchPins);
	} else {
		for (var i = 0; i < numberOfBranches; i++){
			map.GetShapeLayerByIndex(i).Hide();	
		}
		mapFormText = "For driving instruction on how to reach <strong>" + branchTitles[x] + "</strong><br />please enter your post code below and hit search";
		document.getElementById('mapFormText').innerHTML = mapFormText;
		document.mapForm.postCode.disabled = false;
		document.mapForm.btnGetInfo.disabled = false;
		document.mapForm.btnGetInfo.value = "Get directions";
		map.GetShapeLayerByIndex(x).Show();
		map.SetCenter(branchPins[x]);
		TARGET = branchPins[x];
	}
}

function resetMyMap(){
		mapFormText = "For driving instruction on how to find us enter your post code below and hit search";
		branchSelect = "<input name='branch' type='radio' id='branchAll' value='all' checked='checked' onClick='mapPin(999);' />Show all branches<br />";
		document.getElementById('branchSelect').innerHTML = "";
		document.getElementById('mapFormText').innerHTML = mapFormText;
		document.getElementById('directionDetailsSteps').innerHTML = "";
		document.mapForm.postCode.value = "";
		document.mapForm.postCode.disabled = true;
		document.mapForm.btnGetInfo.disabled = true;
		document.mapForm.btnGetInfo.value = "Disabled";
		document.mapForm.toggleMm.checked = true;
		GetMap();
}

function toggleMiniMap(){
		if(document.mapForm.toggleMm.checked == true)
		{
			map.ShowMiniMap(miniMapX, miniMapY);
		} else {
			map.HideMiniMap();
		}
}

var myOptions2 = new VERouteOptions();
var allOfThem = "";

function nearestBranch() {
	if(document.mapForm.near.value != ""){
		var postCode = document.mapForm.near.value;

		//map.GetShapeLayerByIndex(1).Hide();
		//map.GetShapeLayerByIndex(2).Hide();

		//myOptions.SetBestMapView = true;
		
		for (var k = 0; k < numberOfBranches; k++){
			myOptions2.RouteCallback = myRouteHandler2; 
			myOptions2.DrawRoute = false;
			allOfThem = allOfThem + branchTitles[k] + " "
			map.GetDirections([postCode, branchPins[k]], myOptions2);
			//allOfThem = allOfThem + totalDistance.toFixed(1) + " miles<br />";
		}
	} else {
		alert('Please enter your post code');
	}
	document.getElementById('directionDetailsSteps').innerHTML = allOfThem;
}	

function myRouteHandler2(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 += turnNum + ":  " + turn.Text;
         legDistance    = turn.Distance;
         totalDistance += legDistance;

         // Round distances to 1/10ths
         // Note that miles is the default
         //turns += " (" + legDistance.toFixed(1);
      }
   }
   //turns += "<strong>Total distance:  " + totalDistance.toFixed(1) + " miles</strong>";

   // Show directions
   alert(totalDistance.toFixed(1) + " miles<br />");
}
