function updateListePersonnes(){
	var sexe = $("#filtrePersonnes select[name=filtreSexe] option:selected").val();
	var poids = $("#filtrePersonnes select[name=filtrePoids] option:selected").val();
	var idCategorie = $("#filtrePersonnes select[name=filtreCategorie] option:selected").val();
	dataString = "sexe=" + sexe + "&poids=" + poids + "&categorie=" + idCategorie;
	var html = $.ajax({
		type: "POST",
		url: "liste-personnes.php",
		data: dataString,
		async: false
	}).responseText;
    $("#listePersonnes").replaceWith(html);
	$("#listePersonnes").tablesorter({
            sortList: [[1, 0]],
            headers: {
                0: {
                    sorter: false
                },
                6: {
                    sorter: false
                }
            }
        });
}

function hideWeight() {
	var categorie = $("select[name=idCategorie] option:selected").val();
	if(categorie == '') {
		categorie = $("select[name=idCategorie] option[selected=selected]").val();
	}
	if((categorie < 957 || categorie > 1150) && $("#weightBox").css('visibility') != "hidden") {
		$("#weightBox").hide();
	} else {
		$("#weightBox").show();
	}
}

function changePoids(){
	sexe = $("input[name=sexe]:checked").val();
	poids = $("select[name=poids] option:selected").val();
    html = $.ajax({
        type: "POST",
        url: "../ajaxRegistration.php",
        data: "action=choixPoids&sexe=" + sexe + "&poids=" + poids,
        async: false
    }).responseText;
    $("select[name=poids]").empty();
    $("select[name=poids]").prepend(html);
}

function changeCategorie(){
	sexe = $("input[name=sexe]:checked").val();
	poids = $("select[name=poids] option:selected").val();
	dateNaissance = $("input[name=dateNaissance]").val();
	categorie = $("select[name=idCategorie]").val();
    html = $.ajax({
        type: "POST",
        url: "../ajaxRegistration.php",
        data: "sexe=" + sexe + "&poids=" + poids + "&dateNaissance=" + dateNaissance + "&categorie=" + categorie + '&action=choixCategorie',
        async: false
    }).responseText;
    $("#choixCategorie").replaceWith(html);
};

// Inscriptions (modification des menus, affichage / masquage, ...)
function updateClub(pays, departement, idClub){
    var html = $.ajax({
        type: "POST",
        url: "../ajaxRegistration.php",
        data: "action=choixClub&pays=" + pays + "&dpt=" + departement + "&idClub=" + idClub,
        async: false
    }).responseText;
    $("#choixClub").replaceWith(html);
    if ($("select[name=idClub]").attr("class") == "vide") {
        $("#choixClub").hide();
    }
    else {
        $("#choixClub").show("blind");
    }
}

function choixPaysClub(departement,idClub){
	$("select[name=idClub] option:selected").removeAttr('selected');
	$("select[name=departementClub] option:selected").removeAttr('selected');
	$("#nouveauClub").hide();
    var pays = $("select[name=paysClub] option:selected").val();
	$("select[name=paysClub] option.selected").removeAttr("class");
    if (pays == "FRA") {
		updateClub(pays,'','');
        $("#choixDepartementClub").show("blind");
        $("#choixClub").hide();
    }
    else {
		$("#choixDepartementClub").hide();
		if(pays != '') {
			updateClub(pays,'','');
		} else {
			$("#choixClub").hide();
		}
    }
    $("select[name=paysClub] option:selected").attr("class", "selected");
	if($("select[name=idClub] option:selected").val() == 'NC') {
		$("#nouveauClub").show("blind");
	} else {
		$("#nouveauClub").hide();
	}
}

function choixClub(){
    var club = $("#choixClub select option:selected").val();
    $("#choixClub select").removeAttr("class");
    $("#choixClub select option.selected").removeAttr("class");
	$("#nouveauClub").hide();
	if(club != '' && club !='NC') {
		$("#choixClub select option[value="+club+"]").attr("class", "selected");
	} else if(club == 'NC') {
		$("#nouveauClub").show('blind');
	}
}

function choixDptClub(departement,idClub){
	if (departement == "") {
		var departement = $("select[name=departementClub] option:selected").val();
	}
	if(idClub == "") {
		var idClub = $("select[name=idClub] option:selected").val();
	}
    $("select[name=departementClub] option.selected").removeAttr("class");
    updateClub('FRA', departement, idClub);
    if (departement == "") {
        $("#choixClub").hide();
    }
	$("select[name=departementClub] option[value="+departement+"]").attr("class", "selected");
}

$(function($){
	$.ajaxSetup({ 
	    beforeSend: function() { 
	        $("#ajax-loader").show(); 
	    }, 
	    complete: function() { 
	        $("#ajax-loader").hide(); 
	    } 
	});
    $('.ui-state-default').hover(
			function(){ $(this).addClass('ui-state-hover'); }, 
			function(){ $(this).removeClass('ui-state-hover'); }
	);
	$('button.ui-state-highlight').hover(
		function(){ $(this).removeClass('ui-state-highlight'); $(this).addClass('ui-state-default'); }, 
		function(){ $(this).removeClass('ui-state-default'); $(this).addClass('ui-state-highlight'); }
	);
	$('a.ui-state-highlight').hover(
		function(){ $(this).removeClass('ui-state-highlight'); $(this).addClass('ui-state-default'); }, 
		function(){ $(this).removeClass('ui-state-default'); $(this).addClass('ui-state-highlight'); }
	);
	$('button.ui-state-error').hover(
		function(){ $(this).removeClass('ui-state-error'); $(this).addClass('ui-state-hover'); }, 
		function(){ $(this).removeClass('ui-state-hover'); $(this).addClass('ui-state-error'); }
	);
	$('.tipsy-e').tipsy({gravity: 'e'});
	$('.tipsy-w').tipsy({gravity: 'w'});
	$('.tipsy-n').tipsy({gravity: 'n'});
	$('.tipsy-s').tipsy({gravity: 's'});
    // Boite de connexion
    $("#loginBox").hide();
    $("a[id=loginButton]").click(function(){
        $("#loginBox").toggle('slide', {
            direction: "up"
        }, 'normal');
        return false;
    });
    $("#loginBox #login_cancel").click(function(){
        $("#loginBox").toggle('slide', {
            direction: "up"
        }, 'normal');
        return false;
    });
    // Selecteur de langue
    $("#lang .off").mouseover(function(){
        s1 = $(this).children('img').attr("src").replace('_off', '');
        $(this).children('img').attr("src", s1);
        s2 = $("#lang .on img").attr("src").replace('.png', '_off.png');
        $("#lang .on img").attr("src", s2);
    });
    $("#lang .off").mouseout(function(){
        s1 = $(this).children('img').attr("src").replace('.png', '_off.png');
        $(this).children('img').attr("src", s1);
        s2 = $("#lang .on img").attr("src").replace('_off', '');
        $("#lang .on img").attr("src", s2);
    });
	
    $("img.rollover").hover(function(){
        this.src = this.src.replace("_off", "_on")
    }, function(){
        this.src = this.src.replace("_on", "_off");
    });
    $("#container form input").focus(function(){
        this.select();
    });
    $("#container form textarea").focus(function(){
        this.select();
    });
});