var map = null;
var route_div='directions_route';
var directions_div='directions';
var directions_from_div='directions';
var directions_to_div='directions';

function MSNGetMap(divname,lat,lon,range)
{
 	map = new VEMap(divname);
 	map.LoadMap(new VELatLong(lat,lon),range);
}   

function MSNAddMarker(title,description,lat,lon)
{
     layer = new VEShapeLayer();
     map.AddShapeLayer(layer);

	 //Add a pushpin to the new layer
	 
	 var point=new VELatLong(lat,lon);
	 if(lat=='' && lon=='')
		 point=map.GetCenter();
	 
     shape = new VEShape(VEShapeType.Pushpin,point);
     shape.SetTitle(title);
     shape.SetDescription(description);
     layer.AddShape(shape);   
}

function MSNShowDirections(from,to)
{
	var directionsdiv=document.getElementById(directions_div);
	directionsdiv.style.display='block';

	var directionsroutediv=document.getElementById(route_div);
	directionsroutediv.innerHTML="<strong>Directions</strong><br><img alt='loading...' title='loading...' border='0' src='/images/loading.gif'>";				

	map.GetRoute(from,to,null,null,MSNOnGotRoute);
}

function MSNOnGotRoute(route)
{
	var routeinfo="";
	var directionsroutediv=document.getElementById(route_div);
	var directionsdiv=document.getElementById(directions_div);


	if(route)
	{
		routeinfo+="<strong>Total distance: ";
		routeinfo+=   route.Itinerary.Distance+" ";
		routeinfo+=   route.Itinerary.DistanceUnit+"</strong><br>";
		
		var steps="";
		var len = route.Itinerary.Segments.length;
		   for(var i = 0; i < len ;i++)
		   {
			/*HACK*/
			if(i==len-1)
			{
				route.Itinerary.Segments[i].Instruction=route.Itinerary.Segments[i].Instruction.replace("Gallatin Gateway","Big Sky");
				route.Itinerary.Segments[i].Instruction=route.Itinerary.Segments[i].Instruction.replace("59730","59716");
			}


			  if(i>0)
		         steps+="<div class='directions_step'><span class='directions_step_number'>"+(i-1)+"</span> ";
			  steps+=route.Itinerary.Segments[i].Instruction+" <b> (";
		      steps+=route.Itinerary.Segments[i].Distance+") ";
		      steps+=route.Itinerary.DistanceUnit+"</b></div>";
		   }

		/*HACK*/
         steps+="<div class='directions_step'><span class='directions_step_number'>"+(i-1)+"</span> ";
		 steps+="Follow Little Coyote Road to the Big Sky Chapel at 510 Little Coyote Road.  Look for the steeple on the west side of the road.</div>";


		routeinfo+="<strong>Directions:</strong><br><br>"+steps;

		directionsroutediv.innerHTML=routeinfo;				
	}
	else
		directionsroutediv.innerHTML="<strong>Could not find directions for the locations entered</strong>";				

	directionsdiv.style.display='block';

}	  

