﻿//<![CDATA[
if (GBrowserIsCompatible()) {
	
	var latitudine = 44.010422;
	var longitudine = 8.178028;
	var httpdominio = "http://www.hotelsavoia.it";
	var tipomappa = G_SATELLITE_MAP;
	var zoom = 18;
	var con_sidebar = true;

	// aggiungere qui i poi da visualizzare
	var poiFiles = [];
	poiFiles[0] = "data.xml";
	// poiFiles[1] = "http://www.mediawest.it/maps/provinciasv.xml";


	
	// this variable will collect the html which will eventualkly be placed in the side_bar
	var side_bar_html = "";
	
	// arrays to hold copies of the markers and html used by the side_bar
	// because the function closure trick doesnt work there
	var gmarkers = [];
	var htmls = [];
	var i = 0;
	
	// arrays to hold variants of the info window html with get direction forms open
	var to_htmls = [];
	var from_htmls = [];
	
	// A function to create the marker and set up the event window
	function createMarker(markerXML){
		// oggetto markerXML -> attributi : lat,lng,img,tit,addr,telfax,mail,big
		// viene usato anche latitudine e longitutidine che sono le var globali di partenza
	
	 // Creo il punto
	 var point = new GLatLng(parseFloat(markerXML.getAttribute("lat")),parseFloat(markerXML.getAttribute("lng")));

	 // Creo l'icona
	 var icon = new GIcon();
	 icon.image = httpdominio + "/maps/" + markerXML.getAttribute("img") + ".gif";
	 icon.shadow = httpdominio + "/maps/shadow.png";
	 icon.iconSize = new GSize(19, 33);
	 icon.shadowSize = new GSize(37, 34);
	 icon.iconAnchor = new GPoint(0, 33);
	 icon.infoWindowAnchor = new GPoint(5, 1);
	    
	 var marker = new GMarker(point, icon);
	 // costruisco la minipagina da visualizzare (info window)
	 // classi css : tit,addr,coordinate,imgthumb
	 var html = '<div class="gmapsboxtit">' + markerXML.getAttribute("tit") + '</div>';
		html += '<div class="gmapsboxaddr">' + markerXML.getAttribute("addr") + '<br />' + markerXML.getAttribute("telfax") + '<br /><a href="mailto:' + markerXML.getAttribute("mail") + '">' + markerXML.getAttribute("mail") + '</a></div>';
		html += '<div class="gmapsboxcoordinate"><b>lat: </b>' + markerXML.getAttribute("lat") + ' - <b>long: </b>' + markerXML.getAttribute("lng") + '</div>';
		html += '<div class="gmapsboximgthumb"><img src="' + markerXML.getAttribute("big") + '"></div>';
		
	 // aggiungo testo e bottoni per quando ha cliccato su "to here" 
	 // classi css : gmapsboxtxtsmall
	 to_htmls[i] = html + '<div id="gmapsboxtxtsmall">Traccia il percorso <b>fino a qui</b> (o <a href="javascript:fromhere(' + i + ')">da qui a</a>)</div>' +
		'<div class="gmapsboxtxtsmall"><b>da dove: </b>(scrivi: città, ev. indirizzo)<br />(es. Savona, Largo Folconi, 3 - o solo Savona)</div>'+
		'<form action="http://maps.google.com/maps" method="get" target="_blank">' +
		'<input type="text" size=40 maxlength=40 name="saddr" value="" /><br />' +
		'<input value="vai alle indicazioni stradali" type="submit" />' +
		'<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + '" />' +
		'</form>';
		
	 // aggiungo testo e bottoni per quando ha cliccato su  "from here" 
	 // classi css : gmapsboxtxtsmall
	 from_htmls[i] = html + '<div class="gmapsboxtxtsmall">Traccia il percorso: <a href="javascript:tohere(' + i + ')">(a qui)</a> - <b>da qui</b></div>' +
		'<div class="gmapsboxtxtsmall"><b>a dove: </b>(scrivi: città, ev. indirizzo)<br />(es. Savona, Largo Folconi, 3 - o solo Savona)</div>'+
		'<form action="http://maps.google.com/maps" method="get" target="_blank">' +
		'<input type="text" size="40" maxlength="40" name="daddr" value="" /><br />' +
		'<input value="vai alle indicazioni stradali" type="submit" />' +
		'<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() + '" />' +
		'</form>';


	 // aggiungo testo e bottoni per quando visualizzo info window, non selezionato ancora nè "to here" nè "from here"
	 // classi css : gmapsboxtxtsmall
	 html = html + '<div class="gmapsboxtxtsmall">Trova il percorso: <a href="javascript:tohere(' + i + ')">fino a qui</a> oppure <a href="javascript:fromhere(' + i + ')">da qui a</a></div>';
	 
	 GEvent.addListener(marker,"click", function() {
	 	marker.openInfoWindowHtml(html);
	 });

	 gmarkers[i] = marker;
	 htmls[i] = html;
	 
	 // aggiungo il posto alla side_bar html
	 // classi css : gmapssidebar,gmapssidebargoto

	side_bar_html += '<table width="90%" cellpadding="0" cellspacing="2" border="0">';
	side_bar_html += '<tr>';
	side_bar_html += '<td width="24"><a href="javascript:myclick(' + i + ')"><img src="'+icon.image+'" alt="" /></a></td>'; // cella dell'icona
	side_bar_html += '<td class="gmapssidebar"><a href="javascript:myclick(' + i + ')">' + markerXML.getAttribute("tit") + '</a>'; // cella del punto




	if (i>0){
		// presuppongo che il primo marker sia l'origine di partenza quindi non posso calcolare percorso essendo punto uguale (dovrei testare le coordinate se uguali non visualizzare)
		side_bar_html += '<br /><a href="http://maps.google.com/maps?saddr=' + latitudine + ',' + longitudine + '&daddr=' + point.lat() + ',' + point.lng() + '" target="_blank"><img src="icone/percorso.gif" alt="Calcola il percorso" /></a>';
	}

	side_bar_html += '</td></tr>';
	side_bar_html += '</table>';

	 i++;
	 return marker;

	}
	
	
	// This function picks up the click and opens the corresponding info window
	function myclick(i) {
	 gmarkers[i].openInfoWindowHtml(htmls[i]);
		// document.getElementById("tmp_box").innerHTML = htmls[i];
	}
	
	// functions that open the directions forms
	function tohere(i) {
	 gmarkers[i].openInfoWindowHtml(to_htmls[i]);
		// document.getElementById("tmp_box").innerHTML = to_htmls[i];
	}
	function fromhere(i) {
	 gmarkers[i].openInfoWindowHtml(from_htmls[i]);
		// document.getElementById("tmp_box").innerHTML = from_htmls[i];
	}
	
	// create the map
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(latitudine, longitudine), zoom);
	map.setMapType(tipomappa);
	map.addControl(new GOverviewMapControl ());
	
	// add the points
	for (var nFile = 0 ; nFile < poiFiles.length; nFile++)
	{	
		// Download the data in data.xml and load it on the map.
		GDownloadUrl(poiFiles[nFile], function(data, responseCode) 
		{ //alert(responseCode);
		 // To ensure against HTTP errors that result in null or bad data,
		 // always check status code is equal to 200 before processing the data
		 if ((responseCode == 200)||(responseCode == 0)) {

			// qui mettere codice per dividere un gruppo da un altro
			side_bar_html += "<div><h1>Punti di interesse</h1></div>";

			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			for (var j = 0; j < markers.length; j++) {
				var marker = createMarker(markers[j]);
				map.addOverlay(marker);
			} 
			if (con_sidebar)
			{ 	// alert(side_bar_html);
				// put the assembled side_bar_html contents into the side_bar div
				document.getElementById("side_bar").innerHTML = side_bar_html;
			}

		 } 
		 else if(responseCode == -1) { alert("Richiesta scaduta. Prego riprovare più tardi."); }
			 else {	alert("Elenco punti di interesse non recuperabile.Prego controllare."); }
		});
	}


	// window.attachEvent("onunload", closeGMaps);      

	if (window.addEventListener) //DOM method for binding an event
		window.addEventListener("unload", closeGMaps, false);
	else if (window.attachEvent) //IE exclusive method for binding an event
		window.attachEvent("onunload", closeGMaps);
	else if (document.getElementById) //support older modern browsers
		window.onunload=closeGMaps;

}
else { alert("Sorry, the Google Maps API is not compatible with this browser"); }

function closeGMaps() 
{
// alert("chiudo");
 GUnload();
}
//]]>

