// Map variables
var map = null;
var shape;     
var layer;
// Lat and Long of the site
var LA = new VELatLong(51.67109, 0.38392);
var siteTitle = 'Start Smiling';
var siteAddress = '5 The Market Place<br />Ingatestone<br />Essex<br />CM4 0BY';

var pinPoint = null;
var pinPixel = null;

function GetMap() {
	map = new VEMap('myMap');
	map.LoadMap(LA, 14, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
	
	CreateLayer();                  
	
	//Add a pushpin to the new layer         
	shape = new VEShape(VEShapeType.Pushpin, map.GetCenter());         
	shape.SetTitle(siteTitle);         
	shape.SetDescription(siteAddress);         
	layer.AddShape(shape); 
}

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);
	}
}
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;
		if(shape != null)         
		{            
			layer.DeleteShape(shape);            
			shape = null;         
		}
		myOptions.SetBestMapView = true;
		myOptions.RouteCallback = myRouteHandler; 
		map.GetDirections([postCode, LA], myOptions);
	} else {
		alert('Please enter your post code');
	}
}	

function myRouteHandler(route) {
   // Unroll route and populate alert text
   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>";
}

