//document.write("<sc"+"ript type='text/javascript' src='http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6&mkt=fr-fr'><"+"/sc"+"ript>");

// Recup variables
var d_adresse=""; var a_adresse="";
var d_cp=""; var a_cp="";
var d_ville=""; var a_ville="";
var d_pays=""; var a_pays="";

var _depart=""; 
var _arrivee="";
var _ititype=""; 

var tmp = window.location.search.substring(1).split("&");
var tabvaleur;
for (var i=0; i<tmp.length; i++) 
{
	tabvaleur = tmp[i].split("=");
	if (tabvaleur[1]!="") 
	{
		if (tabvaleur[0]=="d_adresse") { _depart =  tabvaleur[1]; d_adresse = tabvaleur[1]; }
		if (tabvaleur[0]=="d_cp") { _depart +=  ", " + tabvaleur[1]; d_cp = tabvaleur[1]; }
		if (tabvaleur[0]=="d_ville") { _depart +=  ", " + tabvaleur[1]; d_ville = tabvaleur[1]; }
		if (tabvaleur[0]=="d_pays") { _depart +=  ", " + tabvaleur[1]; d_pays = tabvaleur[1]; }

		if (tabvaleur[0]=="a_adresse") { _arrivee =  tabvaleur[1]; a_adresse = tabvaleur[1]; }
		if (tabvaleur[0]=="a_cp") { _arrivee +=  ", " + tabvaleur[1]; a_cp = tabvaleur[1]; }
		if (tabvaleur[0]=="a_ville") { _arrivee +=  ", " + tabvaleur[1]; a_ville = tabvaleur[1]; }
		if (tabvaleur[0]=="a_pays") { _arrivee +=  ", " + tabvaleur[1]; a_pays = tabvaleur[1]; }

		if (tabvaleur[0]=="ititype") { _ititype = tabvaleur[1]; }
	}
}
//alert(_depart);
		
