	var map = null;
	var index = 0;
	var numResults = null;
	var shape = null;
	
	$(document).ready(function(){
//		
		if ($('#myMap').length >0){

//			$.getScript("http://mmaps.corriere.it/token/gazzetta.action");
			openMap();
			loadLocation();
		}
	});
	
	function openMap(){
		//carico la mappa del cerca-indirizzo
		
		
		map = new VEMap('myMap');
		map.SetClientToken(document.mstoken);
		map.LoadMap(new VELatLong(41.87165047026817, 12.517909705638878, 0, VEAltitudeMode.RelativeToGround ), 5, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
		
		document.getElementById("MSVE_obliqueNotification").style.visibility = "hidden"; 
		
		//controllo per centrare la mappa se si viene dalla home... (strCountry, strLocation)
		var qs_country = getQuerystring('strCountry', null)
		var qs_loc = getQuerystring('strLocation', null)
		
		
		if(qs_loc.length > 0 || qs_country.length > 0){
			var loc = '';
			loc += (qs_loc != null ? qs_loc : ' ') +' ';
			loc += (qs_country != null ? qs_country: ' ') + ' ';
			
			
			findLoc(loc);
		
		}
		
	}
	
	function firefoxPatch(){
				
		var ua = navigator.userAgent.toLowerCase();
		if(ua.indexOf( "firefox" ) != -1){
			var contain = document.getElementById('superbox');
			contain.style.margin = '-20px 0 0 0';
		}
	
	}
	
	
	function findWhatWhere(location, what){
	
		try
            {	
               results = map.Find(what,
                                  location,
                                  null,
                                  null,
                                  index,
                                  numResults,
                                  true,
                                  true,
                                  true,
                                  true,
                                  setPushpin);
                
            }
            catch(e)
            {
               alert(e.message);
            }
	
	
	}
	
	
	function findLoc(location){
		
		var strSearch = '';
		
		if(location == null){
			
			var address = document.getElementById('strLocation').value;
			var city = document.getElementById('strAddress').value;
			var cap = document.getElementById('strCP').value;
			var country = document.getElementById('strCountry').options[document.getElementById('strCountry').selectedIndex].value;
		
		
			if(address!=null||address.length>0) strSearch += (' ' + address);
			if(cap!=null||cap.length>0) strSearch += (' ' + cap);
			if(city!=null||city.length>0) strSearch += (' ' + city);
			strSearch += ' ' + country;
			
		}else{
			
			strSearch = location;
		}
		
		try
            {	
               strSearch = clearSpaces(strSearch);
               
               results = map.Find(null,
                                  strSearch,
                                  null,
                                  null,
                                  index,
                                  numResults,
                                  true,
                                  true,
                                  true,
                                  true,
                                  setPushpin);
                
            }
            catch(e)
            {
               alert(e.message);
            }
	}
	
	function setPushpin(layer, resultsArray, places, hasMore, veErrorMessage){
		
		if(places.length == 1){
			
			var place = places[0];
			
			try{
				if(shape != null){
            		map.DeleteShape(shape);
            		shape = null;
         		}

				
				shape = new VEShape(VEShapeType.Pushpin, place.LatLong);
         		shape.SetTitle(place.Name);
         		
         		map.AddShape(shape);

			}catch(e){
               alert(e.message);
            }
		}
	
	}
	
	function getTrip(){
		
		
		
		var from = '';
		var to = '';
		
		//prendo il box di avviso
		var achtung = document.getElementById('achtung');
		
		//prendo su tutti i campi
		var strStartAddress = document.getElementById('strStartAddress').value;
		var strStartCity = document.getElementById('strStartCity').value;
		var strStartCP = document.getElementById('strStartCP2').value;
		var strStartCityCountry = document.getElementById('strStartCityCountry').options[document.getElementById('strStartCityCountry').selectedIndex].value;
		
		var strDestAddress = document.getElementById('strDestAddress').value;
		var strDestCity = document.getElementById('strDestCity').value;
		var strDestCP = document.getElementById('strDestCP2').value;
		var strDestCityCountry = document.getElementById('strDestCityCountry').options[document.getElementById('strDestCityCountry').selectedIndex].value;
		
		if(strStartAddress!=null||strStartAddress.length>0) from += (' ' + strStartAddress);
			if(strStartCity!=null||strStartCity.length>0) from += (' ' + strStartCity);
			if(strStartCP!=null||strStartCP.length>0) from += (' ' + strStartCP);
			from += ' ' + strStartCityCountry;
		
			
			if(strDestAddress!=null||strDestAddress.length>0) to += (' ' + strDestAddress);
			if(strDestCity!=null||strDestCity.length>0) to += (' ' + strDestCity);
			if(strDestCP!=null||strDestCP.length>0) to += (' ' + strDestCP);
			to += ' ' + strDestCityCountry;
			
			
		//alert(strDestCity.length);
		//alert(strStartCity.length);
		//controllo che ci sia almeno la citta in modo da avere minori disambiguazioni possibili
		//if(strDestCity.length >0 && strStartCity.length >0){
			
			//achtung.style.display = 'none';
			//achtung.innerHTML = '';
			
			var options = new VERouteOptions();
			options.DistanceUnit = VERouteDistanceUnit.Kilometer;

            options.RouteCallback = onGotRoute;
			
			
			
			map.GetDirections([from,to], options);
			
			
		
		/*}else{
			//Altrimenti visualizzo un box in cui le chiedo
			achtung.style.display = 'block';
			var strAlert = '';
			
			if(strStartCity.length == 0){
				strAlert += '<h2>Inserisci la citt&aacute di partenza.</h2>';
			}
			if(strDestCity.length == 0){
				strAlert += '<h2>Inserisci la citt&aacute di arrivo.</h2>';
			}
			
			achtung.innerHTML = strAlert;
		}*/
	
	}
	
	
	function onGotRoute(route)
         {
         
           //apro il div che contiene le indicazioni
           var indicazioni = document.getElementById('indicazioni');
           indicazioni.style.display = 'block';
           
           // Unroll route
           var legs     = route.RouteLegs;
           var turns    = '';
           var numTurns = 0;
           var leg      = null;
           
           //Setto la distanza
           var distanza = document.getElementById('distanza');
           distanza.innerHTML = route.Distance.toFixed(1);
           
           var tempo = document.getElementById('tempo');
           tempo.innerHTML = Math.round((route.Time / 60)*100)/100;
           		
           var elenco = document.getElementById('elenco');

           // 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
                  
               // Unroll each intermediate leg
               var turn = null;  // The itinerary leg
                  
               for(var j = 0; j < leg.Itinerary.Items.length; j ++)
               {
                  turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
                  numTurns++;
                  turns += '<li>' + numTurns + '.\t' + turn.Text + ' (' + turn.Distance.toFixed(1) + ' Km)</li>' ;
               }
               
            //setto il num di svolte
          	var svolte = document.getElementById('svolte');
           	svolte.innerHTML = leg.Itinerary.Items.length

               
         }

		elenco.innerHTML = turns;
	}

	function getQuerystring(key, default_)
	{
  		if (default_==null) default_="";
  		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  		var qs = regex.exec(window.location.href);
  		if(qs == null)
    		return default_;
  		else
    		return qs[1];
	}
	
	function clearSpaces(key){
		return key.replace(/\+/g," ");
	}
