// Fonction de chargement de la query
// Passe en param la requete spécifique
function siter_query(obj, e) {
	
	// Récupère les données texte
	var input = $('#txt_siter').val();
	var eQuery = '';
	
	// Input libre
	eQuery = '[LIBRE]==' + input;
	
	// Dans les contacts ? 
	var Econtact = ($('#siter_in_contact').attr("checked")) ? 'oui' : 'non';
	var in_contact = '&in_contact=' + Econtact;
	// Gestion des listes de sélection
	eQuery += '-[SELECTS]==';
	if ($('select', obj).length > 0) {
		$('select', obj).each(function(i){
				if (this.value > 0) {
	    			eQuery += + this.value + '_et_';
	    		}
	    });
	    if (eQuery != '') {
	    	eQuery = eQuery.substr(0, (eQuery.length-4));
    	}
    }
    
    // Gestion de l'alphabet
    if ($(".alpha_on").length > 0) {
		eQuery += '-[ALPHABET]==';
		$(".alpha_on").each(function(i){
	    		eQuery += this.innerHTML + '_et_';
	    });
	    eQuery = eQuery.substr(0, (eQuery.length-4));
	}    
	
	 // Le paramétrage de la query est OK
    // On attaque Ajax
    AjaxSqueeze('spip.php?page=liste_structure'+ in_contact +'&public=1&tri=nom_structure&statut=&debut=0&params=' + eQuery,'structures','',e)
    
return false;
}

// Fonction Activation/Desactivation alphabet
function siter_active_key(element) {
	element.className = (element.className == 'alpha_on') ? 'alpha_off' : 'alpha_on';
}

// Affiche les détails rapides de la structure
function quick_details(id_structure) {
	if( $("#c_results_" + id_structure).is(".opened") ) {
		$("#c_results_" + id_structure).slideUp("slow");
		$("#c_results_" + id_structure).toggleClass('opened').toggleClass('closed');
	}
	else {
		$.ajax({
		   type: "POST",
		   url: "spip.php?page=quick_details",
		   data: "id_structure="+id_structure,
		   beforeSend : function() { $("#c_results_" + id_structure).empty(); $("#c_results_" + id_structure).append("wait...");   },
		   success: function(msg){
		   		$("#c_results_" + id_structure).html(msg);
		   		$("#c_results_" + id_structure).slideDown("slow");
		   		$("#c_results_" + id_structure).addClass('opened');
		   }
		 });
		
	}	
}

// Fonction d'attache contact/structure
function attacher(id_structure, id_element, fichier_cible, cible_obj) {
	
	if (id_element > 0) {
		$.ajax({
			   type: "POST",
			   url: "?exec=gestion_" + fichier_cible,
			   data: "act=add" + "&id_structure="+id_structure + "&id_element=" + id_element + "&obj=" + cible_obj,
			   beforeSend : function() { $("#siter_boite_" + cible_obj).empty(); $("#siter_boite_" + cible_obj).append("wait...");   },
			   success: function(msg){
			   		$("#siter_boite_" + cible_obj).html(msg);
			   }
		});
	}
}

function delier(id_structure, id_element, fichier_cible, cible_obj) {
	
	if (id_element > 0) {
		$.ajax({
			   type: "POST",
			    url: "?exec=gestion_" + fichier_cible,
			   data: "act=remove" + "&id_structure="+id_structure + "&id_element=" + id_element + "&obj=" + cible_obj,
			   beforeSend : function() { $("#siter_boite_" + cible_obj).empty(); $("#siter_boite_" + cible_obj).append("wait...");   },
			   success: function(msg){
			   		$("#siter_boite_" + cible_obj).html(msg);
			   }
		});
	}
}

// API Google MAP
function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    geocoder = new GClientGeocoder();
  }
}
var map = null;
var geocoder = null;
    
function showAddress(address) {
 var i = 0;
 alert(adress);
 if (geocoder) {
 	 alert(1);
      geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.setCenter(point, 13);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.openInfoWindowHtml(address);
        }
      }
    );
  }
}

$(document).ready(function(){
  initialize();
});

var my_contact;

function contact_infos(id_auteur) {
	if (id_auteur > 0) {
		if( $("#siter_boite_auteur").is(".opened") && id_auteur == my_contact) {
			$("#siter_boite_auteur").slideUp("slow");
			$("#siter_boite_auteur").toggleClass('opened').toggleClass('closed');
		}
		else {
			$("#siter_boite_auteur").insertAfter("#contact_"+id_auteur);
			$.ajax({
				   type: "POST",
				    url: "?page=contact_infos",
				   data: "&id_auteur=" + id_auteur,
				   beforeSend : function() { $("#siter_boite_auteur").empty(); $("#siter_boite_auteur").append("wait...");   },
				   success: function(msg){
						$("#siter_boite_auteur").html(msg);
						$("#siter_boite_auteur").slideDown("slow");
			   			$("#siter_boite_auteur").addClass('opened');
						my_contact = id_auteur;
				   }
			});
		}
	}
}
