tx_pyramidesgmaps_modeAdresse = {
	ref: null,
	config: null,
	intval:"",
	
	marker: null,
	
	init: function(){	
		var mod = this;
		
		// on cache la liste html (n'est utile que pour ceux qui ont désactivé le javascript)
		jQuery(".tx-decgmaps-modePlan").hide();
	
		var reference = this;
		jQuery.extend(this.ref, {
			tx_pyramidesgmaps_modeAdresse_MakeMarker: this.ref.makeMarker,
			makeMarker: function(node){
				var backupMarker = this.tx_pyramidesgmaps_modeAdresse_MakeMarker(node);

				//GEvent.addListener(backupMarker, "infowindowopen", this.slideshowHI); 
				GEvent.addListener(backupMarker, "infowindowopen",function() {					
					reference.slideshowHI();
				});
				GEvent.addListener(backupMarker, "infowindowclose", function() {					
					reference.deleteSetInterval();
				});
				
				return backupMarker;
			}
		});
		
		
		jQuery.extend(this.ref.modules.tx_decgmaps_addressLookup, {
			tx_decgmaps_addressLookup_findLocation: this.ref.modules.tx_decgmaps_addressLookup.findLocation,
			findLocation: function(){
				
				
				var adrParts = [];
				var val;
				var hasZip = false;
				
				if(this.config.showAdr){
					val = jQuery("#"+this.ref.mapId+"-addressField").val();
					if(val != "") adrParts.push(val);
				}
		
				if(this.config.showZip){
					val = jQuery("#"+this.ref.mapId+"-zipField").val();
					if(val != "") {
						adrParts.push(val);
						hasZip = true;
					}
				}
		
				if(this.config.showCity){
					val = jQuery("#"+this.ref.mapId+"-cityField").val();
					if(val != ""){
						if(hasZip){
							adrParts[adrParts.length-1] += " " + val;
						} else adrParts.push(val);
					}
				}
		
				if(this.config.showCountry){
					val = jQuery("#"+this.ref.mapId+"-countryField").val();
					if(val != "") adrParts.push(val);
				}
				
				this.search = adrParts.join(", ");
				
				if(this.search == ""){
					this.ref.gmap.removeOverlay(this.marker);
					this.ref.gmap.setCenter(new GLatLng(parseFloat(this.ref.config.defaut.lat), parseFloat(this.ref.config.defaut.lng)), parseInt(this.ref.config.defaut.zoom));
					this.ref.mng.refresh();
				} else {					
					var mod = this;
							
					//this.geocoder.setBaseCountryCode('fr');
					
					// voir à garder
					/*var pointSW = new GLatLng(41, 10);
					var pointNE = new GLatLng(51, -6);
					this.geocoder.setViewport(new GLatLngBounds(pointSW, pointNE));*/
					
					
					this.geocoder.getLatLng(this.search, function(point){
						// on récupère la latitude et la longitude
						var latA = point.lat();
						var lngA = point.lng();
						
						var diff = 1000000000;
						
						// on boucle sur tous les POI pour trouver le point le plus proche de la recherche
						jQuery(".tx-decgmaps-modePlan li").each(function(){
							var classn = jQuery(this).attr("class");
							
							var coord = classn.split("|");
							var latB = coord[0];
							var lngB = coord[1];
							
							var calc = (latB - latA)*(latB - latA) + (lngB - lngA)*(lngB - lngA);
							calc = Math.sqrt(calc);
							
							//alert(calc+" - "+jQuery(this).text());
							if(calc < diff){
								diff = calc;
								pyramideProche = coord;
							}	
						});
						
						
						// on prend les valeurs les 2 valeurs les plus loins des 2 latitudes et longitudes
						var latc;
						var lngc;
						
						if(latA > pyramideProche[0]){
							latc = pyramideProche[0] + (latA - pyramideProche[0]) / 2;
						}else{
							latc = latA + (pyramideProche[0] - latA) / 2;
						}
						if(lngA > pyramideProche[1]){
							lngc = lngA - (lngA - pyramideProche[1]) / 2;
						}else{
							lngc = pyramideProche[1] - (pyramideProche[1] - lngA) / 2;
						}

																					 
						// on calcule le zoom nécessaire
						var bounds = new GLatLngBounds(new GLatLng(pyramideProche[0],pyramideProche[1]),new GLatLng(point.lat(), point.lng()));
						if(mod.ref.gmap.getBoundsZoomLevel(bounds) == 1){
							// on inverse l'ordre des points
							var bounds = new GLatLngBounds(new GLatLng(point.lat(), point.lng()),new GLatLng(pyramideProche[0],pyramideProche[1]));
						}
						var zoom = Math.max(1, mod.ref.gmap.getBoundsZoomLevel(bounds));
						
						//alert(zoom);
						if(point){
							// on affiche le point (function setPoint)
							mod.marker = new GMarker(new GLatLng(point.lat(), point.lng()), {clickable: false, title: mod.search});
							mod.ref.gmap.addOverlay(mod.marker);
							//mod.ref.gmap.setCenter(new GLatLng(latc, lngc),parseInt(zoom));
							mod.ref.gmap.setCenter(bounds.getCenter(),zoom);
							mod.ref.mng.refresh();
						} else this.showError(eval(this.ref.LL.address_notfound));	
					});
				}	
				
				
			}
		});
		
		
	},
	
	changeLocation: function(){
		var adrParts = [];
		var val;
		
		// on remet la zdl sur l'option zéro
		jQuery("#" + this.ref.mapId + " .tx-decgmaps-regionLookup select option:first").attr("selected", "selected");
		
		val = jQuery("#" + this.ref.mapId + " .tx-decgmaps-centreLookup select").val();
		
		if(val == 0){
			this.ref.gmap.setCenter(new GLatLng(parseFloat(this.ref.config.defaut.lat), parseFloat(this.ref.config.defaut.lng)), parseInt(this.ref.config.defaut.zoom));
			this.ref.mng.refresh();
		} else {
			centre = this.centres["cle_"+val];
			coords = centre.split("|");
			
			var t = jQuery("#" + this.ref.mapId + " .tx-decgmaps-centreLookup select option:selected").text();
			
			// afficher la bulle
			this.ref.loadInfoWindowContent(this.indices["cle_"+val]);
			
			this.ref.gmap.setCenter(new GLatLng(parseFloat(coords[0]), parseFloat(coords[1])), parseInt(this.ref.mng.maxZoom_ - 4));
			this.ref.mng.refresh();
		}
	},
	
	slideshowHI:function(){
			var nb = jQuery(".int_slideshow").length;	
			
			if(nb > 1){
				intval=setInterval(this.slideSwitch, 5000);
			}
	},
	
	slideSwitch:function(){
		var active = jQuery(".int_slideshow_active");
	
		if(active.length == 0) active = jQuery(".int_slideshow:last");
	
		var next =  active.next().length ? active.next() : jQuery(".int_slideshow:first");
	
		active.addClass("int_slideshow_last-active");
	
		next.css({opacity: 0.0})
			.addClass("int_slideshow_active")
			.animate({opacity: 1.0}, 1000, function() {
				active.removeClass("int_slideshow_active int_slideshow_last-active");
			});
	},
	
	deleteSetInterval:function(){
		clearInterval(intval);
	}
}
