//<![CDATA[ 

/** Mappa */
var map = null;

/** Layer per le indicazioni stradali */
var dir = null;





/** Coordinate del punto centrale della mappa, iniziale */
var mapLat = 43.072865;
var mapLon = 12.608614;
/** Coordinate dell'hotel */
var hwsLat = 43.072995;
var hwsLon = 12.606714;
/** Livello di zoom iniziale */
var mapZoom = 15;
/** Tipo di mappa iniziale */
var mapType = google.maps.MapTypeId.HYBRID;

/** Icone dei marker per la rappresentazione dei punti di interesse */
var defaultMarkerIcon = "http://www.hotelwindsorsavoia.it/pictures/hws/logo/icon.gif";
var othersMarkerIcon  = "http://www.hotelwindsorsavoia.it/pictures/marker.gif";

/** Colore per il pulsante HOME */
var homeColor = "#23336E";

/** Finestra InfoWindow (eventualmente) attiva */
var currentInfoWindow = null;

/** Indirizzo di arrivo per ottenere le indicazioni stradali */
var toAddress = "viale Marconi 1, 06081, Assisi, PG, Italy";





/**
 * Testa il browser e crea la mappa
 */
function openGoogleMapsPage(locale) {
	var md = 'Hotel Windsor Savoia';
	var mt = 'HWS';
	var s1 = '';
	var s2 = '';
	var s3 = '';
	var s4 = '';
	var s5 = '';
	if (locale == 'it') {
		s1 = 'Basilica di San Francesco';
		s2 = 'Tempio della Minerva';
		s3 = 'Chiesa di Santa Chiara';
		s4 = 'Basilica di San Rufino';
		s5 = 'Chiesa di San Pietro';
	}
	if (locale == 'en') {
		s1 = 'St. Francis church';
		s2 = 'Minerva roman temple';
		s3 = 'St. Clare church';
		s4 = 'St. Rufino church';
		s5 = 'St. Peter church';
	}
	if (locale == 'de') {
		s1 = 'Basilika St. Francesco';
		s2 = 'Tempel der Minerva';
		s3 = 'Basilika S. Chiara';
		s4 = 'Katedrale St. Rufino';
		s5 = 'Basilika St. Peter';
	}
	loadGoogleMap(md, mt);
	loadStreetView(md, mt);
	setDefaultMarker(hwsLat, hwsLon, md, "<div class=\"google\" style=\"height:120px\">" + md + "<br /><img class=\"icon\" src=\"pictures/hws/hotel_small.jpg\" /></div>");
	addMarker(43.073855, 12.606398, s1, "<div class=\"google\" style=\"height:120px\">" + s1 + "<br /><img class=\"icon\" src=\"pictures/assisi/marker_sanfrancesco.jpg\" /></div>");
	addMarker(43.071184, 12.614584, s2, "<div class=\"google\" style=\"height:155px\">" + s2 + "<br /><img class=\"icon\" src=\"pictures/assisi/marker_minerva.jpg\" /></div>");
	addMarker(43.069103, 12.616665, s3, "<div class=\"google\" style=\"height:120px\">" + s3 + "<br /><img class=\"icon\" src=\"pictures/assisi/marker_santachiara.jpg\" /></div>");
	addMarker(43.070576, 12.617384, s4, "<div class=\"google\" style=\"height:120px\">" + s4 + "<br /><img class=\"icon\" src=\"pictures/assisi/marker_sanrufino.jpg\" /></div>");
	addMarker(43.071795, 12.607825, s5, "<div class=\"google\" style=\"height:120px\">" + s5 + "<br /><img class=\"icon\" src=\"pictures/assisi/marker_sanpietro.jpg\" /></div>");
}



/**
 * Crea il pulsante HOME sulla mappa
 */
