
var gDetailZoom	= 15;
var gGlobalZoom	= 12;
var gCountryZoom	= 5;

var gNoMapControl		= 0;
var gSmallMapControl	= 1;
var gLargeMapControl	= 2;

var gSelectedMarker = new GIcon();
gSelectedMarker.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
gSelectedMarker.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
gSelectedMarker.iconSize = new GSize(12, 20);
gSelectedMarker.shadowSize = new GSize(22, 20);
gSelectedMarker.iconAnchor = new GPoint(6, 20);
gSelectedMarker.infoWindowAnchor = new GPoint(5, 1);

var gGenericMarker = new GIcon();
gGenericMarker.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
gGenericMarker.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
gGenericMarker.iconSize = new GSize(12, 20);
gGenericMarker.shadowSize = new GSize(22, 20);
gGenericMarker.iconAnchor = new GPoint(6, 20);
gGenericMarker.infoWindowAnchor = new GPoint(5, 1);

var gHotelMarker = new GIcon();
gHotelMarker.image = "/images/icons/map-hotel.png";
//gHotelMarker.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
gHotelMarker.iconSize = new GSize(22, 22);
//gHotelMarker.shadowSize = new GSize(22, 20);
gHotelMarker.iconAnchor = new GPoint(11, 22);
gHotelMarker.infoWindowAnchor = new GPoint(5, 1);

var gEstateMarker = new GIcon();
gEstateMarker.image = "/images/icons/map-estate.png";
//gEstateMarker.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
gEstateMarker.iconSize = new GSize(32, 32);
//gEstateMarker.shadowSize = new GSize(22, 20);
gEstateMarker.iconAnchor = new GPoint(16, 32);
gEstateMarker.infoWindowAnchor = new GPoint(5, 1);

var gLandmarkMarker = new GIcon();
gLandmarkMarker.image = "/images/icons/map-landmark.png";
//gHotelMarker.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
gLandmarkMarker.iconSize = new GSize(22, 22);
//gHotelMarker.shadowSize = new GSize(22, 20);
gLandmarkMarker.iconAnchor = new GPoint(11, 22);
gLandmarkMarker.infoWindowAnchor = new GPoint(5, 1);

var gCityMarker = new GIcon();
gCityMarker.image = "/images/icons/map-city.gif";
//gCityMarker.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
gCityMarker.iconSize = new GSize(15, 15);
//gCityMarker.shadowSize = new GSize(22, 20);
gCityMarker.iconAnchor = new GPoint(8, 8);
gCityMarker.infoWindowAnchor = new GPoint(5, 1);

