	
	
	//
	// Store Locator:
	//
	
	var map;
	var markers = [];
	var markers_index = 0;
	var reenable_updates = false;
	
	function enable_updates() {
		//ome.log('enable markers updates');
		//if (map) map.enable_markers_update = true;		
		
		ome.log('re-enable updates');
		
		// signal to moveend method that it can re-enable markers update:
		reenable_updates = true;		// because moveend is called after this function
	}
	
	var slUpdateMarkers = function(with_autozoom) {

		//ome.log('update markers');

		if (!map) return;

		var current_zoom_level = map.getZoom(); 
		//ome.log('map zoom level: '+current_zoom_level);

		var b = map.getBounds();
		
		//ome.log('map bounds: '+b);
		
		if (current_zoom_level > 1) {
			$('#map_sw').val(b.getSouthWest().toUrlValue());
			$('#map_ne').val(b.getNorthEast().toUrlValue());			
		} else {
			$('#map_sw').val('-90.0,-180.0');
			$('#map_ne').val('90.0,180.0');	
		}
		
		jQuery.post(
			'index.php?eID=slmapmarkers',
			$('#fsl').serialize(),
			function(data) {

				var box = null;

				var d = data.total;

				$('#t3_count_stores').html(d+' ');
				
				//$('#t3_msg_carrying').html(data.msg);

				map.clearOverlays(); 

				jQuery.each(
					data.stores, 
					function(indexInArray, obj) {		
						
						//ome.log('store lat,lng: '+obj.lat+', '+obj.lng);
						
						var ll = new GLatLng(obj.lat, obj.lng);
						
						if (box == null) {
							box = new GLatLngBounds(ll, ll);
						} else {
							box.extend(ll);
						}														
									
						var ico = new GIcon();
						
						ico.image = obj.st>0 ? 
										'/fileadmin/templates/theme/images/pin-boutique.png' : 
										'/fileadmin/templates/theme/images/pin-store.png';
			
						ico.iconSize = new GSize(26, 28);
						ico.iconAnchor = new GPoint(13, 28);
						ico.infoWindowAnchor = new GPoint(13, 28);
			
						//ico.shadow = '...TODO...';
						//ico.shadowSize = new GSize(12, 25);
						//ico.infoShadowAnchor = new GPoint(12, 25);
									
						//markers[markers_index] = new GMarker(ll, { icon: G_DEFAULT_ICON });
						markers[markers_index] = new GMarker(ll, { icon: ico });
						
						markers[markers_index].html_info = obj.info;
							
						if (obj.info instanceof Array) {	
							markers[markers_index].zoom_in = true;
						} else {						
							markers[markers_index].zoom_in = false;
						}
												
						map.addOverlay(markers[markers_index]);										
										

						GEvent.addListener(markers[markers_index], 'click',
							function() {
								//ome.log('marker click');		
								if (this.zoom_in) {
									if (map.getZoom() < 19) {
										var ll = this.html_info;
										slZoomIn(ll[0],ll[1],ll[2],ll[3]);
									}	
								} else {
									ome.log('open info window');
									reenable_updates = false;
									ome.log('disable markers update');
									map.enable_markers_update = false;							
									this.openInfoWindowHtml(
										this.html_info, {
											onOpenFn:enable_updates()
										}
									);
								}
							} 
						);
								
						  
						markers_index++;
					}
				); // each
																		
				//if (map.enable_autozoom) {
				if (with_autozoom && (box != null)) {			
					//ome.log('autozoom enabled, box: '+box);
					
					if (map.getZoom() >= 2) {
					
						ome.log('disable markers update');
						map.enable_markers_update = false;	
						
						map.setCenter(box.getCenter());
						
						var zoom_level = map.getBoundsZoomLevel(box);
						ome.log('autozoom to level '+zoom_level+' for box '+box);
						map.setZoom(zoom_level);
						//map.setZoom(map.getBoundsZoomLevel(box));
						
					}
												
				}									
						
					//ome.log('enable markers update');			
					map.enable_markers_update = true;		
					
				//ome.log('disable map.auto_zoom');
				//map.enable_autozoom = false;			// auto_zoom only for the initial markers loading

			},
			'json'
		);	
	} // slUpdateMarkers()
	
	
	slZoomIn = function(sw_lat, sw_lng, ne_lat, ne_lng) {
		
		map.closeInfoWindow();
		//ome.log('disable markers update');
		map.enable_markers_update = false;	
		
		var box = new GLatLngBounds(
							new GLatLng(sw_lat, sw_lng), 
							new GLatLng(ne_lat, ne_lng));
		map.setCenter(box.getCenter());
		
		var zoom_level = map.getBoundsZoomLevel(box);
		ome.log('zoom in to level '+zoom_level+' for box '+box);
		map.setZoom(zoom_level);
		
		slUpdateMarkers(false);
	}
	
	
	$(document).ready(function() { 		
				
		// Note: We can not use the "changeE event in IE because this event does not bubble up.
		//       Therefore, we need to use the "click" event.		
				
		var store_locator = $('#fsl');
		if (store_locator.length) {		//TODO: test encore utile?
	
			$('#rb01').click(function(){
	         //ome.log('rb01 changed');
				$('#slst').val('c');	// search type is CRITERIA
				slUpdateMarkers(false);
			});
			$('#rb02').click(function(){
	         //ome.log('rb02 changed');
				$('#slst').val('c');	// search type is CRITERIA
				slUpdateMarkers(false);
			});
			
			$('#sellwatches').click(function(){
	         //ome.log('sellwatches changed');
				$('#slst').val('c');	// search type is CRITERIA
				slUpdateMarkers(false);
			});
			$('#sellleather').click(function(){
	         //ome.log('sellleather changed');
				$('#slst').val('c');	// search type is CRITERIA
				slUpdateMarkers(false);
			});
			$('#selljewellery').click(function(){
	         //ome.log('selljewellery changed');
				$('#slst').val('c');	// search type is CRITERIA
				slUpdateMarkers(false);
			});
	
		} // if					

/*
 * 
 * 
		$('#sl_btsubmit').click(function(){
			//ome.log('clic clic');
			
			$('#slst').val('t');	// search type is FULLTEXT
			
			slUpdateMarkers(true);
			
			return false;
		});
*/

		$('#t3_btshow').click(function(){
			//ome.log('show stores');
			
			jQuery.post(
				'index.php?eID=slmapstores',
				$('#fsl').serialize(),
				function(data) {
					$('#t3_stores').html(data);
				},
				'html'
			); // get()
			
			return false;
		});

		if (GBrowserIsCompatible()) {			
		
			map = new GMap2(document.getElementById("google-map-container"));
			
			// custom properties:
			//ome.log('enable markers update');
			map.enable_markers_update = true;
			//map.enable_autozoom = true;
			
			GEvent.addListener(map, 'load', function() {
				//ome.load_marker();
				slUpdateMarkers(true);
			});
			map.setMapType(G_NORMAL_MAP); 
			map.setCenter(new GLatLng(31, 25), 1);
			map.setUIToDefault();			
			

			// moveend is called when zooming too 
			GEvent.addListener(map, 'moveend', function() {
				ome.log('map moveend; map center='+map.getCenter());
				if (this.enable_markers_update) {
					//ome.log('moveend call to slUpdateMarkers');
					slUpdateMarkers(false);
				}
				if (reenable_updates) {
					this.enable_markers_update = true;
					ome.log('updates re-enabled');
				}
				reenable_updates = false;
			});
			
			//Note: it is not necessary to trigger this event on
			//zoomend as moveend is automatically triggerd by zoomend.
							
			// moveend is called when zooming too 
			GEvent.addListener(map, 'zoomend', function(oldLevel, newLevel) {
				ome.log('map zoomend');
				if (oldLevel != newLevel) reenable_updates = true;
				//if (false && this.enable_markers_update) {
				//	slUpdateMarkers(false);
				//}	
			});
		
			
	 
			GEvent.addListener(map, 'infowindowopen', function() {
 				//ome.log('map.infowindowopen');
				//ome.log('disable markers update');
				map.enable_markers_update = false;
			});

			
			GEvent.addListener(map, 'infowindowclose', function() {
 				//ome.log('map.infowindowclose');		
				//ome.log('enable markers update');
				map.enable_markers_update = true;	
			});
			
			// Si la maj des markers est désactivée suite à l'ouverture d'une infoWindow,
			// mais que l'utilisateur déplace manuellement la carte, alors il faut réactiver
			// la maj des marqueurs.
			// dragEnd est appelé avant moveEnd.
			//
			// NON, on fait pas, car sinon cela fait disparaitre la infoWindow lors de la
			// maj des marqueurs, à cause de l'appel à clearOverlays();
			/*
			GEvent.addListener(map, 'dragend', function() {
 				ome.log('map.dragend');
				ome.log('enable markers update');
				map.enable_markers_update = true;
			});
			*/
		}

	});
 	
	$(document).unload(function() { 
		GUnload()
	});
/*
	ome.load_marker = function() {
		ome.log('load markers');
		
		slUpdateMarkers(true);
		
		
	} // ome.load_marker()
*/	