var map = null;
		  
var mapZoom = 4;                 //Zoom level on the map : 1 to 19/21
var mapStyle = "r";              //Style of the map : a=aerial; h=hybrid; r=route
var latitude = 46.9202;          //Point to set center on France
var longitude = 2.1094;          //
var mapFixed = false;	           //True=display only; false=complete control
var mapMode = VEMapMode.Mode2D;  //Display mode : Mode2d; Mode3d
var mapDashboard = true;         //True=display the switch mode in dashboard;
                               //False=hide

var latLongPointStart = null;
var latLongPointStop = null;
var checkPoints = new Array();

var pinStart;
var pinStop; 
var routeGot = null;

//--- Virtual Earth functions

function GetMap()
{
  map = new VEMap('myMap');
 
  map.LoadMap(new VELatLong(latitude,longitude), 
              mapZoom, mapStyle, mapFixed, mapMode, mapDashboard);
  
  map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
  map.HideDashboard() ;   
  
  map.AttachEvent("onclick", OnClickMap);
   
  map.AttachEvent("onobliqueenter", showBirdMode);
  map.AttachEvent("onobliqueleave", hideBirdMode);
}   

function ChangeMapStyle(sMapStyle) 
{
  if(mapStyle != sMapStyle) {
    mapStyle = sMapStyle;
    map.SetMapStyle(mapStyle);
	if(mapStyle == 'o')
	  changeVisibility('birdOrientation','visible');
	else 
	  changeVisibility('birdOrientation','hidden');
  }             
}

function ChangeMapMode(sMapMode) 
{
  if((mapMode != sMapMode) || (mapStyle == 'o')) {	  
	mapMode = sMapMode;
	
    ChangeMapStyle('h');		
    map.SetMapMode(sMapMode);
	
	if(mapMode == VEMapMode.Mode3D) 
	  changeVisibility('mode3d_option','visible');
	else 
	  changeVisibility('mode3d_option','hidden');
  }
}

function SetBirdseyeOrientation(sOrientation)
{
    map.SetBirdseyeOrientation(sOrientation);
	changeBack('bird_arrow', 'toolbox/bird_' + sOrientation + '_f.png');
}


//--- event functions
function showBirdMode(e) 
{
	changeVisibility('modeBird', 'visible');
}

function hideBirdMode(e) 
{
	changeVisibility('modeBird', 'hidden');
}

function OnClickMap(e)
{
	//Get the latLong of the clicked point
	var x = e.mapX;
	var y = e.mapY;
	pixel = new VEPixel(x, y);
	var latLong = map.PixelToLatLong(pixel);
	
	//Left click set the start point, right set the stop point
	if(e.leftMouseButton)
	{
		if(latLongPointStart == null)
		{
			map.Clear();
			latLongPointStart = latLong;
			pinStart = new VEShape(VEShapeType.Pushpin, latLong);
			map.AddShape(pinStart);
		}
		else
		{
			alert("left but check");
			checkPoints.push(latLong);
			var checkPin = new VEShape(VEShapeType.Pushpin, latLong);
			map.AddShape(checkPin);
		}
	}
	else if (e.rightMouseButton)
	{
		latLongPointStop = latLong;
		pinStop = new VEShape(VEShapeType.Pushpin, latLong);
		map.AddShape(pinStop);
		drawRoute();
	}			
}

function drawRoute() 
{	
	map.Clear();
	var locations = new Array();
	locations.push(latLongPointStart);	
	for(var i in checkPoints)
	{
		locations.push(checkPoints[i]);
	}
	locations.push(latLongPointStop);
	
	latLongPointStart = null;
	checkPoints = new Array();
	latLongPointStop = null;
	
	var options = new VERouteOptions;	
	options.DrawRoute = true; 					// The route is drawn on the map
	options.ShowDisambiguation = true; 			// Show the disambiguation dialog
	options.SetBestMapView = true; 				// The map change to the show the route	
	options.RouteCallback = onGetRoute; 		// Call this function when map route is determined:	
	options.DistanceUnit = VERouteDistanceUnit.Kilometer; // Show as kilometers
	options.UseMWS = true; 						// The MapPoint Web Service is used to find 
												//the route (must be true if we want localization)	
	
	map.GetDirections(locations, options);
}

function onGetRoute(route) 
{
	document.getElementById("myVE_Information").innerHTML = "<b>Distance totale : " + route.Distance.toString().substring(0,6) + " km";
	document.getElementById("myVE_Information").innerHTML += "</b><br />";
	
	for(var indexRoute in route.RouteLegs)
	{
		for(var index in route.RouteLegs[indexRoute].Itinerary.Items)
		{
			var sInfoTemp;
			sInfoTemp = ">> " + route.RouteLegs[indexRoute].Itinerary.Items[index].Text;
			sInfoTemp += " - Dist : " + route.RouteLegs[indexRoute].Itinerary.Items[index].Distance.toString().substring(0,6) + " km";
			
			document.getElementById("myVE_Information").innerHTML += sInfoTemp + "<br />";
		}	
		document.getElementById("myVE_Information").innerHTML += "================ Point intermediaire " + indexRoute + " ================<br />";
	}
}

//--- CSS tips functions
function changeBack(sIdElement, sUrl) 
{
  document.getElementById(sIdElement).style.background= "url('" + sUrl + "')" ;
}

function changeVisibility(sIdElement, sState) 
{
  document.getElementById(sIdElement).style.visibility = sState ;	
}