function HomeControl(controlDiv, homeTitle, homeText, map) {
	// Set CSS styles for the DIV containing the control
	// Setting padding to 5 px will offset the control
	// from the edge of the map
	controlDiv.style.padding = '5px';

	// Set CSS for the control border
	var controlUI = document.createElement('DIV');
	controlUI.style.backgroundColor = 'white';
	controlUI.style.borderStyle = 'solid';
	controlUI.style.borderColor = homeColor;
	controlUI.style.borderWidth = '2px';
	controlUI.style.cursor = 'pointer';
	controlUI.style.textAlign = 'center';
	controlUI.title = homeTitle;
	controlDiv.appendChild(controlUI);
	controlDiv.index = 1;

	// Set CSS for the control interior
	var controlText = document.createElement('DIV');
	controlText.style.fontFamily = 'Arial,sans-serif';
	controlText.style.fontSize = '12px';
	controlText.style.color = homeColor;
	controlText.style.paddingLeft = '10px';
	controlText.style.paddingRight = '10px';
	controlText.innerHTML = '<b>' + homeText + '</b>';
	controlUI.appendChild(controlText);

	// Setup the click event listeners: simply set the map center
	google.maps.event.addDomListener(controlUI, 'click', function() {
		map.setCenter(new google.maps.LatLng(hwsLat, hwsLon))
	});
}



/**
 * Carica la mappa di Google con le impostazioni di posizione,
 * zoom e tipo di mappa iniziale
 * @param	homeTitle	Titolo del pulsante HOME
 * @param	homeText	Testo del pulsante HOME
 */
function loadGoogleMap(homeTitle, homeText) {
	//Creo e imposto la mappa
	map = new google.maps.Map(document.getElementById("map"), {
		mapTypeId: mapType,
		zoom: mapZoom,
		streetViewControl : false,
		//navigationControlOptions: {style: google.maps.NavigationControlStyle.ANDROID}
		navigationControlOptions: {style: google.maps.NavigationControlStyle.DEFAULT}
	});
	//Inizializzo la posizione della mappa
	map.setCenter(new google.maps.LatLng(mapLat, mapLon)); 

	/*
	//Layer aggiuntivi
	var bicyclingLayer = new google.maps.BicyclingLayer();
	var trafficLayer = new google.maps.TrafficLayer();
	bicyclingLayer.setMap(map);
	trafficLayer.setMap(map);
	*/

	// Create the DIV to hold the control and
	// call the HomeControl() constructor passing
	// in this DIV.
	var homeControlDiv = document.createElement('DIV');
	var homeControl = new HomeControl(homeControlDiv, homeTitle, homeText, map);
	map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);

	//Restituisco la mappa creata
	return map;
}



/**
 * Carica la street-view di google
 */
function loadStreetView(homeTitle, homeText) {
	//Non implementato tramite API
}



/**
 * Assegno il marker di default
 * @param	lat		Latitudine del marker di default
 * @param	lon		Longitudine del marker di default
 * @param	title	Titolo del marker
 * @param	htmlMessage		Messaggio HTML da visualizzare quando si clicca sul marker
 */
function setDefaultMarker(lat, lon, title, htmlMessage) {
	if (map != null) {
		//Creo il marker
		var defaultMarker = new google.maps.Marker();
		defaultMarker.setPosition(new google.maps.LatLng(lat, lon));
		defaultMarker.setIcon(new google.maps.MarkerImage(defaultMarkerIcon));
		defaultMarker.setTitle(title);
		defaultMarker.setZIndex(100);
		//Gestione degli eventi
		google.maps.event.addListener(defaultMarker, "click", function() { 
			showInfoWindow(defaultMarker, htmlMessage);
		});
		//Aggiungo il marker alla mappa
		defaultMarker.setMap(map);
		return defaultMarker;
	}
	return null;
}



/**
 * Aggiunge un marker secondario sulla mappa
 * @param	lat		Latitudine del marker 
 * @param	lon		Longitudine del marker 
 * @param	title	Titolo del marker
 * @param	htmlMessage		Messaggio HTML da visualizzare quando si clicca sul marker
 */
