var Autorise_Liste_Residence = true;

function creation_icone(i){
		var icon_categorie = new GIcon(G_DEFAULT_ICON);
			icon_categorie.image = "../img/RechercheGeo/"+i+".png";
			icon_categorie.shadow = "../img/RechercheGeo/ombre.png";
			icon_categorie.iconSize = new GSize(21,34);
			icon_categorie.shadowSize = new GSize(40, 37);
			icon_categorie.iconAnchor = new GPoint(10, 34);
			return icon_categorie;
}

function LimitesCarte() {
	//récupération des cordonnées de la carte affichée
		var limitescarte = map.getBounds();
		var pointSOuest = limitescarte.getSouthWest();
		var pointNEst = limitescarte.getNorthEast();

		var lat_so=pointSOuest.lat();
		var lat_ne=pointNEst.lat();
		var lng_ne=pointNEst.lng();
		var lng_so=pointSOuest.lng();

		//liste_residences(lat_ne,lat_so,lng_ne,lng_so);
		return {lat_ne:lat_ne, lat_so:lat_so, lng_ne:lng_ne, lng_so:lng_so};
}

function showTooltip(marker, tooltip) {
      	tooltip.innerHTML = marker.tooltip;
		var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
		var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
		var anchor=marker.getIcon().iconAnchor;
		var width=marker.getIcon().iconSize.width;
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width - 5, offset.y - point.y -anchor.y  +1));
		pos.apply(tooltip);
		tooltip.style.visibility="visible";
}

function CreateMarkerVille(point, commentaire, idville, tooltip, mgr) {

	var marker=new GMarker(point,{ icon:creation_icone("ville")});
	marker.tooltip = '<div class="tooltip ville"><div class="gauche"></div><div class="centre">'+commentaire+'</div><div class="droite"></div></div>';
	mgr.addMarker(marker, 1, 10);

	GEvent.addListener(marker, 'click', function()
		{
			tooltip.style.visibility="hidden";
			residence_ville(idville);
		});

	GEvent.addListener(marker, 'mouseover', function()
		{showTooltip(marker,tooltip);});

	GEvent.addListener(marker,"mouseout", function()
		{tooltip.style.visibility="hidden";});

	return mgr;
}

function CreateMarkerResidence(point,commentaire,id,tooltip,mgr,url) {
	var marker=new GMarker(point,{ icon:creation_icone("residence")});
	marker.tooltip = '<div class="tooltip residence"><div class="gauche"></div><div class="centre">'+commentaire+'</div><div class="droite"></div></div>';
	mgr.addMarker(marker, 11, 17);
	GEvent.addListener(marker, 'click', function()
		{
			document.location.href= url ;
		});
	GEvent.addListener(marker, 'mouseover', function()
		{showTooltip(marker,tooltip);});
	GEvent.addListener(marker,"mouseout", function()
		{tooltip.style.visibility="hidden";});

	return mgr;
}

function liste_residences( evt ) {
	if( ! Autorise_Liste_Residence ) {
		return false;
	}

	var bounds = LimitesCarte()	;

    var url = '/RechercheGeo/RechercheGeo.json.php';
    var myAjax = new Ajax.Request(
            url,
            {
                method: 'post',
				//passage des parametres en post - $H permet de recevoir les valeurs en post. Si get : pas besoin, et à la place de postBody, il faut mettre parameter
				 postBody: $H( {action:1, lat_ne:bounds.lat_ne, lat_so:bounds.lat_so, lng_ne:bounds.lng_ne, lng_so:bounds.lng_so}).toQueryString(),
					onSuccess: function( xhr ) { // en cas de succès status HTTP 200

						eval("data="+xhr.responseText);

						$("container_residence").update('<div id="liste_residence"></div>');


						$("liste_residence").update();
						_tpl1 = new Template('<div id="liste_residence" class="result_box ville_#{ville_idville}"><div class="haut"><div class="col_1"><a href="#{rewrited_url}"><img src="/upload/#{illustration_subdir}/fiche#{idfiche}_168x126_#{illustration_stamp}.jpg" alt="" /></a></div><div class="col_2"><span class="titre">#{nom}</span><br /><span class="soustitre">#{ville_label} #{num_dep} - #{nom_region}</span><p>#{intro_textile}</p></div><div class="col_3"><a class="voir_fiche" href="#{rewrited_url}"><span>Voir la fiche</span></a></div><div class="col_4"><ul><li>#{repere_texte_1}</li><li>#{repere_texte_2}</li><li>#{repere_texte_3}</li></ul></div><div class="clearer">&nbsp;</div></div><p class="col_5"><a href="#header" onclick="window.scrollTo(0,0);return false;">Haut de page</a></p></div>');

						data.residence.each( function( residences ) {
							$("liste_residence").insert( {top:_tpl1.evaluate( residences )} );
						});



						$("liste_ville").update();
						_tpl2 = new Template('<li style="cursor:pointer" onclick="residence_ville(#{idville});">#{label}</li>');

						data.ville.each( function( ville ) {
							$("liste_ville").insert( {bottom:_tpl2.evaluate( ville )} );
						});

					},

					onFailure: function() { // executé en cas d'erreur quelquonque
							alert("Une erreur inconnue s'est produite.");
					}
            });
}

function residence_ville( idville ) {
	Autorise_Liste_Residence = false;

    var url = '/RechercheGeo/RechercheGeo.json.php';
    var myAjax = new Ajax.Request(
            url,
            {
                method: 'post',
				//passage des parametres en post - $H permet de recevoir les valeurs en post. Si get : pas besoin, et à la place de postBody, il faut mettre parameter
				 postBody: $H({action:2, idville: idville}).toQueryString(),
					onSuccess: function( xhr ) { // en cas de succès status HTTP 200

						eval("data="+xhr.responseText);

						var _ville = data.ville[0];

						map.setCenter(new GLatLng( _ville.lat_ville , _ville.lng_ville ) , 11);

						$("container_residence").update('<div id="liste_residence"></div>');


						$("liste_residence").update();
						_tpl1 = new Template('<div id="liste_residence" class="result_box ville_#{ville_idville}"><div class="haut"><div class="col_1"><a href="#{rewrited_url}"><img src="/upload/#{illustration_subdir}/fiche#{idfiche}_168x126_#{illustration_stamp}.jpg" alt="" /></a></div><div class="col_2"><span class="titre">#{nom}</span><br /><span class="soustitre">#{ville_label} #{num_dep} - #{nom_region}</span><p>#{intro_textile}</p></div><div class="col_3"><a class="voir_fiche" href="#{rewrited_url}"><span>Voir la fiche</span></a></div><div class="col_4"><ul><li>#{repere_texte_1}</li><li>#{repere_texte_2}</li><li>#{repere_texte_3}</li></ul></div><div class="clearer">&nbsp;</div></div><p class="col_5"><a href="#header" onclick="window.scrollTo(0,0);return false;">Haut de page</a></p></div>');

						data.residence.each( function( residences ) {
							$("liste_residence").insert( {top:_tpl1.evaluate( residences )} );
						});



						$("liste_ville").update();
						_tpl2 = new Template('<li style="cursor:pointer" onclick="residence_ville(#{idville});">#{label}</li>');

						data.ville.each( function( ville ) {
							$("liste_ville").insert( {bottom:_tpl2.evaluate( ville )} );
						});

						Autorise_Liste_Residence = true;
					},

					onFailure: function() { // executé en cas d'erreur quelquonque
							alert("Une erreur inconnue s'est produite.");
					}
            });

}