/**
* Ce fichier contient tout le code JS necessaire pour afficher une carte Google Map
* 
*  Pour que cela fonction il faut inserer 4 choses dans votre code HTML (TPL ou PHP):
*  - La balise HTML suivante dans le header de la page pour activer GMap (ATTENTION NE RIEN MODIFIER DANS CETTE BALISE ELLE CONTIENT NOTRE ID GOOGLE MAP)
*  		=>	<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA-WxewXkD4xgRJL65nC0oGBSwSqmuvtnNhBFR7-L7nk_bse5AyxQ6z6yUfWC8lyqRHy5Ek7VDRfapaw&sensor=true_or_false"
            type="text/javascript"></script>
*  - La balise HTML suivante là où vous souhaitez que la carte se trouve 
*  		=> <div id="map_canvas" style="width: 500px; height: 300px"></div>
*  - L'appel à la fonction JS initializeMap(...) (soit dans le unload soit lors d'un appel specifique)
*  	avec les parametres que vous souhaitez en fonction de la configuration voulue
*  - L'appel à la fonction JS 
*  		=> onunload="GUnload()"
*  
*  Pour modifier le comportement du composant se référer à la document en ligne sur
*  http://code.google.com/intl/fr/apis/maps/documentation/index.html
*/

/**
 * Fonction qui initialise l'affichage de la carte GMap
 * pAddress String : Adresse a afficher avec la syntaxe "1 rue d'ici, 35000 RENNES"
 * pDisplayInfo Boolean : Flag indiquant si on veut afficher une info bulle contenant l'adresse (implique un décalage du centrage)
 * pControlMode Integer : compris entre 0 et 5 
 * 		=> 0 Aucun controles
 * 		=> 1 Petit controle de position et zoom
 * 		=> 2 Grand controle de position et zoom
 * 		=> 3 Petit controle de position et zoom avec selection du type de carte
 * 		=> 4 Grand controle de position et zoom avec selection du type de carte
 * 		=> 5 Controle complet next gen
 */
function initializeMap(pAddress, pDisplayInfo, pControlMode) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
		var geocoder = new GClientGeocoder();

		// Create our "tiny" marker icon
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
		//		markerOptions = { icon:blueIcon };

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

		// Set up our GMarkerOptions object literal
		markerOptions = {
			icon : tinyIcon
		};

		//Selection de l'affichage des controles
		switch (pControlMode) {
		case 0:
			break;
		case 3:
			map.addControl(new GMapTypeControl());
		case 1:
			map.addControl(new GSmallMapControl());
			break;
		case 4:
			map.addControl(new GMapTypeControl());
		case 2:
			map.addControl(new GLargeMapControl());
			break;
		case 5:
			map.setUIToDefault();
			break;
		}		
		geocoder.getLatLng(pAddress, function(point) {
			if (!point) {
				alert(pAddress + " not found");
			} else {
				map.setCenter(point, 15);

				var marker = new GMarker(point, markerOptions);

				map.addOverlay(marker);
				if (pDisplayInfo) {
					marker.openInfoWindowHtml(pAddress);
				} else {
					GEvent.addListener(marker, "click", function() {
						var reg=new RegExp("[,]", "g");
						var oInfo = pAddress.replace(reg,"<BR>");
						marker.openInfoWindowHtml(oInfo);
					});
				}
			}
		});
	}
}