//-----------------------------------

         var map = null;
		 var CarteFrance;
		 
         // Initial map view
         //var SeattleEastside = new VELatLong(47.65, -122.24, 0, VEAltitudeMode.Default);

         function GetMap()
         {
            map = new VEMap('mymap');
			map.LoadMap(CarteFrance, 8);
			map.SetZoomLevel('5');
			map.ShowMiniMap(10, 210);
			map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
			if (_depart!="" && _arrivee!="") GetRouteMap();
			if(d_pays=="") { document.formiti.d_pays[10].selected = "selected"; }
			if(a_pays=="") { document.formiti.a_pays[10].selected = "selected"; }
			traductionfr();
         }
		function traductionfr() 
		{
			
			document.getElementById("Compass").title = "Deplacer";	
			
			document.getElementById("MSVE_navAction_container").style.width = "465px";	
			document.getElementById("MSVE_navAction_container").className = "MSVE_Dashboard_V6 MSVE_OrthoView";	
			
			
			document.getElementById("MSVE_navAction_tinyZoomBar_plus").title = "Zoomer +";	
			document.getElementById("MSVE_navAction_tinyZoomBar_minus").title = "Zoomer -";	
			
			
			document.getElementById("MSVE_navAction_FlatlandMapMode").title = "Voir la carte en mode 2D";	
			document.getElementById("MSVE_navAction_View3DMapMode").title = "Voir la carte en mode 3D";
			
			document.getElementById("MSVE_navAction_ObliqueMapView").style.width = "80px";	
			document.getElementById("MSVE_navAction_ObliqueMapView").innerHTML = "Oeil d'oiseau";	
			document.getElementById("MSVE_navAction_ObliqueMapView").title = "Oeil d'oiseau";	
			
			document.getElementById("MSVE_navAction_RoadMapStyle").innerHTML = "Route";	
			document.getElementById("MSVE_navAction_RoadMapStyle").title = "Route";	
			
			document.getElementById("MSVE_navAction_AerialMapStyle").style.width = "80px";	
			document.getElementById("MSVE_navAction_AerialMapStyle").innerHTML = "Vue a&#233;rienne";	
			document.getElementById("MSVE_navAction_AerialMapStyle").title = "Vue aerienne";
			
			document.getElementById("MSVE_navAction_HybridMapStyle").style.width = "80px";	
			document.getElementById("MSVE_navAction_HybridMapStyle").innerHTML = "Vue hybride";	
			document.getElementById("MSVE_navAction_HybridMapStyle").title = "Vue hybride";	
			
			document.getElementById("MSVE_navAction_toggleGlyphWrapper").title = "Cacher la barre de controle";	

		}
		
		function No3DInstalled ()
		{
			alert('pour afficher les cartes en mode 3D, '+
			  'vous devez installer Virtual Earth 3D sur votre ordinateur.' +
			  '\npour installer le logiciel, visitez : ' +
			  'http://go.microsoft.com/fwlink/?LinkId=72623.');
		}

         function GetRouteMap()
         {
            var locations;
			
			if (d_adresse!="") d_adresse = decodeURI(d_adresse.replace(/\+/g," "));
			if (d_cp!="") d_cp = decodeURI(d_cp.replace(/\+/g," "));
			if (d_ville!="") d_ville = decodeURI(d_ville.replace(/\+/g," "));
			if (d_pays!="") d_pays = decodeURI(d_pays.replace(/\+/g," "));
			
			if (a_adresse!="") a_adresse = decodeURI(a_adresse.replace(/\+/g," "));
			if (a_cp!="") a_cp = decodeURI(a_cp.replace(/\+/g," "));
			if (a_ville!="") a_ville = decodeURI(a_ville.replace(/\+/g," "));
			if (a_pays!="") a_pays = decodeURI(a_pays.replace(/\+/g," "));
			
            document.formiti.d_adresse.value = d_adresse;
            document.formiti.d_cp.value = d_cp;
            document.formiti.d_ville.value = d_ville;

            document.formiti.a_adresse.value = a_adresse;
            document.formiti.a_cp.value = a_cp;
            document.formiti.a_ville.value = a_ville;
			
			for(i=0; i<document.formiti.d_pays.length; i++) 
			{ 
				if(document.formiti.d_pays[i].value==d_pays) { document.formiti.d_pays[i].selected = "selected";  }
			}
			for(i=0; i<document.formiti.a_pays.length; i++) 
			{ 
				if(document.formiti.a_pays[i].value==a_pays) { document.formiti.a_pays[i].selected = "selected";  }
			}
			
			if (_ititype=="court") { document.formiti.ititype[1].checked = "checked"; }

			//if (legForm.legType[0].checked)
            //{
          //locations = new Array("Tacoma WA", "Seattle WA", "Everett WA");
            //}
            //else if (legForm.legType[1].checked)
            //{
             //  locations = new Array("Tacoma WA", "Bellevue WA", "Everett WA");
            //}
			
			locations = new Array(_depart, _arrivee);
			//alert(locations);
			
            var options = new VERouteOptions;
            // Otherwise what's the point?
            options.DrawRoute      = true;
            // So the map doesn't change:
            options.SetBestMapView = true;
            // Call this function when map route is determined:
            options.RouteCallback  = ShowTurns;
            // En kms
            options.DistanceUnit   = VERouteDistanceUnit.Kilometer;
			options.UseMWS   = true;
            // Show the disambiguation dialog
            options.ShowDisambiguation = true;
			
			if (_ititype=="court") { options.RouteOptimize = VERouteOptimize.MinimizeDistance; }
			if (_ititype=="rapide") { options.RouteOptimize = VERouteOptimize.MinimizeTime; }
			
            map.GetDirections(locations, options);
         }

         function ShowTurns(route)
         {
            var turns = "<div class=iti_titre>R&eacute;sum&eacute; de votre itin&eacute;raire</div>";

            turns += "<div class=iti_titredepart><span>D&eacute;part</span>&nbsp;" + d_adresse + ", "+ d_cp+" "+ d_ville +" ("+ d_pays +")</div></div>";
            turns += "<div class=iti_titrearrivee><span>Arriv&eacute;e</span>&nbsp;" + a_adresse + ", "+ a_cp+" "+ a_ville +" ("+ a_pays +")</div></div>";
			turns += "<div class=iti_soustitre><div>Distance :</div> " + route.Distance.toFixed(1) + " kilom&egrave;tre(s)</div>";
            turns += "<div class=iti_soustitre><div>Temps :</div> " + GetTime(route.Time) + "</div>";

               var legs          = route.RouteLegs;
               var leg           = null;
               var turnNum       = 0;  // The turn #

               turns += "<div class=iti_feuilleroute>";
			   // Get intermediate legs
               for(var i = 0; i < legs.length; i++)
               {
                  // Get this leg so we don't have to derefernce multiple times
                  leg = legs[i];  // Leg is a VERouteLeg object

                  var legNum = i + 1;
                  //turns += "<br/><b>Distance for leg " + legNum + ":</b> " + leg.Distance.toFixed(1) + " kilom&egrave;tres" //+
                           //"<br/><b>Time for leg "     + legNum + ":</b> " + GetTime(leg.Time) + "<br/><br/>";

                  // Unroll each intermediate leg
                  var turn        = null;  // The itinerary leg
                  var legDistance = null;  // The distance for this leg
                  
                  turns += "<div class=iti_legende><div style=\"width:285px;\">&nbsp;&nbsp;&nbsp;Etapes</div><div style=\"width:100px; text-align:right;\">Kilom&egrave;tres&nbsp;</div></div>";
				  
				  var nb = 0;
				  var classbleu = "";
				  for(var j = 0; j < leg.Itinerary.Items.length; j ++)
                  {
                     if (nb==1) { nb = 0; classbleu = " iti_etape_bleue"; } else { nb = nb + 1; classbleu = ""; }
					 
					 turnNum++;
                     turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
                     if (turnNum==1)
					 {
						 turns += "<div class=iti_depart><div class=iti_etape_num>" + turnNum + "</div><div class=iti_etape_texte><span>D&eacute;part</span>" + turn.Text + "</div>";
					 }
					 else if (turnNum==leg.Itinerary.Items.length)
					 {
						 turns += "<div class=iti_arrivee><div class=iti_etape_num>" + turnNum + "</div><div class=iti_etape_texte><span>" + turn.Text + "</span></div>";
					 }
					 else
					 {
						 turns += "<div class=\"iti_etape"+classbleu+"\"><div class=iti_etape_num>" + turnNum + "</div><div class=iti_etape_texte>" + turn.Text + "</div>";
                     }
					 legDistance    = turn.Distance;

                     // So we don't show 0.0 for the arrival
                     if(legDistance > 0)
                     {
                        // Round distances to 1/10ths
                        turns += "<div class=iti_etape_kms>" + legDistance.toFixed(1) ;

                        
						/*
						// Append time if found
						if(turn.Time != null)
						{
						   turns += "; " + GetTime(turn.Time);
						}
						*/

                        turns += "</div></div>";
                     }
                  
				  // fin du for
				  
				  }

               }
			   turns += "</div>";


               // Populate DIV with directions
               SetDirections(turns);
            //}
         }

         function SetDirections(s)
         {
            var d = document.getElementById("directions");
            d.innerHTML = s;
         }

         // time is an integer representing seconds
         // returns a formatted string
         function GetTime(time)
         {
            if(time == null)
            {
               return("");
            }

            if(time > 60)
            {                                 // if time == 100
               var seconds = time % 60;       // seconds == 40
               var minutes = time - seconds;  // minutes == 60
               minutes     = minutes / 60;    // minutes == 1


               if(minutes > 60)
               {                                     // if minutes == 100
                  var minLeft = minutes % 60;        // minLeft    == 40
                  var hours   = minutes - minLeft;   // hours      == 60
                  hours       = hours / 60;          // hours      == 1

                  //return(hours + " heure(s), " + minLeft + " minute(s), " + seconds + " seconde(s)");
                  return(hours + " heure(s), " + minLeft + " minute(s)");
               }
               else
               {
                  //return(minutes + " min, " + seconds + " s");
                  return(minutes + " min");
               }
            }
            else
            {
               return(time + " s");
            }
         }
        
         function ClearAll()
         {
            map.DeleteRoute();
            SetDirections("");
            map.LoadMap(CarteFrance, 8);
         }


