// variable globales
//var geocoder = new GClientGeocoder();
var map = '';
var container_map;
var tableau_resultat = new Array();

//fonction qui réencode les texte
function HTMLEncode(wText){
if(typeof(wText)!="string"){
wText=wText.toString();};
wText=wText.replace(/&/g, "&amp;") ;
wText=wText.replace(/"/g, "&quot;") ;
wText=wText.replace(/</g, "&lt;") ;
wText=wText.replace(/>/g, "&gt;") ;
wText=wText.replace(/'/g, "&#146;") ;
return wText;};


function addslashes(ch) {
ch = ch.replace(/\\/g,"\\\\")
ch = ch.replace(/\'/g,"\\'")
ch = ch.replace(/\"/g,"\\\"")
return ch
}

// fonction qui créé le marker, on defini dedans les event listener etc...
function createMarker(point,nom,contenu) {
		var imageSRC = "/extension/design_valority/design/design_valority/images/picto_maison.gif";
    var blueIcon = new GIcon(G_DEFAULT_ICON);
		//blueIcon.image = img;

		// on efface l'ombre par default
		blueIcon.shadowSize = new GSize(0, 0);
		blueIcon.iconSize = new GSize(17, 18);
		blueIcon.image = imageSRC;
		// on definit les option qui seront utilisé pour l'instanciation
		markerOptions = { icon:blueIcon };
		// on instancie par le constructeur en precisant les coordonnées et les options du marker
	
    var marker = new google.maps.Marker(point, markerOptions);
		// action sur l'evement mouseover sur le marker
		// 	GEvent.addListener(marker, "mouseover", function() {
		//	marker.openInfoWindowHtml(nom+" <br>click pour plus d'information");
		//  });
		// action sur l'evement mouseout sur marker
    GEvent.addListener(marker, "mouseout", function() {
    });
      // on associe le contenu de la fenetre du point au données complementaires totales

      marker.bindInfoWindow(contenu);
      return marker; // on retourne le marker
}

function initialize(nom_div) {
  
   if (GBrowserIsCompatible()) {
   	
   	container_map = nom_div;
   	
   	map = new GMap2(document.getElementById(nom_div)); // on instancie la carte
   	geocoder = new GClientGeocoder(); // on declare le client coder
   	map.addControl(new GSmallMapControl());
    
   	// on centre la carte
		map.setCenter(new GLatLng(45.7637913,4.8339774), 10);
		// action si on sort de la carte
		GEvent.addListener(map, "mouseout", function() {
															map.closeInfoWindow();//on ferme toute les fenetres des marker
      								});
    //YK : zoom min et max
    GEvent.addListener(map, "zoomend", function(a,b) {
												if(b < 10){
													map.setZoom(10);
												}else if(b > 15){
													map.setZoom(15);
												}else{
													map.setZoom(b);
												}
      								});
      
   	/*YK: affectation des evenements aux inputs*/
   	var inputList = document.getElementById('full_liste_annonce_recherche');
   	if (inputList) {
      var inputListArray = inputList.getElementsByTagName('input');
      for(i=0;i<inputListArray.length;i++){
        if(inputListArray[i].type.toLowerCase()=="checkbox"){
          inputListArray[i].onclick = updateMap;
        }
        else if(inputListArray[i].type.toLowerCase()=="text"){
          inputListArray[i].onchange = updateMap;
        }
      }
    }
   	/*fin affectation des evenementsaux inputs*/
	} else { 
		alert("navigateur incompatible");
	}
}

//fonction qui retourne un marker a partir d'un adresse, 
//elle utilise un geocoder pour passer d'une adresse a des coordoné gps, 
//elle apelle aussi la fonction create marker pour faire des marker personnalisé

function showAddress(nom,cp,contenu,picto,b) {
if(b==1)alert(b);
geocoder = new GClientGeocoder();
  if (geocoder) {
     //fonction qui recupere les adresses en coordonnées
	var test=geocoder.getLatLng(
      cp + " France", function (point) {
					if (!point) {
					//showAddress2(nom,cp,contenu,picto,nb);
					  //alert(cp + " France : n'est pas connu sur googlemaps");
					  return true;
				 } else {
					  var marker = createMarker(point,nom,contenu,picto); // on apelle la fonction createMarker pour personnalisé le markner
					  map.addOverlay(marker); // on ajoute a la carte le markers
					  
					}
				}
			);
	} else {alert("geocoder inconnu");}
}


function initialize_vmb(nom_div) {
   if (GBrowserIsCompatible()) {
	
	map = new GMap2(document.getElementById(nom_div)); // on instancie la carte
	//on ajoute des controle a la carte
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	//on centre la carte
	GEvent.addListener(map, "mouseout", function() {
		map.closeInfoWindow();//on ferme toute les fenetres des marker
      });
  	//YK : zoom min et max
  GEvent.addListener(map, "zoomend", function(a,b) {
																				if(b < 10){
																					map.setZoom(10);
																				}else if(b > 15){
																					map.setZoom(15);
																				}else{
																					map.setZoom(b);
																				}
      								});


      } else { alert("navigateur incompatible");}
 
}


function showAddress_vmb(nom,cp,contenu,picto) {
	
	geocoder = new GClientGeocoder(); // on declare le client coder
  if (geocoder) {
     //fonction qui recupere les adresses en coordonnées
	geocoder.getLatLng(
      cp + " France", function (point) {
					if (!point) {
					//showAddress2(nom,cp,contenu,picto,nb);
					alert(cp + " France : n'est pas connu sur googlemaps");
				 } else {
					  var marker = createMarker(point,nom,contenu,picto); // on apelle la fonction createMarker pour personnalisé le markner
					  // valentin //
						map.addOverlay(marker); // on ajoute a la carte le markers
						map.setCenter(point,15); 
					}
				}
			);
	} else {alert("geocoder inconnu");}
}

function showAddress_vmb_mairie(nom,adresse,cp,contenu,picto) {
	
	geocoder = new GClientGeocoder(); // on declare le client coder
  if (geocoder) {
     //fonction qui recupere les adresses en coordonnées
	geocoder.getLatLng(
      adresse + " France", function (point) {		  
					if (!point) {
					//showAddress2(nom,cp,contenu,picto,nb);
					//alert(cp + " France : n'est pas connu sur googlemaps");
				 } else {
					  var marker = createMarker(point,nom,contenu,picto); // on apelle la fonction createMarker pour personnalisé le markner
					  // valentin //
						map.addOverlay(marker); // on ajoute a la carte le markers
						map.setCenter(point,15); 
					}
				}
			);
	} else {alert("geocoder inconnu");}
}

function showAddress2(nom,cp,contenu,picto,nb) {

  if (geocoder) {
     //fonction qui recupere les adresses en coordonnées
	geocoder.getLatLng(
      cp + " France", function (point) {
					if (!point) {
						if(nb>10)
						{alert(cp + " France : n'est pas connu sur googlemaps");}
						else
						{showAddress2(nom,cp,contenu,picto,nb+1);}
					} else {
					  var marker = createMarker(point,nom,contenu,picto); // on apelle la fonction createMarker pour personnalisé le markner
					  map.addOverlay(marker); // on ajoute a la carte le markers
					}
				}
			);
	} else {alert("geocoder inconnu");}
}

	
function centrer(point,information_survol,departement){

	if(departement==75||departement==91||departement==92||departement==77||departement==93||departement==94||departement==95||departement==78){
		map.openInfoWindowHtml(point, information_survol+"<br> Cliquez sur l'icone pour plus de d&eacute;tails",{pixelOffset: new GSize(0,-30)});
	}
	else
	{
		map.closeInfoWindow();
	}
	//map.setCenter(point,5);
	map.openInfoWindowHtml(point, information_survol+"<br> Cliquez sur l'icone pour plus de d&eacute;tails",{pixelOffset: new GSize(0,-30)});
}

function centrer_par_code_postal(cp){
	if((cp.length==5) && (!isNaN(cp)))
	{
	  if (geocoder) {
		 //fonction qui recupere les adresses en coordonnées
		geocoder.getLatLng(
		  cp + " France", function (point) {
						if (!point) {
						  alert(cp + " Fraance : n'est pas connu sur googlemaps");
					 } else {
							map.setCenter(point,14);
						}
					}
				);
		} else {alert("geocoder inconnu");}
	}
}



function timer_carte(num,b){
	if(b==1){alert(tableau_resultat[num][1])}
		showAddress(tableau_resultat[num][0],tableau_resultat[num][1],tableau_resultat[num][2],tableau_resultat[num][3],b);
		taille = tableau_resultat.length;
		if(b==1){tableau_resultat.length}
		if(num < taille)
		{
			setTimeout("timer_carte("+ (num+1) +")",400);
		}
}

function placerMarkers(){
	var marker = "";
	var point = "";
	if(tableau_resultat.length > 0){
		for(i=0;i<tableau_resultat.length;i++){
			point = new GLatLng(parseFloat(tableau_resultat[i][2]),parseFloat(tableau_resultat[i][3]));
			marker = createMarker(point,tableau_resultat[i][0],tableau_resultat[i][4]); // on apelle la fonction createMarker pour personnalisé le markner
			map.addOverlay(marker)
		}
	}
}



function initialize_map_etudiant(div_etudiant,codepostal,lat,long) 
{
	if (GBrowserIsCompatible()) 
	{
	
		map = new GMap2(document.getElementById(div_etudiant)); // on instancie la carte
		//on ajoute des controle a la carte
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());

		//on centre la carte
		GEvent.addListener(map, "mouseout", function() 
		{
			map.closeInfoWindow();//on ferme toute les fenetres des marker
      	});
	
  		//YK : zoom min et max
  		GEvent.addListener(map, "zoomend", function(a,b) 
		{
										  
//	    	var adresse = codepostal +',france';
//			var geocoder = new google.maps.ClientGeocoder();
//	   		geocoder.getLatLng(adresse, function (coord) 
//			{
          
		 
				a = lat;
				c =	long;
				g_objGLatLng = new google.maps.LatLng(a,c)
   				map.setCenter(g_objGLatLng);

 //   		});
		
			if(b < 5){
				map.setZoom(5);
			}else if(b > 18){
				map.setZoom(18);
			}else{
				map.setZoom(b);
			}
      			
		});

	} else { alert("navigateur incompatible");}
 
}
/*
function initialize_map_etudiant(div_etudiant,lat,long) 
{
	if (GBrowserIsCompatible()) 
	{
	
		map = new GMap2(document.getElementById(div_etudiant)); // on instancie la carte
		//on ajoute des controle a la carte
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());

		//on centre la carte
		GEvent.addListener(map, "mouseout", function() 
		{
			map.closeInfoWindow();//on ferme toute les fenetres des marker
      	});
	
  		//YK : zoom min et max
  		GEvent.addListener(map, "zoomend", function(a,b) 
		{
										  
          
        		var a = 45;
        		var b = 4; 
		 
				g_objGLatLng = new google.maps.LatLng(a,b)
   				map.setCenter(g_objGLatLng);

		
			if(b < 5){
				map.setZoom(5);
			}else if(b > 18){
				map.setZoom(18);
			}else{
				map.setZoom(b);
			}
      			
		});

	} else { alert("navigateur incompatible");}
 
}

*/
//function showAddress_map_etudiant(nom,lat,long,contenu,picto) 
function showAddress_map_etudiant(nom,cp,contenu,picto,lat,long) 
{
	
	
	
	geocoder = new GClientGeocoder(); // on declare le client coder
	if (geocoder)
	{
		 //fonction qui recupere les adresses en coordonnées
		geocoder.getLatLng(cp + " France", function (point) 
		{
			if (!point) 
			{
				alert(cp + " France : n'est pas connu sur googlemaps");
			} 
			else
			{
			var	a = lat;
			var	b =	long;
		g_objGLatLng = new google.maps.LatLng(a,b)
		var marker = createMarker(g_objGLatLng,nom,contenu,picto);
		map.addOverlay(marker); // on ajoute a la carte le markers

		map.setCenter(g_objGLatLng,15);
		
			}
		});
		
	} 
	else 
	{
		alert("geocoder inconnu");
	}
}
