$(document).ready(function() {
    annonceVehiculeReady();
});

function annonceVehiculeReady() {
    $("#annonceVehicule_orderby_champ").change(function() {
        $('#annonceVehicule_limit_page_en_cours').val('1');
        
        $('#form_annonceVehicule_recherche').submit();
    });

    $("#annonceVehicule_limit_nb_par_page").change(function() {
        $('#annonceVehicule_limit_page_en_cours').val('1');

        $('#form_annonceVehicule_recherche').submit();
    });

    $("td.colonne_photo").hover(function() {
        //$(this).find('div.liste-annonce-vendu-container').css('display', 'none');
        /*$(this).find('div.liste-annonce-vendu-container').stop()
        .animate({
            marginTop: '-222px'
        }, 300, 'linear');*/
        $(this).find('img.liste-annonce-photo').stop()
        .animate({
            width: '317px',
            height: '222px'
        }, 300, 'linear');
    } , function() {
        $(this).find('img.liste-annonce-photo').stop()
        .animate({
            width: '147px',
            height: '103px'
        }, 200, 'linear');
        /*$(this).find('div.liste-annonce-vendu-container').stop()
        .animate({
            marginTop: '-103px'
        }, 200, 'linear');*/
        //$(this).find('div.liste-annonce-vendu-container').css('display', 'block');
    });
}

function supprimerCritere(key) {
    $('#annonceVehicule_filter_' + key).val('');

    $('#form_annonceVehicule_recherche').attr('action', listeVehiculeURL);

    $('#form_annonceVehicule_recherche').submit();
}

function afficherPage(num_page) {
    $('#annonceVehicule_limit_page_en_cours').val(num_page);

    $('#form_annonceVehicule_recherche').submit();
}

function modifierCriteres(urlRechercheAvancee) {
    document.location.href = urlRechercheAvancee;
}

function enregistrerCriteres() {
    $.post(baseUrl + '/ajax/frontend-membre/enregistrer-recherche',
        $('#form_annonceVehicule_recherche').serialize(),
        function(jsonData) {
            if (jsonData.resultat > 0) {
                msg = 'Votre recherche a été enregistrée dans votre espace membre.';
            }
            else {
                msg = 'Votre recherche n\'a pu être enregistrée suite à une erreur applicative.';
            }         

            $('#modalDialog').dialog('destroy');
            init_modal_dialog_noheight();
            $('#modalDialog').dialog('option', 'buttons', {
                "Ok": function() {
                    $(this).dialog("close");
                }
            });
            $('#modalDialog').html(msg);
            $('#modalDialog').dialog('open');
        },
        'json'
        );
}

function alerteEmailCriteres() {
    $.post(baseUrl + '/ajax/frontend-membre/enregistrer-alerte-email',
        $('#form_annonceVehicule_recherche').serialize(),
        function(jsonData) {
            if (jsonData.resultat > 0) {
                msg = 'Votre alerte e-mail a été enregistrée dans votre espace membre.';
            }
            else {
                msg = 'Votre alerte e-mail n\'a pu être enregistrée suite à une erreur applicative.';
            }

            $('#modalDialog').dialog('destroy');
            init_modal_dialog_noheight();
            $('#modalDialog').dialog('option', 'buttons', {
                "Ok": function() {
                    $(this).dialog("close");
                }
            });
            $('#modalDialog').html(msg);
            $('#modalDialog').dialog('open');
        },
        'json'
        );
}

function ajouterAnnonceFavoris(pk_id) {
    $.post(baseUrl + '/ajax/frontend-membre/enregistrer-annonce-favoris',
    {
        fk_id_annonce_vehicule: pk_id
    },
    function(jsonData) {
        if ((jsonData.resultat['fk_id_membre'] > 0) && (jsonData.resultat['fk_id_annonce_vehicule'] > 0)) {
            msg = 'Votre annonce a été enregistrée dans votre espace membre.';
        }
        else {
            msg = 'Votre annonce n\'a pu être enregistrée suite à une erreur applicative.';
        }

        $('#modalDialog').dialog('destroy');
        init_modal_dialog_noheight();
        $('#modalDialog').dialog('option', 'buttons', {
            "Ok": function() {
                $(this).dialog("close");
            }
        });
        $('#modalDialog').html(msg);
        $('#modalDialog').dialog('open');
    },
    'json'
    );
}

function ajouterAnnonceComparateur(pk_id, url_comparateur) {
    $.post(baseUrl + '/ajax/frontend-annonce-vehicule/ajouter-annonce-comparateur',
    {
        fk_id_annonce_vehicule: pk_id
    },
    function(jsonData) {
        if (jsonData.resultat == '1') {
            msg = 'Votre annonce a été ajoutée au comparateur.';

            $('#modalDialog').dialog('destroy');
            init_modal_dialog_noheight();
            $('#modalDialog').dialog('option', 'buttons', {
                "Afficher le comparateur": function() {
                    $(this).dialog("close");
                    document.location = url_comparateur;
                },
                "Sélectionner un autre véhicule": function() {
                    $(this).dialog("close");
                }
            });
            $('#modalDialog').html(msg);
            $('#modalDialog').dialog('open');
        }
        else {
            msg = 'Votre annonce n\'a pu être ajoutée au comparateur<br />(4 véhicules maximum).';

            $('#modalDialog').dialog('destroy');
            init_modal_dialog_noheight();
            $('#modalDialog').dialog('option', 'buttons', {
                "Afficher le comparateur": function() {
                    $(this).dialog("close");
                    document.location = url_comparateur;
                },
                "Fermer": function() {
                    $(this).dialog("close");
                }
            });
            $('#modalDialog').html(msg);
            $('#modalDialog').dialog('open');
        }


    },
    'json'
    );
}