function createMarker(point,icon,html) {
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

var hotelOverlays = new Array();

function GMapsLoad(mapId,centerLat,centerLong,zoomLevel,mapControl,typeControl,marker,markerHtml,moreMarkers,moreMarkersIcon,notDraggable,pointer,xmlHotelIds,xmlGetUrl,xmlMarker,xmlPattern,xmlSelectedHotelId)
{
	var gMap = null;
	
	if ( GBrowserIsCompatible() )
	{
	//	Visualizza e centra la mappa nella coordinata
		gMap = new GMap2( document.getElementById(mapId), pointer ? { draggableCursor: 'pointer' } : null );

		gMap.setCenter(new GLatLng(centerLat, centerLong), zoomLevel);

		if ( mapControl )
		{
			if ( mapControl == gLargeMapControl )
				gMap.addControl(new GLargeMapControl());
			else
				gMap.addControl(new GSmallMapControl());
		}

		if ( typeControl )
			gMap.addControl(new GMapTypeControl());
		if ( notDraggable )
			gMap.disableDragging();


	//	Overlay dati open source
		//var wikiLayer = new GLayer("org.wikipedia.it");
		//var panoLayer = new GLayer("com.panoramio.all");
		//gMap.addOverlay(wikiLayer);
		//gMap.addOverlay(panoLayer);	

	// Aggiunge il marcatore nel centro
		var bounds = gMap.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngSpan = northEast.lng() - southWest.lng();
		var latSpan = northEast.lat() - southWest.lat();
		var point = new GLatLng(southWest.lat() + latSpan/2,southWest.lng() + lngSpan/2);
		if ( marker )
		{
		//	Icona con possibilità di click
			if ( isNaN(markerHtml) )
				addHtmlMarker( gMap, point, marker, markerHtml );

		//	Solo icona
			else
			{
				gMap.addOverlay(new GMarker(point,marker));
			}
		}

	//	Marcatori ulteriori
		if ( moreMarkers instanceof Array )
		{
			//alert( moreMarkers )
			//alert( moreMarkers.length )

			for ( i in moreMarkers )
			{
				if ( moreMarkers[i]._set )
				{
					var marker = createMarker( moreMarkers[i]._coord, moreMarkersIcon, moreMarkers[i]._html );
					gMap.addOverlay( marker );
				}
			}

			/*
			for ( i=0; i<moreMarkers.length; i++)
			{
				if ( moreMarkers[i] )
				{
					var marker = createMarker( moreMarkers[i]._coord, gHotelMarker, moreMarkers[i]._html );
					gMap.addOverlay( marker );
				}
			}
			*/
		}


	//	Marcatori ajax
		if ( xmlHotelIds instanceof Array  && xmlGetUrl )
		{
			if ( xmlHotelIds.length )
				getXmlHotels(gMap,xmlGetUrl,xmlHotelIds,centerLat,centerLong,xmlMarker,xmlPattern,xmlSelectedHotelId);
		}

		gMapsLoaded = true;

	}

	return gMap;
}

function addHtmlMarker( gMap, point, icon, html )
{
	var marker = createMarker( point, icon, html );
	gMap.addOverlay( marker );
	marker.openInfoWindowHtml( html );

	return marker;
}

function addMarkers(gMap,markers,icon)
{
	if ( markers instanceof Array )
	{
		for ( i in markers )
		{
			if ( markers[i]._set )
			{
				var marker = createMarker( markers[i]._coord, icon, markers[i]._html );
				gMap.addOverlay( marker );
			}
		}
	}

}

function getXmlHotels(gMap,getUrl,hotelIds,mapLat,mapLng,hotelMarker,pattern,selectedHotelId)
{
	var request = GXmlHttp.create();

	request.open('GET', getUrl + '&ts=' + (new Date()).getTime(), true);

	request.onreadystatechange = function ()
	{
		//if (request.readyState < 4)
		if (request.readyState == 4)
		{
			var xmlDoc = request.responseXML;
			if(xmlDoc)
			{
				result = new Array();
				hotels = xmlDoc.documentElement.getElementsByTagName("hotel");
				//alert("hotels.length "+hotels.length);
				if (hotels.length>0)
				{
					var selectedMarker	= null;

					for (var i = 0; i < hotels.length; i++)
					{
					//	Inizializzo il contenuto della finestra al pattern standard
						var hotelDescription	= pattern;
						
					//	Recupero gli attributi dall'xml recuperato
						hotel_id		= hotels[i].getAttribute("hotel_id");
						if ( hotelIds[hotel_id] )
						{
							hotelDescription = hotelDescription.replace( '%hotel_name%', hotels[i].getAttribute("hotel_name") );
							hotelDescription = hotelDescription.replace( '%hotel_addr%', hotels[i].getAttribute("address") );
							hotelDescription = hotelDescription.replace( '%hotel_class%', hotels[i].getAttribute("class") );
							hotelDescription = hotelDescription.replace( '%hotel_url%', urldecode( hotels[i].getAttribute("url") ) );
							hotelDescription = hotelDescription.replace( '%hotel_thumb%', urldecode( hotels[i].getAttribute("thumb_url") ) );
							hotelDescription = hotelDescription.replace( '%hotel_minrate%', hotels[i].getAttribute("minrate") );
							hotelDescription = hotelDescription.replace( '%hotel_currency%', hotels[i].getAttribute("currency") );

							/*
							hotel_name		= hotels[i].getAttribute("hotel_name");
							hotel_addr		= hotels[i].getAttribute("address");
							hotel_class		= hotels[i].getAttribute("class");
							hotel_url		= urldecode( hotels[i].getAttribute("url") );
							hotel_thumb		= urldecode( hotels[i].getAttribute("thumb_url") );
							*/
							hotel_lat		= hotels[i].getAttribute("lat");
							hotel_lng		= hotels[i].getAttribute("lng");
							/*
							hotel_minrate	= hotels[i].getAttribute("minrate");
							hotel_currency	= hotels[i].getAttribute("currency");
							*/

						//	Creo il marker per l'oggetto
							var marker = createMarker( new GLatLng(hotel_lat,hotel_lng), hotelMarker, hotelDescription );
							gMap.addOverlay( marker );
							
						//	Salvo il marker in una struttura temporanea per successivo recupero
							hotelOverlays[hotel_id] = marker;
						}
						
					//	Se l'hotel è quello selezionato, salvo il relativo marker
						if ( selectedHotelId==hotel_id )
							selectedMarker	= marker;
					}
				}

			//	Se presente, attivo il marker selezionato simulando il click
				if ( selectedMarker )
					GEvent.trigger(selectedMarker,'click');
			}
		}
	}

	request.send(null);
}