/*
 * La carto dans page_liste_hotels_manif.php
 * Consomme 3 variables globales au format JSON : 
 * 		+ geo_hotels : les hôtels
 * 		+ geo_lieu : le lieu de l'événement
 * 		+ markers : les marqueurs hôtel 
 */
 	map = null;
 
	function initGMap() {
		map = new google.maps.Map2(document.getElementById("carto"));
		
		map.addControl(new GLargeMapControl());
		
		map.addControl(new GOverviewMapControl());
		
		// map.setCenter(new google.maps.LatLng(geo_lieu.lat, geo_lieu.lng), 15);
		
		// Placer l'événement
		var point = new GLatLng(geo_lieu.lat, geo_lieu.lng, false);
		
		// var eventIcon = MapIconMaker.createMarkerIcon({width: 50, height: 50, primaryColor: "#0000ff"});
		// var eventIcon = MapIconMaker.createMarkerIcon({width: 50, height: 50, primaryColor: "#0000ff"});

		var eventIcon = createIcon('lieu');

		var markerEvent = new GMarker(point, {icon: eventIcon});

		GEvent.addListener(markerEvent, "click", function(latlng){
			this.openInfoWindowHtml(
				'<b>' + geo_lieu.NOM_FR_EVENT + '</b>' + '<br />' +
				geo_lieu.NAME_LIEU,
				{maxWidth: 250}
			);
		});
		
		map.addOverlay(markerEvent);			
		
		// Nécéssaire pour afficher adapter le zoom en fonction de l'emplacement des markeurs
		var bounds = new GLatLngBounds();

		var hotelIcon = createIcon('hotel');
		
		// Placer les hôtels
		for(var i=0;i<geo_hotels.length;i++) {
			if(geo_hotels[i] != null) {
				
				var point = new GLatLng(geo_hotels[i].lat, geo_hotels[i].lng, false);
				
				bounds.extend(point);
				
				var marker = new GMarker(point, {icon: hotelIcon});

				marker.ID = geo_hotels[i].ID_HOTEL;
				
				GEvent.addListener(marker, "click", function(latlng){
					this.openInfoWindowHtml(
						getGmapWinContent(this.ID),
						{maxWidth: 250}
					);
				});										
				
				markers.push(marker);
				
				map.addOverlay(marker);
			}
		}
		
		// map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		
		map.setCenter(markerEvent.getLatLng(), parseInt(geo_lieu.zoom));
	}
	/*
	 * @param {Integer} ID identifiant de l'hôtel recherché
	 */
	// retourne un hôtel par rapport à son ID_HOTEL
	function getHotelById(ID) {
		for(var i=0;i<geo_hotels.length;i++) {
			var hotel = geo_hotels[i];
			if (hotel != null) {
				//alert(geo_hotels[i].ID_HOTEL + '-' + ID);				
				if (hotel.ID_HOTEL == ID) {
					return hotel;
				}
			}
		}
		return null;
	}
	
	// retourne un marqueur hôtel par rapport à son ID_HOTEL
	function getMarkerByID(ID) {
		for(var i=0;i<markers.length;i++) {
			if (markers[i].ID == ID) {
				return markers[i];
			}
		}
		return null;
	}
	
	// Afficher la bulle d'information
	function showMarkerWinInfo(ID) {
		marker = getMarkerByID(ID);
		
		if (marker) {
			marker.openInfoWindowHtml(
				getGmapWinContent(marker.ID),
				{maxWidth: 250}
			);
		}else {
			// Fermer l'info bulle si l'hôtel n'est pas localisé
			map.closeInfoWindow();
		}
	}
	
	// Retourne le contenu (html) de la bulle d'aide
	function getGmapWinContent(ID) {
		var hotel = getHotelById(ID);
		
		var html = "";
		if (hotel != null) {
			html += '<div align="left">';
			html += '<img src="' + ressource_dir + '/images/Public/' + hotel.CATEGORIE_HOTEL + '_etoiles.gif"/>' + '<br />';
			html += '<a href="#" onclick="voirpanelhotel(\'' + hotel.ID_LINK_EVENT_HOTEL + '\',\'' + hotel.NOM_HOTEL + '\',\'' + hotel.CATEGORIE_HOTEL + '\');return false;" class="liste_titre">' +  hotel.NOM_HOTEL + '</a>';
			html += '</div>';
			
		}else {
			html = "non trouvé";
		}
		return html;
	}

	// 19/04/2010 -- eric -- modification des marqueurs de carto
	// Créer l'icon voir le site
	// http://www.powerhut.co.uk/googlemaps/custom_markers.php
	// Un répertoire par icon
	
	function createIcon(sIconKind) {
		var icon = new GIcon();
		var cartoIconsPath = '/images/Public/carto/';
		icon.image = ressource_dir + cartoIconsPath + sIconKind + '/image.png';
		icon.printImage = ressource_dir + cartoIconsPath + sIconKind + '/printImage.gif';
		icon.mozPrintImage = ressource_dir + cartoIconsPath + sIconKind + '/mozPrintImage.gif';
		if (sIconKind == "lieu") {
			icon.iconSize = new GSize(37,54);
			icon.shadowSize = new GSize(64,54);
		} else {
			icon.iconSize = new GSize(29,42);
			icon.shadowSize = new GSize(50,42);
		}
		icon.shadow = ressource_dir + cartoIconsPath + sIconKind + '/shadow.png';
		icon.transparent = ressource_dir + cartoIconsPath + sIconKind + '/transparent.png';
		icon.printShadow = ressource_dir + cartoIconsPath + sIconKind + '/printShadow.gif';
		icon.iconAnchor = new GPoint(0,54);
		icon.infoWindowAnchor = new GPoint(19,0);
		icon.imageMap = [36,0,36,1,36,2,36,3,36,4,36,5,36,6,36,7,36,8,36,9,36,10,36,11,36,12,36,13,36,14,36,15,36,16,36,17,36,18,36,19,36,20,36,21,36,22,36,23,36,24,36,25,36,26,36,27,36,28,36,29,36,30,36,31,36,32,36,33,36,34,36,35,36,36,36,37,36,38,36,39,36,40,26,41,26,42,25,43,25,44,24,45,23,46,23,47,22,48,21,49,21,50,20,51,20,52,19,53,17,53,17,52,16,51,15,50,15,49,14,48,13,47,13,46,12,45,11,44,11,43,10,42,0,41,0,40,0,39,0,38,0,37,0,36,0,35,0,34,0,33,0,32,0,31,0,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,0,0];

		return icon;

	}