var mapEngine = {
		
		init: function(){
	
				this.latN = null;
				this.latS = null;
				this.lonN = null;
				this.lonS = null;
				
				this.zoom = 5;
				this.map = new GMap2(document.getElementById("mapEngine"));
				
				this.legend = new GCustomLegend();
				this.map.addControl(this.legend);
				
				this.map.setCenter(new GLatLng(46.604167, 2.285156), this.zoom);
				this.map.setUIToDefault();
				this.getPoi(this.zoom);
				return this.map;
		},
		
		getPoi:function(zoomLevel,point){
				
				this.zoom = zoomLevel;
				var map = this.map;
				
				
				this.latN = map.getBounds().getNorthEast().lat();
				this.latS = map.getBounds().getSouthWest().lat();
				this.lonN = map.getBounds().getNorthEast().lng();
				this.lonS = map.getBounds().getSouthWest().lng();
				
				if(this.zoom <= 6){ var context = "region"; }
				if(this.zoom < 12 && this.zoom > 6){ var context = "ville";}
				if(this.zoom >= 12 ) { var context = "resto"; }
				if(this.zoom == 0) { var context = "adresse"; }
				
				$("#mapLegendLoading").ajaxStart(function(){
					   $(this).show();
				});
				
				$("#mapLegendLoading").ajaxStop(function(){
					  $(this).hide();
				}); 
				
				$.ajax({
					   type: "GET",
					   async: true,
					   url: "control.php?action=getMapIndex&zoom="+context+"&latN="+this.latN+"&lonN="+this.lonN+"&latS="+this.latS+"&lonS="+this.lonS,
					   success: function(msg){
							map.clearOverlays();
							if (msg != ""){
					     		
					     		var marker = [];
					     		var points = msg.split("|");
					     		
					     		var bounds = new GLatLngBounds();
				     			
					     		
					     		for(var i=0;i<parseInt(points.length-1);i++){
					     			var lat = points[i].split("#")[0];
					     			var lon = points[i].split("#")[1];
					     			if (context != "resto" && context != "adresse"){
					     				var legend = $('<div style="padding:2px;" class="sizesection"><p class="bold">'+points[i].split("#")[2]+'</p><p>'+points[i].split("#")[3]+' restaurant(s)</p></div>').get(0);
					     			} else {
					     				var legend = $('<div style="padding:2px;" class="sizesection"><p class="bold">Restaurant '+points[i].split("#")[2]+'</p></div>').get(0);
					     			}
					     			var latLon = new GLatLng(lat, lon);
					     			
					     			var html = points[i].split("#")[3];
					     			var marker = createMarker(map, latLon, legend, context, html);
					     			if (context != "region"){
					     				bounds.extend(latLon);
					     			}
					     			
					     			
					     		}
					     		if (context == "adresse"){
					     			bounds.extend(point);
					     			var Icon = new GIcon(G_DEFAULT_ICON);
							          Icon.image = "assets/map_ville.gif";
							          Icon.iconSize = new GSize(25, 25);
							          markerOptions = { icon:Icon };
							        var marker1 = new GMarker(point,markerOptions);
							        map.addOverlay(marker1);
						     		var zoomB=Math.min(map.getBoundsZoomLevel(bounds));
					     	        //var mapCenter = bounds.getCenter();
					     	        //map.setCenter(mapCenter,zoomB);
					     		}
					     		
					     	}
					   }
			    });
		},
		
		removeEventDrag:function(){
			GEvent.removeListener(dragEvent);
		},
		
		removeEventZoom:function(){
			GEvent.removeListener(zoomEvent);
		},
		
		getAdresse:function(adresse, pays){
			geocoder = new GClientGeocoder();
			
			if (geocoder) {
			    geocoder.getLatLng(
			      adresse + "," + pays,
			      function(point) {
			        if (!point) {
			          alert(adresse + " not found");
			        } else {
			          GEvent.removeListener(zoomEvent);
			          map.setCenter(point, 15);
			          var Icon = new GIcon(G_DEFAULT_ICON);
			          Icon.image = "assets/map_ville.gif";
			          Icon.iconSize = new GSize(25, 25);
			          markerOptions = { icon:Icon };
				      var marker1 = new GMarker(point,markerOptions);
				      map.addOverlay(marker1);
			          mapEngine.getPoi(0, point);
			        }
			      }
			    );
			  }
			
		}
		
	
}

window.GCustomLegend = function () {}
window.GCustomLegend.prototype = new GControl;
window.GCustomLegend.prototype.initialize = function(map) {
	var me = this;
	this.loadingRefCount = 0;

	var html = '<div style="border:1px solid black;width:140px;padding:5px;background:white" id=\"mapLegendContainer\"><div id="mapLegendInnerList">';
	html += '<p class="sizesection"><img src="assets/map_region.gif" border="0" align="center" width="25" style="vertical-align:middle"/>&nbsp;&nbsp;&nbsp;Régions</p>';
	html += '<p class="sizesection"><img src="assets/map_ville.gif" border="0" align="center" width="25" style="vertical-align:middle"/>&nbsp;&nbsp;&nbsp;Villes</p>';
	html += '<p class="sizesection"><img src="assets/map_resto.gif" border="0" align="center" width="25" style="vertical-align:middle"/>&nbsp;&nbsp;&nbsp;Restaurants</p>';
	html += '</div>';
	
	html += '<div id="mapLegendLoading" style="display:none;margin-top:10px"><p class="sizesection center"><img src="assets/ajax-loader1.gif" style="vertical-align:middle">&nbsp;&nbsp;&nbsp;Chargement...</p></div>';
	html += '</div>';

	me.panel = $(html).get(0);
	map.getContainer().appendChild(me.panel);

	return me.panel;
}

window.GCustomLegend.prototype.getDefaultPosition = function() {
	return new GControlPosition(
		G_ANCHOR_TOP_RIGHT, new GSize(10, 50)
	);
}



