$(document).ready(function() {
    indexReady();
});

function indexReady() {
    $("#annonceVehicule_filter_fk_id_type_vehicule").change(function() {
        $("#annonceVehicule_filter_fk_id_modele_vehicule_1").html('');
        $("#annonceVehicule_filter_fk_id_modele_vehicule_1").append('<option value="">- Tous -</option>');

        if ($("#annonceVehicule_filter_fk_id_marque_vehicule_1").val() != '') {
            $.ajax({
                type: 'POST',
                url: baseUrl + '/ajax/divers/liste-modele-type-vehicule',
                data: ({
                    fk_id_marque_vehicule: $("#annonceVehicule_filter_fk_id_marque_vehicule_1").val(),
                    fk_id_type_vehicule: $("#annonceVehicule_filter_fk_id_type_vehicule").val()
                }),
                dataType: 'json',
                async: false,
                success: function(jsonData){
                    for (index in jsonData.resultat) {
                        $("#annonceVehicule_filter_fk_id_modele_vehicule_1").append('<option value="' + jsonData.resultat[index]['pk_id'] + '">' + jsonData.resultat[index]['libelle'] + '</option>');
                    }
                }
            });
        }

        nombreResultatRechercheSimple();
    });

    $("#annonceVehicule_filter_fk_id_marque_vehicule_1").change(function() {
        $("#annonceVehicule_filter_fk_id_modele_vehicule_1").html('');
        $("#annonceVehicule_filter_fk_id_modele_vehicule_1").append('<option value="">- Tous -</option>');

        if ($("#annonceVehicule_filter_fk_id_marque_vehicule_1").val() != '') {
            $.ajax({
                type: 'POST',
                url: baseUrl + '/ajax/divers/liste-modele-type-vehicule',
                data: ({
                    fk_id_marque_vehicule: $("#annonceVehicule_filter_fk_id_marque_vehicule_1").val(),
                    fk_id_type_vehicule: $("#annonceVehicule_filter_fk_id_type_vehicule").val()
                }),
                dataType: 'json',
                async: false,
                success: function(jsonData){
                    for (index in jsonData.resultat) {
                        $("#annonceVehicule_filter_fk_id_modele_vehicule_1").append('<option value="' + jsonData.resultat[index]['pk_id'] + '">' + jsonData.resultat[index]['libelle'] + '</option>');
                    }
                }
            });
        }

        nombreResultatRechercheSimple();
    });

    $("#annonceVehicule_filter_fk_id_modele_vehicule_1").change(function() {
        nombreResultatRechercheSimple();
    });

    $("#annonceVehicule_filter_prix_ttc_mini").change(function() {
        nombreResultatRechercheSimple();
    });

    $("#annonceVehicule_filter_prix_ttc_maxi").change(function() {
        nombreResultatRechercheSimple();
    });

    $("#annonceVehicule_filter_annee_premiere_mes_debut").change(function() {
        nombreResultatRechercheSimple();
    });

    $("#annonceVehicule_filter_annee_premiere_mes_fin").change(function() {
        nombreResultatRechercheSimple();
    });

    $("#annonceVehicule_filter_kilometrage_maxi").change(function() {
        nombreResultatRechercheSimple();
    });

    $("#annonceVehicule_filter_fk_id_type_carburant").change(function() {
        nombreResultatRechercheSimple();
    });

    $("#annonceVehicule_filter_code_departement").change(function() {
        nombreResultatRechercheSimple();
    });

    /*
    $("#annonceVehicule_filter_code_postal").change(function() {
        nombreResultatRechercheSimple();
    });

    $("#annonceVehicule_filter_rayon").change(function() {
        nombreResultatRechercheSimple();
    });

    $('#annonceVehicule_filter_code_postal').autocomplete(baseUrl + '/ajax/divers/liste-code-postal-autocomplete', {
        minChars: 2,
        max: 0
    });

    $('#annonceVehicule_filter_code_postal').result(function(event, data, formatted) {
        if (!data) {
            $('#annonceVehicule_filter_code_postal').val('');
        }
        else {
            var commune_code_postal = data;

            $('#annonceVehicule_filter_code_postal').val(commune_code_postal);
        }
    }).blur(function(){
        $(this).search();
    });
    */

   $("#annonceVehicule_filter_fk_id_modele_vehicule_1").change(function() {
        nombreResultatRechercheSimple();
    });

    nombreResultatRechercheSimple();

    $('#slider-g').nivoSlider({
        effect: 'fade', // Specify sets like: 'random,fold,fade,sliceDown'
        directionNav: false, // Next & Prev navigation
        directionNavHide: false, // Only show on hover
        controlNav: false, // 1,2,3... navigation
        controlNavThumbs: false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel: false, // Use image rel for thumbs
        keyboardNav: true, // Use left & right arrows
        pauseOnHover: true, // Stop animation while hovering
        manualAdvance: false, // Force manual transitions
        captionOpacity: 0.8 // Universal caption opacity
    });

    $('#slider-d').nivoSlider({
        effect: 'fade', // Specify sets like: 'random,fold,fade,sliceDown'
        directionNav: false, // Next & Prev navigation
        directionNavHide: false, // Only show on hover
        controlNav: false, // 1,2,3... navigation
        controlNavThumbs: false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel: false, // Use image rel for thumbs
        keyboardNav: true, // Use left & right arrows
        pauseOnHover: true, // Stop animation while hovering
        manualAdvance: false, // Force manual transitions
        captionOpacity: 0.8 // Universal caption opacity
    });
}

function changeTypeAnnonce() {
   var msg;

   if ($('#annonceVehicule_filter_fk_id_type_annonce').val() == '3') {
       msg = 'Attention, ces véhicules sont uniquement destinés aux professionnels disposant d’un numéro RCS';

       $('#modalDialog').dialog('destroy');
        init_modal_dialog_noheight();
        $('#modalDialog').dialog('option', 'buttons', {
            "Ok": function() {
                $(this).dialog("close");
            }
        });
        $('#modalDialog').html(msg);
        $('#modalDialog').dialog('open');
   }

   nombreResultatRechercheSimple();
}

function nombreResultatRechercheSimple() {
    $('#accueil-recherche-vehicules-nb-resultats').html('<img src="' + baseUrl + '/images/frontend/ajax-loader-nb-resultats.gif" width="16" height="16" border="0" />');

    $.post(baseUrl + '/ajax/frontend-annonce-vehicule/nombre-resultat-recherche',
        $('#form_annonceVehicule_recherche').serialize(),
        function(jsonData) {
            $('#accueil-recherche-vehicules-nb-resultats').html(jsonData.nb_total_resultat + ' annonce' + ((jsonData.nb_total_resultat > 1)?'s':''));
        },
        'json'
        );
}

