var map;
var geocoder;
var userAdded = 1;
var server = "http://nimue.provincia.milano.it/system/modules/it.milano.provincia.milano.portale_mappe/resources/kmz/";

if (document.domain == "provincia.milano.it" || document.domain == "www.provincia.milano.it") {
	server = "http://www.provincia.milano.it/mappe/pianificazione_territoriale/KMZ/"
} else if (document.domain == "provincia.mi.it" || document.domain == "www.provincia.mi.it") {
	server = "http://www.provincia.mi.it/mappe/pianificazione_territoriale/KMZ/"
}

var visualizza = {
	 "milano":					{"url" : server+"confini_provincia_milano.kmz"},
	 "monza":					{"url" : server+"confini_provincia_monza.kmz"},
	 "comuni":					{"url" : server+"confini_comuni_milano.kmz"}
}

function onLoad() {
  map=new GMap2(document.getElementById("map"),{draggableCursor: 'crosshair', draggingCursor: 'move'});
  map.setCenter(new GLatLng(45.483, 9.1873), 9);
  map.addControl(new GSmallMapControl());

  map.enableScrollWheelZoom();
  map.enableDoubleClickZoom();
  map.enableContinuousZoom();
  geocoder = new GClientGeocoder();

  
  var geoXml = new GGeoXml(visualizza['milano'].url);
  visualizza['milano'].geoXml = geoXml;
  map.addOverlay(geoXml);

  var Indirizzo = getUrlVars()["address"];
  if(Indirizzo!=null && Indirizzo!=""){
    geocoder.getLocations(Indirizzo, addAddressToMap);
  }
} 

function changelight(a,b,c,d){
	document.getElementById(a).style.display = "block";
	document.getElementById(b).style.display = "none";
	document.getElementById(c).className="on";
	document.getElementById(d).className="off";
}

function resizemap() {
	if (navigator.appName=="Microsoft Internet Explorer"){
		x=document.documentElement.clientWidth;
		document.getElementById("map").style.height = ((x*3)/4)/2 + "px";
	}
	else{
		x=window.innerWidth;
		document.getElementById("map").style.height = ((x*3)/4)/2 + "px";
	}
}



// Strade e trova indirizzo
// ------ Geocode del Cerca indirizzo
function showLocation() {
      var address = document.getElementById('indirizzoinserito').value;
      geocoder.getLocations(address, addAddressToMap);
    }
function addAddressToMap(response) {
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		map.setCenter(point, 13);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml('<span class="fumetto">' + place.address + '<br>' + '<b>Stato:</b> ' + place.AddressDetails.Country.CountryNameCode + '</span>');
      }
    }
// ----------------------


function getDirections() {
	var saddr = document.getElementById("saddr").value;
	var daddr = document.getElementById("daddr").value;
	gdir = new GDirections(map, document.getElementById("strade"));
	gdir.load("from: "+saddr+" to: "+daddr);
      // === Array for decoding the failure codes ===
      var reasons=[];
      reasons[G_GEO_SUCCESS]            = "Success";
      reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
      reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
      reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
      reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
      reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
      reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
      reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
      reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
      reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
      // === catch Directions errors ===
      GEvent.addListener(gdir, "error", function() {
        var code = gdir.getStatus().code;
        var reason="Code "+code;
        if (reasons[code]) {
          reason = reasons[code]
        } 
        alert("Failed to obtain directions, "+reason);
	}); 
}

// Funzioncina per prendere i parametri dell'URL
function getUrlVars(){
    var vars = [], hash;
    var hashes = unescape(window.location.href).slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
