jQuery.noConflict();

//Option sum functions
function sumOptionLine(optionId,lineId,number,price) {
    jQuery('#total_line_' + optionId + '_'+lineId).html(number * price);

    var total=0;
    
    jQuery(" span[ id ^= 'total_line_" + optionId+ "' ]").each(function(i){
        total+=parseFloat(jQuery( this ).html());
    });

    jQuery('#option_total_'+optionId).html(total);
    updateGeneralTotal();

}

function updateGeneralTotal() {
    var price=0;
    price += jQuery.catPrice;

    jQuery(" span[ id ^= 'option_total_' ]").each(function(i){
        //Get the if of the category clicked
        price+=parseFloat(jQuery( this ).html());
    });

    jQuery('#general_total').html(price);

}

function togglePaiement() {
    jQuery('#virement').toggle();
    jQuery('#cheque').toggle();
}

//jQuery instead of $ to avoid conflicts with scriptaculous
jQuery(document).ready(function($){

    $.catPrice=0;
    //Display the appropiate category gender div
    function displayCategoryGender(id) {

        $(" div[ id ^= 'gender_' ]").each(function(i){
            //Get the if of the category clicked
            var categoryIdArray=$( this ).attr('id').split('_');
            var catId = categoryIdArray[categoryIdArray.length-1];
            if(catId == id)
                $( this ).fadeIn('slow');
            else
                $( this ).hide();
        });

    }

    //Display appropriate number of lines for the team
    function updateRidersNb(nb) {

        //Number of lines ine the rider table
        var count = $('#team_list > tbody').children().length;

        //We have to remove some lines
        $("#team_list > tbody").find("tr:gt(" + (nb-1) + ")").each(function(i) {
            $(this).hide();
        });

        //We hae to show :)
        $("#team_list > tbody").find("tr:lt(" + (nb) + ")").show();
    
        $('#team_list').children('tbody').each(function(i){
            //alert($( this ).html());
            });
    }

    //Check the cateory choice
    $(" input[ id ^= 'registration_category_id' ]").each(function(i){
        $( this ).click(function () {
            //Get the if of the category clicked
            var categoryIdArray=$( this ).attr('id').split('_');
            var catId = categoryIdArray[categoryIdArray.length-1];

            $.catPrice=parseFloat($('#price_cat_'+catId).html());

            var nb = parseInt($('#nb_participant_'+catId).html());

            //update the total price
            updateGeneralTotal();

            //update the team list number
            updateRidersNb(nb);
            
            //display the good gender choice
            displayCategoryGender(catId);
        });
    });

    $(" input[ id ^= 'registration_category_id_' ]:checked").each(function(i){
        //Get the id of the category checked
        var catId = $( this ).attr('value');
        $.catPrice=parseFloat($('#price_cat_'+catId).html());
    });

    //Mise à jour du total à chaque update de la page
    updateGeneralTotal();

    //Sponsors
    $("#show_sponsors").click(function () {
        $("div.left").toggle();
        $("div.right").toggle();
        $('#sponsors').toggle();
    });

});
