// Trier les catégories
function triCateg(lienordre) {
	$.ajax({
		type: "GET",
		url: "ajax/tri_categ.php",
		data: { ordre: lienordre },
		async: false,
		success: function(echo){
			_error = "off";
			$('ul#cat_list').html(echo);
		}/*,
		error: function(echo){
			_error = "on";
			alert("Error - " + echo);
		}*/
	});	
}

// Charger les produits
function getProduits(field_rubrique) {
	$.ajax({
		type: "GET",
		url: "ajax/get_produits.php",
		data: { rubrique: $(field_rubrique).val() },
		async: false,
		success: function(echo){
			_error = "off";
			$("select[@name=produit]").html(echo);
		}/*,
		error: function(echo){
			_error = "on";
			alert("Error - " + echo);
		}*/
	});	
}

// Charger les communes
function getCommunes(field_province) {
	$.ajax({
		type: "GET",
		url: "ajax/get_communes.php",
		data: { province: $(field_province).val() },
		async: false,
		success: function(echo){
			_error = "off";
			$("select[@name=commune]").html(echo);
		}/*,
		error: function(echo){
			_error = "on";
			alert("Error - " + echo);
		}*/
	});	
}

// Charger le texte de l'annonce
function getTexte(field_id) {
	$.ajax({
		type: "GET",
		url: "ajax/get_texte.php",
		data: { id: $(field_id).val() },
		async: false,
		success: function(echo){
			_error = "off";
			$("input[@name=texte]").val(echo);
		}/*,
		error: function(echo){
			_error = "on";
			alert("Error - " + echo);
		}*/
	});	
}

$(document).ready(function() {
	
	// Sélectionner la rubrique lors d'un clic sur une catégorie
	/*$('ul#cat_list li').click(function() {
		$('select[name="rubrique"] option[value="' + $(this).attr('title') + '"]').attr('selected', 'selected');
		getProduits($('select[name="rubrique"]'));
		($("select[@name=produit]")[0]).selectedIndex = 0;
	});*/
	
	// Scroller les catégories vers le bas
	$("#scroll_bottom").click(function() {
		var divOffset = $("#cat_list").offset().top;
		var pOffset = $("#cat_list li:eq(14)").offset().top;
		var pScroll = pOffset - divOffset;
		$("#cat_list_content").animate({scrollTop: "+=" + pScroll + "px"}, 1000, "easeinout");
	});
	
	// Scroller les catégories vers le haut
	$("#scroll_top").click(function() {
		var divOffset = $("#cat_list").offset().top;
		var pOffset = $("#cat_list li:eq(14)").offset().top;
		var pScroll = pOffset - divOffset;
		$("#cat_list_content").animate({scrollTop: "-=" + pScroll + "px"}, 1000, "easeinout");
	});
	
	// Afficher les produits
	$("select[@name=rubrique]").change(function(){
		getProduits(this);
		($("select[@name=produit]")[0]).selectedIndex = 0;		
	});
	
	// Afficher les communes
	$("select[@name=province]").change(function(){
		getCommunes(this);	
		($("select[@name=commune]")[0]).selectedIndex = 0;		
	});
	
	// Envoyer le formulaire de recherche par rubrique
	$("#recherche_button").click(function(){
		$("input[@name=rubrique_txt]").val($("select[@name=rubrique] option:selected").text());
		$("input[@name=produit_txt]").val($("select[@name=produit] option:selected").text());
		$("input[@name=province_txt]").val($("select[@name=province] option:selected").text());
		$("input[@name=commune_txt]").val($("select[@name=commune] option:selected").text());
		$("input[@name=neuve_txt]").val($("input[@name=neuve]:checked").parent().text());
		$("form[@name=form_recherche]").submit();	
	});
	
	// Afficher les méthodes de tri d'affichage des catégories
	$("#categories ul#cat_opt li#cat_afficher a:eq(0)").click(function(){
		$("#categories ul#cat_opt li#cat_afficher ul").toggle();
	});
	
	// Changer le tri d'affichage des catégories
	$("#categories ul#cat_opt li#cat_afficher ul li").click(function(){
		
		$("#categories ul#cat_opt li#cat_afficher ul li#cat_afficher_selected")
			
			// On ajoute le lien
			.html( '<a href="#" onclick="triCateg(\'' + $('#categories ul#cat_opt li#cat_afficher ul li#cat_afficher_selected').attr('title') + '\'); return false;">' + $('#categories ul#cat_opt li#cat_afficher ul li#cat_afficher_selected').html() + '</a>' )
			
			// On retire l'id
			.removeAttr("id");
		
		$(this)
			
			// on rejoute l'id
			.attr("id", "cat_afficher_selected")
			
			// on change le contenu de la balise li
			.html($(this)
				// par le contenu de la balise enfant a
				.children("a").html()
			);
			
			
		// Sélectionner la rubrique lors d'un clic sur une catégorie
		$('ul#cat_list li').click(function() {
			$('select[name="rubrique"] option[value="' + $(this).attr('title') + '"]').attr('selected', 'selected');
			getProduits($('select[name="rubrique"]'));
			($("select[@name=produit]")[0]).selectedIndex = 0;
		});
		
	});
	
	// Switcher l'affichage dans Rechercher une bonne affaire et Rechercher un client
	$("ul#liste_h2 li").click(function() {
		
		$("li#liste_h2-select")
			// On ajoute le lien
			.html("<h2><a>" + $("li#liste_h2-select h2").html() + "</a></h2>")
			// On retire l'id
			.removeAttr('id');
					
		$(this)
			// on rejoute l'id
			.attr("id","liste_h2-select")
			// on change le contenu de la balise li
			.html("<h2>" + $(this)
				// par le contenu de la balise enfant a
				.children("h2").children("a").html()
				+ "</h2>"
			);
			
		if ($(this).attr('title') == "clients") {
			$("#tdprix").hide();
			$("#tdtype").hide();
			$("#p_recherche").hide();
			$('input[name="sort"]').val("client");
		} else {
			$("#tdprix").show();
			$("#tdtype").show();
			$("#p_recherche").show();
			$('input[name="sort"]').val("annonce");
		}
		
	});
	
	// Envoyer le formulaire d'accès direct
	$("#recherche_accesdirect").click(function() {
		if ($('input[@name="petiteannid"]').val() != "") {
			getTexte($('input[name="petiteannid"]'));
			$("form[@name=form_accesdirect]").submit();	
		}
	});
	
});
