/**
 * Fonctionnement des items de la recherche géographique
 * 
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-02-05
 * @version	1.0
 * @return	void
 */
$(document).ready(function(){
	$('#map_everywhere').click(everywhereOnClick);
	$('#map_region').change(regionsOnChange);
	$('#map_ville').change(villesOnChange);
	
	return false;
});

function everywhereOnClick() {
	if ($(this).attr('checked')==true) {
		$('#map_region').find('option').each(function(){
			if ($(this).attr('id')==0){
				$(this).attr('selected', 'selected');
			}
			else {
				$(this).attr('selected', '');
			}
		});
		
		var reg_id = 0;
		$.ajax({
			url: base_url + "f_map/region/" + reg_id + "/villes",
			type: "GET",
			data: '',
			dataType: "xml",
			error: function(){},
			beforeSend: function(){},
			success: function(xml){
				document.getElementById('map_ville').options.length = 0;
				
				$('#map_ville').append('<option value="0">Votre ville</option>');
				$(xml).find('data').find('ville').each(function() {
					var vil_id = $(this).attr("id");
					var vil_name = $(this).attr("name");
					$('#map_ville').append('<option value="'+vil_id+'">'+vil_name+'</option>');
				});
				
				document.getElementById('map_flash').goRegion(reg_id);
			}
		});
	}
	
	document.getElementById('map_flash').un_selected_ville_all();
	document.getElementById('map_flash').un_selected_region_all();
}

/**
 * Actions lors d'un changement de région
 * -> mise à jour des villes dans le menu de sélection en dessous
 * -> mise à jour de la map Flash
 * 
 * @name	regionsOnChange
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-02-06
 * @version	1.0
 * @return	void
 */
function regionsOnChange() {
	var reg_id = $(this).val();
	
	if (reg_id==0) {
		document.getElementById('map_flash').un_selected_region_all();
	}
	else {
		$('#map_everywhere').attr('checked', '');
	}
	
	$.ajax({
		url: base_url + "f_map/region/" + reg_id + "/villes",
		type: "GET",
		data: '',
		dataType: "xml",
		error: function(){},
		beforeSend: function(){},
		success: function(xml){
			document.getElementById('map_ville').options.length = 0;
			
			if (reg_id==0) {
				$('#map_ville').append('<option value="0">Votre ville</option>');
			}
			else {
				$('#map_ville').append('<option value="0">Toutes les villes</option>');
			}
			
			$(xml).find('data').find('ville').each(function() {
				var vil_id = $(this).attr("id");
				var vil_name = $(this).attr("name");
				$('#map_ville').append('<option value="'+vil_id+'">'+vil_name+'</option>');
			});
			
			document.getElementById('map_flash').goRegion(reg_id);
		}
	});
	
	return false;
}

/**
 * Actions lors d'un changement de ville
 * -> si la region est sur "toutes", on sélectionne la région de la ville active
 * -> mise à jour de la map Flash
 * 
 * @name	villesOnChange
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-02-06
 * @version	1.0
 * @return	void
 */
function villesOnChange() {
	var vil_id = $(this).val();
	
	if (vil_id==0) {
		var reg_id = $('#map_region').val();
		document.getElementById('map_flash').un_selected_ville_all();
		document.getElementById('map_flash').goRegion(reg_id);
	}
	else {
		$('#map_everywhere').attr('checked', '');
		
		$.ajax({
			url: base_url + "f_map/ville/" + vil_id + "/region",
			type: "GET",
			data: '',
			dataType: "xml",
			error: function(){},
			beforeSend: function(){},
			success: function(xml){
				var reg_id = $(xml).find('data').find('region').attr('id');
				var tmp_id = 0;
				$('#map_region').find('option').each(function(){
					tmp_id = $(this).val();
					if (tmp_id==reg_id) {
						$(this).attr('selected', 'selected');
					}
				});
				
				var reg_id = $('#map_region').val();
				
				document.getElementById('map_flash').goVille(reg_id, vil_id);
			}
		});
	
	}
	
	
	return false;
}

/**
 * Actions lors d'une requete à partir de la map Flash
 * -> sélection de la région passée en paramètre
 * -> si la ville est passée aussi, sélection de la ville
 * 
 * @name	updateRegionAndVille
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-02-06
 * @version	1.0
 * @param	int reg_id Identifiant de la région
 * @param	int vil_id Identifiant de la ville
 * @return	void
 */
function updateRegionAndVille(reg_id, vil_id) {
	// UpdateRegion
	var tmp_id = 0;
	$('#map_region').find('option').each(function(){
		tmp_id = $(this).val();
		if (tmp_id==reg_id) {
			$(this).attr('selected', 'selected');
		}
	});
	
	// UpdateVille
	$.ajax({
		url: base_url + "f_map/region/" + reg_id + "/villes",
		type: "GET",
		data: '',
		dataType: "xml",
		error: function(){},
		beforeSend: function(){},
		success: function(xml){
			document.getElementById('map_ville').options.length = 0;
			$('#map_ville').append('<option value="0">Votre ville</option>');
			$(xml).find('data').find('ville').each(function() {
				var tmp_vil_id = $(this).attr("id");
				var tmp_vil_name = $(this).attr("name");
				
				if (vil_id!=null && tmp_vil_id==vil_id) {
					$('#map_ville').append('<option value="'+tmp_vil_id+'" selected="selected">'+tmp_vil_name+'</option>');
				}
				else {
					$('#map_ville').append('<option value="'+tmp_vil_id+'">'+tmp_vil_name+'</option>');
				}
				
			});
		}
	});

	if (reg_id!=0) {
		$('#map_everywhere').attr('checked', '');
	}
	
	return false;
}

/**
 * Action lancée par la map Flash pour rafraichissement du contenu de page en fonction des critères géographiques
 * 
 * @name	vasYBalanceLesResultsMonLapin
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-02-06
 * @version	1.0
 * @param	int reg_id Identifiant de la région
 * @param	int vil_id Identifiant de la ville
 * @return	void
 */
function vasYBalanceLesResultsMonLapin(reg_id, vil_id) {
	// alert(reg_id);
	// alert(reg_id==null);
	// alert(vil_id);
	// alert(vil_id==null);
	
	return false;
}