var gmarkers = [];
var htmls = [];

function myclick(i) { gmarkers[i].openInfoWindowHtml(htmls[i]); } // Apre la finesta delle informazioni

(function(load, unload){
onunload = function(){GUnload(unload?unload():0)};

onload = function(){if(GBrowserIsCompatible(load?load():0)) {
	var side_bar_html = "";
    var i = 0;
	
	// Create an associative array of GIcons()
	var gicons = [];
	gicons["HOME"] = new GIcon(G_DEFAULT_ICON, "../maps/markerHOME.png");
	gicons["A"] = new GIcon(G_DEFAULT_ICON, "../maps/markerA.png");
	gicons["B"] = new GIcon(G_DEFAULT_ICON, "../maps/markerB.png");
	gicons["C"] = new GIcon(G_DEFAULT_ICON, "../maps/markerC.png");
	gicons["D"] = new GIcon(G_DEFAULT_ICON, "../maps/markerD.png");
	gicons["E"] = new GIcon(G_DEFAULT_ICON, "../maps/markerE.png");
	gicons["F"] = new GIcon(G_DEFAULT_ICON, "../maps/markerF.png");
	gicons["G"] = new GIcon(G_DEFAULT_ICON, "../maps/markerG.png");
	gicons["H"] = new GIcon(G_DEFAULT_ICON, "../maps/markerH.png");
	gicons["I"] = new GIcon(G_DEFAULT_ICON, "../maps/markerI.png");
	gicons["J"] = new GIcon(G_DEFAULT_ICON, "../maps/markerJ.png");
	gicons["K"] = new GIcon(G_DEFAULT_ICON, "../maps/markerK.png");
	gicons["L"] = new GIcon(G_DEFAULT_ICON, "../maps/markerL.png");
	gicons["M"] = new GIcon(G_DEFAULT_ICON, "../maps/markerM.png");
	gicons["N"] = new GIcon(G_DEFAULT_ICON, "../maps/markerN.png");
	gicons["O"] = new GIcon(G_DEFAULT_ICON, "../maps/markerO.png");
	gicons["P"] = new GIcon(G_DEFAULT_ICON, "../maps/markerP.png");
	gicons["Q"] = new GIcon(G_DEFAULT_ICON, "../maps/markerQ.png");
	gicons["R"] = new GIcon(G_DEFAULT_ICON, "../maps/markerR.png");
	gicons["S"] = new GIcon(G_DEFAULT_ICON, "../maps/markerS.png");
	gicons["T"] = new GIcon(G_DEFAULT_ICON, "../maps/markerT.png");
	gicons["U"] = new GIcon(G_DEFAULT_ICON, "../maps/markerU.png");
	gicons["V"] = new GIcon(G_DEFAULT_ICON, "../maps/markerV.png");
	gicons["W"] = new GIcon(G_DEFAULT_ICON, "../maps/markerW.png");
	gicons["X"] = new GIcon(G_DEFAULT_ICON, "../maps/markerX.png");
	gicons["Y"] = new GIcon(G_DEFAULT_ICON, "../maps/markerY.png");
	gicons["Z"] = new GIcon(G_DEFAULT_ICON, "../maps/markerZ.png");

// A function to create the marker and set up the event window
	function createMarker(point,name,html,category) {
		var marker = new GMarker(point, gicons[category]);
		GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
		gmarkers[i] = marker;
		htmls[i] = html;
		side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
		i++;
		return marker;
	}
	
	// create the map
	var map = new GMap2(document.getElementById("mappa"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(varLAT,varLON), 9);
	//map.setMapType(G_HYBRID_MAP);
	
	// A function to read the data
	function readMap(url) {
		var request = GXmlHttp.create();
		request.open("GET", url, true);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				var xmlDoc = request.responseXML;
				// obtain the array of markers and loop through it
				var channel = xmlDoc.documentElement.getElementsByTagName("item");
				// hide the info window, otherwise it still stays open where the removed marker used to be
				map.getInfoWindow().hide();
				map.clearOverlays();
				// empty the array
				gmarkers = [];
				// reset the side_bar
				side_bar_html="";
				
			for (var i = 0; i < channel.length; i++) {
				// obtain the attribues of each marker
				var lat = GXml.value(channel[i].getElementsByTagName("geo:lat")[0])||GXml.value(channel[i].getElementsByTagName("lat")[0]);
				var lng = GXml.value(channel[i].getElementsByTagName("geo:long")[0])||GXml.value(channel[i].getElementsByTagName("long")[0]);
				
				var point = new GLatLng(lat,lng);
				var label = GXml.value(channel[i].getElementsByTagName("title")[0]);
				var category = GXml.value(channel[i].getElementsByTagName("category")[0]);
				var descr = GXml.value(channel[i].getElementsByTagName("description")[0]);
				var html = '<div class="mappa"><h1>' + label + '</h1><div class="mappaDesc">' + descr + '</div></p></div>';
				// create the marker
				var marker = createMarker(point,label,html,category);
				map.addOverlay(marker);
			}
			// put the assembled side_bar_html contents into the side_bar div
			//document.getElementById("side_bar").innerHTML = side_bar_html;

			}
		}
		request.send(null);
	}
	
	// When initially loaded, use the data from "map11a.xml"
	readMap("../maps/maps-"+varLANG+".xml");
}
else {alert("Siamo spiacenti, non \u00E8 possibile visualizzare le mappe. Contattare Web Agency Meta Line: info@metaline.it - www.metaline.it");}
};
})(window.onload, window.onunload);