function addMarker(lat, lon, title, htmlMessage) {
	if (map != null) {
		//Creo il marker
		var marker = new google.maps.Marker();
		marker.setPosition(new google.maps.LatLng(lat, lon)); 
		marker.setIcon(new google.maps.MarkerImage(othersMarkerIcon));
		marker.setTitle(title);
		marker.setZIndex(1);
		//Gestione degli eventi
		google.maps.event.addListener(marker, "click", function() { 
			showInfoWindow(marker, htmlMessage);
		});
		//Aggiungo il marker alla mappa
		marker.setMap(map);
		return marker;
	}
	return null;
}



/**
 * Mostra la finestra contenente le informazioni aggiuntive sul marker
 * @param		marker			Marker
 * @param		htmlMessage		Messaggio HTML da visualizzare
 */
function showInfoWindow(marker, htmlMessage) {
	if (currentInfoWindow != null) {
		currentInfoWindow.close();
	}
	currentInfoWindow = new google.maps.InfoWindow({content: htmlMessage});			
	currentInfoWindow.open(map, marker);
}





//-----------------------------------------------------------
// INDICAZIONI STRADALI
//-----------------------------------------------------------





/**
 * Mostra le indicazioni stradali sulla mappa, prendento l'
 * indirizzo di partenza dal parametro "address" passato 
 * sull'URL per chiamare la pagina
 * @param	locale		Lingua per le indicazioni stradali
 */
function loadGoogleRoute(locale) {
	//Prendo l'indirizzo di partenza
	var requestURL = "" + document.location;
	var index = requestURL.indexOf("?address=", 0);
	var fromAddress = requestURL.substring(index + 9, requestURL.length);
	//Sostituisco i %20 con gli spazi
	var space = "%20";
	while (fromAddress.indexOf(space) != -1) {
		var index = fromAddress.indexOf(space);
		fromAddress = fromAddress.substring(0, index) + " " + fromAddress.substring(index + space.length);
	}

	//Preparo la richiesta
    var request = {
        origin : fromAddress, 
        destination : toAddress,
        travelMode : google.maps.DirectionsTravelMode.DRIVING
    };

	//Creo mappa e pannello con le indicazioni
    var map = new google.maps.Map(document.getElementById("map"), {
		disableDefaultUI: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});
	var dir = new google.maps.DirectionsRenderer();
	dir.setMap(map);
    dir.setPanel(document.getElementById("dir"));

	//Calcolo il percorso
	var service = new google.maps.DirectionsService();
    service.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			dir.setDirections(response);
		}
		if (status == google.maps.DirectionsStatus.INVALID_REQUEST) {
			alert("The DirectionsRequest provided was invalid.");
		}
		if (status == google.maps.DirectionsStatus.MAX_WAYPOINTS_EXCEEDED) {
			alert("Too many DirectionsWaypoints were provided in the request. The total allowed waypoints is 8, plus the origin and destination.");
		}
		if (status == google.maps.DirectionsStatus.NOT_FOUND) {
			alert("At least one of the origin, destination, or waypoints could not be geocoded.");
		}
		if (status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT) {
			alert("The webpage has gone over the requests limit in too short a period of time.");
		}
		if (status == google.maps.DirectionsStatus.REQUEST_DENIED) {
			alert("The webpage is not allowed to use the directions service.");
		}
		if (status == google.maps.DirectionsStatus.UNKNOWN_ERROR) {
			alert("A directions request could not be processed due to a server error. The request may succeed if you try again.");
		}
		if (status == google.maps.DirectionsStatus.ZERO_RESULTS) {
			alert("No route could be found between the origin and destination.");
		}
	});
}



/**
 * Apre la pagina contenente le indicazioni stradali
 * @param       page        Nome della pagina da aprire
 * @param		address		Indirizzo di partenza
 * @param		target		Target della pagina "_self", "_blank", ...
 */
function openRoute(page, address, target) {
	var w = 600;
	var h = 455;
    window.open(page + "?address=" + address, target, "scrollbars,width=" + w + ",height=" + h, false);
}



/** 
 * Effettua il submit del form e chide la finestra chiamante
 * @param       form        Form da submittare
 * @param       window      Finestra da chiudere
 */
function findRoute(form, window) {
    form.submit();
    window.close();
}



//]]> 

