//Script qui gere tous les fonctionnement de google map
$(function(){
	
	// Les objects
	var bigMapConfig = {};
	var bigMap;
	
	var mapConfig = {};
	var map;
	
	var mapOpen = false;
	
	// Initialisation de la map 
	$.fn.gmap_initialize = function() {
		this.bigMapConfig = {zoom:6,
							 centerLat:46.871208,
							 centerLong:2.460938
							};
	};
	
	// Ajoute le map sur la page il reçois l'id de la map
	$.fn.gmap_addBigMap = function(mapId, lat, long) {
		this.gmap_initialize();
		var latlng = new GLatLng(lat, long);
	    this.bigMap = new GMap2(document.getElementById(mapId));
	    this.bigMap.setCenter(latlng, this.bigMapConfig.zoom);
	    this.bigMap.enableScrollWheelZoom();
	    this.bigMap.enableDoubleClickZoom();
	    this.bigMap.setUIToDefault();
	    
	    return this.bigMap;
	};

	
	// Ajoute un marker pour chaque distributeurs
	$.fn.gmap_addMarker = function (map2,marker2,html,id) {
			var elem = $(this);
        	var coord = marker2.split(',');
        	var markerLatlng = new GLatLng(coord[0], coord[1]);

			var m = new GMarker(markerLatlng);
			
			GEvent.addListener(m, "mouseover", function() {
           		m.openInfoWindowHtml(html);
          	});
			
			//Affiche la bulle d'info en cliquant sur les element de la listes des distributeurs
          	$('#list_'+id).click(function(){
    			
    			//Haut de page en animation
    			$('html, body').animate({scrollTop:400}, 'slow');
    			
    			m.openInfoWindowHtml(html);
    		});
			
			map2.addOverlay(m);
	};
	
	$.fn.gmap_getLatLon = function(address) {
    	geo.getLocations(address, function (result){
			return result.Placemark[0].Point.coordinates;
		});
	};
	
});
