var initRomaLinee = function() {
	$('body').bind('unload',function(){GUnload()});
	
	attivaGMaps("marcheRomaXml.php",9,43.302758,13.722353);
}

var initNapoliLinee = function() {
	$('body').bind('unload',function(){GUnload()});
	
	attivaGMaps("marcheNapoliXml.php",8,42.9090725,13.8);
}

//Dichiarazioni Google Maps
//array per appoggiare i markers
var gmarkers = [];
var map;
var bounds
var gicons
var baseIcon
var geo

//A function to create the marker and set up the event window
// Dont try to unroll this function. It has to be here for the function closure
// Each instance of the function preserves the contends of a different instance
// of the "marker" and "html" variables which will be needed later when the event triggers.    
function createMarker(point,html,icontype) {
	var marker = new GMarker(point, gicons[icontype]);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	// save the info we need to use later for the side_bar
    gmarkers.push(marker);
	return marker;
}

//This function picks up the click and opens the corresponding info window
function myclick(i) {
	GEvent.trigger(gmarkers[i], "click");
	map.setZoom(10);
}


var attivaGMaps = function(XmlFile,Zoom,CentroLat,CentroLng) {
	if (GBrowserIsCompatible()) {

		//====== 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[403]                      = "Error 403: Probably an incorrect error caused by a bug in the handling of invalid JSON.";
		
		// ====== Create a Client Geocoder ======
	    geo = new GClientGeocoder(); 

		//=== Create an associative array of GIcons() ===
		baseIcon = new GIcon();
        baseIcon.iconSize=new GSize(27,32);
        baseIcon.shadowSize=new GSize(37,32);
        baseIcon.iconAnchor=new GPoint(16,32);
        baseIcon.infoWindowAnchor=new GPoint(16,0);
		gicons = [];
		gicons["A"] = new GIcon(baseIcon, "../layout/icona_maps_A.png", null, "../layout/icona_maps_32ombra.png");
		gicons["B"] = new GIcon(baseIcon, "../layout/icona_maps_B.png", null, "../layout/icona_maps_32ombra.png");
		gicons["C"] = new GIcon(baseIcon, "../layout/icona_maps_C.png", null, "../layout/icona_maps_32ombra.png");
		gicons["D"] = new GIcon(baseIcon, "../layout/icona_maps_D.png", null, "../layout/icona_maps_32ombra.png");
		gicons["E"] = new GIcon(baseIcon, "../layout/icona_maps_E.png", null, "../layout/icona_maps_32ombra.png");
		gicons["F"] = new GIcon(baseIcon, "../layout/icona_maps_F.png", null, "../layout/icona_maps_32ombra.png");
		gicons["G"] = new GIcon(baseIcon, "../layout/icona_maps_G.png", null, "../layout/icona_maps_32ombra.png");
		gicons["H"] = new GIcon(baseIcon, "../layout/icona_maps_H.png", null, "../layout/icona_maps_32ombra.png");
		gicons["I"] = new GIcon(baseIcon, "../layout/icona_maps_I.png", null, "../layout/icona_maps_32ombra.png");
		gicons["L"] = new GIcon(baseIcon, "../layout/icona_maps_L.png", null, "../layout/icona_maps_32ombra.png");
		gicons["M"] = new GIcon(baseIcon, "../layout/icona_maps_M.png", null, "../layout/icona_maps_32ombra.png");
		gicons["N"] = new GIcon(baseIcon, "../layout/icona_maps_N.png", null, "../layout/icona_maps_32ombra.png");
		gicons["O"] = new GIcon(baseIcon, "../layout/icona_maps_O.png", null, "../layout/icona_maps_32ombra.png");
		gicons["P"] = new GIcon(baseIcon, "../layout/icona_maps_P.png", null, "../layout/icona_maps_32ombra.png");
		gicons["Q"] = new GIcon(baseIcon, "../layout/icona_maps_Q.png", null, "../layout/icona_maps_32ombra.png");
		gicons["R"] = new GIcon(baseIcon, "../layout/icona_maps_R.png", null, "../layout/icona_maps_32ombra.png");
		gicons["S"] = new GIcon(baseIcon, "../layout/icona_maps_S.png", null, "../layout/icona_maps_32ombra.png");
		gicons["T"] = new GIcon(baseIcon, "../layout/icona_maps_T.png", null, "../layout/icona_maps_32ombra.png");
		gicons["U"] = new GIcon(baseIcon, "../layout/icona_maps_U.png", null, "../layout/icona_maps_32ombra.png");
		gicons["V"] = new GIcon(baseIcon, "../layout/icona_maps_V.png", null, "../layout/icona_maps_32ombra.png");
		
		
        
		
		// Display the map, with some controls and set the initial location 
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		//map.setMapType(G_SATELLITE_MAP);
		//map.setCenter(new GLatLng(43.907787,-70.359741),7);
		// ==== It is necessary to make a setCenter call of some description before adding markers ====
		// ==== At this point we dont know the real values ====
		map.setCenter(new GLatLng(0,0),0);
		// ===== Start with an empty GLatLngBounds object =====     
		bounds = new GLatLngBounds();
		// Set up three markers with info windows 
		//var point = new GLatLng(43.65654,-79.90138);
		//var marker = createMarker(point,'<div style="width:240px">Some stuff to display in the First Info Window. With a <a href="http://www.econym.demon.co.uk">Link<\/a> to my home page<\/div>')
		//map.addOverlay(marker);

		//var point = new GLatLng(43.91892,-78.89231);
		//var marker = createMarker(point,'Some stuff to display in the<br>Second Info Window')
		//map.addOverlay(marker);

		//var point = new GLatLng(43.82589,-79.10040);
		//var marker = createMarker(point,'Some stuff to display in the<br>Third Info Window')
		//map.addOverlay(marker);
		// Read the data from example.xml

		GDownloadUrl(XmlFile, function(doc) {
		
			var xmlDoc = GXml.parse(doc);
//alert('test6');
//alert(xmlDoc);
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	
			for (var i = 0; i < markers.length; i++) {
				// obtain the attribues of each marker
				var lat = parseFloat(markers[i].getAttribute("lat"));
				var lng = parseFloat(markers[i].getAttribute("lng"));
				var point = new GLatLng(lat,lng);
				//var html = markers[i].getAttribute("html");
				var html = GXml.value(markers[i].getElementsByTagName("infowindow")[0]);
				var label = markers[i].getAttribute("label");
				// === read the icontype attribute ===
				var icontype = markers[i].getAttribute("icontype");
				// === create the marker, passing the icontype string ===
				var marker = createMarker(point,html,icontype);
				map.addOverlay(marker);
				// ==== Each time a point is found, extent the bounds ato include it =====
				bounds.extend(point);
			}
		    
			//========= Now process the polylines ===========
			var lines = xmlDoc.documentElement.getElementsByTagName("line");
			// read each line
			for (var a = 0; a < lines.length; a++) {
				// get any line attributes
				var colour = lines[a].getAttribute("colour");
				var width  = parseFloat(lines[a].getAttribute("width"));
				// read each point on that line
				var points = lines[a].getElementsByTagName("point");
				var pts = [];
				for (var i = 0; i < points.length; i++) {
					pts[i] = new GLatLng(parseFloat(points[i].getAttribute("lat")),parseFloat(points[i].getAttribute("lng")));
				}
				map.addOverlay(new GPolyline(pts,colour,width));
			}
			// ================================================     
			// ===== determine the zoom level from the bounds =====
	        //map.setZoom(map.getBoundsZoomLevel(bounds));
			//map.setZoom(9);

	        // ===== determine the centre from the bounds ======
	        //map.setCenter(bounds.getCenter());
			map.setCenter(new GLatLng(CentroLat, CentroLng), Zoom);
		
		});
		
		
	} else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}	
}