//var leform = '<form name="legForm">Route:<br/><input id="thruSeattle" type="radio" name="legType" checked="checked"/>Through Seattle<br/><input id="thruBellevue" type="radio" name="legType"/>Through Bellevue</form><form name="dirsForm"><input id="showdirs" type="radio" name="dirsType" checked="checked"/>Show turn-by-turn directions (below)<br/><input id="showdirs" type="radio" name="dirsType"/>Don\'t show turn-by-turn directions</form><input id="getroutemap" type="button" value="Get Route Map" onclick="GetRouteMap();"/><input id="clear" type="button" value="Clear All" onclick="ClearAll();"/>';

//var leform = '<form name="legForm">D&eacute;part : <input id="depart" type="text" name="depart" value="" size="50" /><br/>Arriv&eacute;e : <input id="arrivee" type="text" name="arrivee" value="" size="50" /></form><form name="dirsForm"><input id="getroutemap" type="button" value="Get Route Map" onclick="GetRouteMap();"/><input id="clear" type="button" value="Clear All" onclick="ClearAll();"/>';

//document.getElementById("leform").innerHTML = leform;

setTimeout("CarteFrance = new VELatLong(46.316584181822186, 2.636718749999995, 0, VEAltitudeMode.Default);", 1000);
//var SeattleEastside = new VELatLong(47.65, -122.24, 0, VEAltitudeMode.Default)
setTimeout("GetMap();", 1500);

