google.load("maps", "2.x");
		
var map  = null;
var lat  = "45.7729";
var long = "9.339262";
var centerLat  = "45.75";
var centerLong = "9.339262";
var centerZoom = 9;
//var map = new GMap2(document.getElementById("map_canvas"));
var geocoder = null;
var address = 'Via don A. Sempreboni, 5 - 37024 Negrar (Vr), Italia';
var gdir;
var addressMarker;

function cerchio (color) {
	map.clearOverlays();
	//map.setCenter(new GLatLng(centerLat,centerLong), centerZoom);
	var markerPoint = new GLatLng(lat, long);//map.getCenter();
	var polyPoints = Array();	
	var mapNormalProj = G_NORMAL_MAP.getProjection();
	var mapZoom = map.getZoom();
	var clickedPixel = mapNormalProj.fromLatLngToPixel(markerPoint, mapZoom);
	
	var polySmallRadius = 10;
	var polyNumSides = 20;
	var polySideLength = 18;
	for (var a = 0; a<(polyNumSides+1); a++) {
	var aRad = polySideLength*a*(Math.PI/180);
	var polyRadius = polySmallRadius; 
	var pixelX = clickedPixel.x + polyRadius * Math.cos(aRad);
	var pixelY = clickedPixel.y + polyRadius * Math.sin(aRad);
	var polyPixel = new GPoint(pixelX,pixelY);
	var polyPoint = mapNormalProj.fromPixelToLatLng(polyPixel,mapZoom);
	polyPoints.push(polyPoint);
	}
	// Using GPolygon(points,  strokeColor?,  strokeWeight?,  strokeOpacity?,  fillColor?,  fillOpacity?)
	highlightCircle = new GPolygon(polyPoints,"#000000",2,0.0,color,.5);
	return map.addOverlay(highlightCircle);
}

function tragitto(color, points, levels, lat, long, zoom) {
	map.clearOverlays();
	var encodedPolyline = new GPolyline.fromEncoded({
		color: color,//"#FF0000",
		weight: 4,
		points: points,//"siktGe|bbAhKyJpFtN`G}EpIjQvE`F",
		levels: levels,//"BBBBBB",
		zoomFactor: 32,
		numLevels: 4
	});
	if (lat && long && zoom) {
		map.setCenter(new GLatLng(lat,long), zoom);
	} else {
		map.setCenter(new GLatLng(centerLat,centerLong), centerZoom);
	}
	cerchio(color);
	return map.addOverlay(encodedPolyline);
}

function percorso(from, to) {
	document.getElementById('googleMap').style.display='block';
	map.clearOverlays();
	gdir = new GDirections(map, document.getElementById("directions"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
	setDirections(from, to, "it_IT");
}

function showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(
		  address,
			function(point) {
				if (!point) {
				  alert("Attenzione: indirizzo non trovato -> " + address);
					var lat  = "45.7729";
					var long = "9.339262";
					var point = new GLatLng(lat, long);
					//map.addOverlay(new GMarker(point));
					var marker = new GMarker(point);
					map.addOverlay(marker);
					//marker.openInfoWindowHtml("<b>Attenzione: indirizzo non trovato</b><br>" + address);
				} else {
				  map.setCenter(point, 14);
				  var marker = new GMarker(point);
				  map.addOverlay(marker);
				  //marker.openInfoWindowHtml(address);
				}
			}
		);
	}
}

function initialize() {
	if (GBrowserIsCompatible()) {
		//var map = new google.maps.Map2(document.getElementById("aziendaAgricola"));
		map = new GMap2(document.getElementById("googleMap"));			
		map.addControl(new GSmallMapControl());
		//map.addControl(new GMapTypeControl());
		var mapTypeControl = new GMapTypeControl();
		var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(5,15));
		map.addControl(mapTypeControl, bottomRight);
		//map.setCenter(new google.maps.LatLng(centerLat,centerLong), centerZoom);
		//map.setCenter(new GLatLng(centerLat,centerLong), centerZoom);	

		geocoder = new GClientGeocoder();
		//cerchio("0000FF");
		showAddress(address);
	}
}

function setDirections(fromAddress, toAddress, locale) {
	document.getElementById("directions").innerHTML = '';
	document.getElementById('directions').style.display='block';
	gdir.load("from: " + fromAddress + " to: " + toAddress,
			{ "locale": locale });
}

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);	 
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	 alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	
   else alert("An unknown error occurred.");
   
}

function onGDirectionsLoad(){
	// Use this function to access information about the latest load()
	// results.
	// e.g.
	//document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	//document.getElementById("getDistance").innerHTML = gdir.getDistance().html;
	//var pippo = gdir.getStatus().code;
	//var pluto = gdir.getDistance().html;
	//alert(pippo);
	//alert(pluto);
}

google.setOnLoadCallback(initialize);
