$(function() {
    $('a.lightBox').lightBox({fixedNavigation:true});

    /* Gestion des onglets */
    $(".tabnav li").live("click", function() {
        var id = $(this).attr("id");
        var id2 = $(this).siblings(".active").attr("id");
        if(id== "videoDuJourLink" || id == "contactLiens" || id == "promoBMX" || id == "membre" || id == "modifierProfil" || id == "videoDuJour" || id == "contactProposer" || id == "messagerie" || id == "nouveauMessage") {
            return true;
        }
        $(this).attr("class", "active");
        $(this).siblings(".active").attr("class", "autre");
        $("#"+id2+"Bloc").css("display", "none");
        $("#"+id+"Bloc").fadeIn("slow");
        
        return false;
    }); 

    /* Affichage de la fleche */
    $(window).scroll(function() {
        if($(window).scrollTop() == 0){
            $('#menuBarre img').fadeOut("fast");
        } else {
            $('#menuBarre img').fadeIn("slow");
        }
    });

    /* Gestion du scroll */
    $('#menuBarre img').live('click', function(event){
        event.preventDefault();
        $('html,body').animate({scrollTop: 0}, 'slow');
    });

    $("#boutonEnregistrer img").live("hover", function () {
        if($(this).attr("src") == "/images/graph/senregistrer2.png") {
            $(this).attr("src", "/images/graph/senregistrer.png");
        }
        else {
            $(this).attr("src", "/images/graph/senregistrer2.png");
        }
    });

    $('.barreArticle img').live("hover", function () {
        var maClasse = $(this).attr("class");
        switch(maClasse) {
            case "voirComs" :
                if($(this).attr("src") == "/images/graph/lesComs2.png") {
                    $(this).attr("src", "/images/graph/lesComs.png");
                }
                else {
                    $(this).attr("src", "/images/graph/lesComs2.png");
                }
                break;

            case "voirInfos" :
                if($(this).attr("src") == "/images/graph/plusInfos2.png") {
                    $(this).attr("src", "/images/graph/plusInfos.png");
                }
                else {
                    $(this).attr("src", "/images/graph/plusInfos2.png");
                }
                break;

            case "voirFacebook" :
                if($(this).attr("src") == "/images/graph/facebook2.png") {
                    $(this).attr("src", "/images/graph/facebook.png");
                }
                else {
                    $(this).attr("src", "/images/graph/facebook2.png");
                }
                break;

            case "voirTwitter" :
                if($(this).attr("src") == "/images/graph/twitter2.png") {
                    $(this).attr("src", "/images/graph/twitter.png");
                }
                else {
                    $(this).attr("src", "/images/graph/twitter2.png");
                }
                break;
        }
    });

    $('#connexionBloc input, #rechercheBloc input, .postCommentaireAll input').blur(function() {
        if($(this).val() == "") {
            switch($(this).attr("id")) {
                case "connexionPass" :
                    $(this).val("()()()()");
                    break;
                case "connexionPseudo" :
                    $(this).val("Pseudo");
                    break;
                case "s" :
                    $(this).val("Rechercher un article");
                    break;
                case "demandePass" :
                    $(this).val("Entrez votre adresse mail");
                    break;
                case "inscriptionPseudo" :
                    $(this).val("Votre pseudo");
                    break;
                case "inscriptionPass" :
                    $(this).val("zdefzd");
                    break;
                case "inscriptionMail" :
                    $(this).val("Votre mail");
                    break;
                case "inscriptionNom" :
                    $(this).val("Votre nom");
                    break;
                case "inscriptionPrenom" :
                    $(this).val("Votre prénom");
                    break;
                case "inscriptionVille" :
                    $(this).val("Votre ville");
                    break;
                case "comPseudo" :
                    $(this).val("Votre pseudo");
                    break;
                case "contactMail" :
                    $(this).val("Votre mail");
                    break;
                case "newSujetTitre" :
                    $(this).val("Titre du sujet");
                    break;
                case "newMessTitre" :
                    $(this).val("Sujet du message");
                    break;
                case "newAnnNom" :
                    $(this).val("Nom de la pièce (ne pas remplir si inexistant)");
                    break;
                case "newAnnPrix" :
                    $(this).val("Prix souhaité");
                    break;
            }
        }
    });

    $('#connexionBloc input, #rechercheBloc input, .postCommentaireAll input').focus(function () {
        switch($(this).attr("id")) {
            case "connexionPass" :
                if($(this).val() == "()()()()") {
                    $(this).val("");
                }
                break;
            case "connexionPseudo" :
            if($(this).val() == "Pseudo") {
                $(this).val("");
            }
                break;
            case "s" :
            if($(this).val() == "Rechercher un article") {
                $(this).val("");
            }
                break;
            case "demandePass" :
            if($(this).val() == "Entrez votre adresse mail") {
                $(this).val("");
            }
                break;
            case "inscriptionPseudo" :
            if($(this).val() == "Votre pseudo") {
                $(this).val("");
            }
                break;
            case "inscriptionPass" :
            if($(this).val() == "zdefzd") {
                $(this).val("");
                
            }
                break;
            case "inscriptionMail" :
            if($(this).val() == "Votre mail") {
                $(this).val("");
            }
                break;
            case "inscriptionNom" :
            if($(this).val() == "Votre nom") {
                $(this).val("");
            }
                break;
            case "inscriptionPrenom" :
            if($(this).val() == "Votre prénom") {
                $(this).val("");
            }
                break;
            case "inscriptionVille" :
            if($(this).val() == "Votre ville") {
                $(this).val("");
            }
                break;
            case "comPseudo" :
            if($(this).val() == "Votre pseudo") {
                $(this).val("");
            }
                break;
            case "contactMail" :
            if($(this).val() == "Votre mail") {
                $(this).val("");
            }
                break;
            case "newSujetTitre" :
            if($(this).val() == "Titre du sujet") {
                $(this).val("");
            }
                break;
            case "newMessTitre" :
            if($(this).val() == "Sujet du message") {
                $(this).val("");
            }   
                break;
            case "newAnnPrix" :
            if($(this).val() == "Prix souhaité") {
                $(this).val("");
            }
                break;
            case "newAnnNom" :
            if($(this).val() == "Nom de la pièce (ne pas remplir si inexistant)") {
                $(this).val("");
            }
                break;
        }
    });

    //
    // Affichage des articles - bouton afficher plus d article
    //
    $('#articles').live("click",function() {
        $("#navigation").attr("alt", "article");
        var typeArticle = $(this).attr("alt");
        $("#allArticles").html("<div id='moreArticleID'><center><h1>Chargement...</h1></center></div>");

        $.ajax({
            type: "POST",
            url: "/Controller/AjaxArticle.php",
            data: "lastID="+ 9999999999999+"&type="+typeArticle+"&typeAffichage=article",
            cache: false,
            success: function(html){
                $("#moreArticleID").remove(); // removing old more button
                $("#allArticles").append(html);
                $(".article").slideDown("slow");
                $(".fastArticle").slideDown("slow");
                FB.XFBML.parse();
            }
        });
        document.title = "Simplified BMX";

        return false;
    });

    $('.moreArticle').live("click",function() {
        var ID = $(this).attr("id");
        var typeAffichage = $(this).attr("typeAffichage");
        var typeArticle = $(this).attr("alt");
        if(ID) {
            $("#"+ID).html("Chargement...");

            $.ajax({
                type: "POST",
                url: "/Controller/AjaxArticle.php",
                data: "lastID="+ ID+"&type="+typeArticle+"&typeAffichage="+typeAffichage,
                cache: false,
                success: function(html){
                    $("#moreArticleID").remove(); // removing old more button
                    $("#allArticles").append(html);
                    $(".article").slideDown("slow");
                    $(".fastArticle").slideDown("slow");
                    FB.XFBML.parse();
                }
            });
        }
        else {
            $("#"+ID).html('Tout est là');// no results
        }

        return false;
    });

    //
    // Affichage du top
    //
    $('#leTop').live("click",function() {
        $("#navigation").attr("alt", "top");
        $("#allArticles").html("<div id='moreTopID'><center><h1>Chargement...</h1></center></div>");
        var typeArticle = $("#articles").attr("alt");
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxTop.php",
            data: "current="+999999999999+"&type="+typeArticle+"&typeAffichage=article",
            cache: false,
            success: function(html){
                $("#moreTopID").remove(); // removing old more button
                $("#allArticles").append(html);
                $(".article").slideDown("slow");
                $(".fastArticle").slideDown("slow");
                FB.XFBML.parse();
            }
        });
        document.title = "Simplified BMX";
        
        return false;
    });

    $('.moreArticleTop').live("click",function() {
        var CURRENT = $(this).attr("id");
        var typeAffichage = $(this).attr("typeAffichage");
        var typeArticle = $("#articles").attr("alt");
        if(CURRENT) {
            $("#moreTopID").html("<center><h1>Chargement...</h1></center>");

            $.ajax({
                type: "POST",
                url: "/Controller/AjaxTop.php",
                data: "current="+CURRENT+"&type="+typeArticle+"&typeAffichage="+typeAffichage,
                cache: false,
                success: function(html){
                    $("#moreTopID").remove(); // removing old more button
                    $("#allArticles").append(html);
                    $(".article").slideDown("slow");
                    $(".fastArticle").slideDown("slow");
                    FB.XFBML.parse();
                }
            });
        }

        return false;
    });

    //type de navigation
    // Par Article
    $("#afficheArticle").live("click", function() {
        var typeArticle = $("#articles").attr("alt");
        if($("#navigation").attr("alt") == "article") {
            $("#allArticles").html("<div id='moreArticleID'><center><h1>Chargement...</h1></center></div>");

            $.ajax({
                type: "POST",
                url: "/Controller/AjaxArticle.php",
                data: "lastID="+ 9999999999999+"&type="+typeArticle+"&typeAffichage=article",
                cache: false,
                success: function(html){
                    $("#moreArticleID").remove(); // removing old more button
                    $("#allArticles").append(html);
                    $(".article").slideDown("slow");
                    $(".fastArticle").slideDown("slow");
                    FB.XFBML.parse();
                }
            });
        }
        else {
            $("#allArticles").html("<div id='moreTopID'><center><h1>Chargement...</h1></center></div>");

            $.ajax({
                type: "POST",
                url: "/Controller/AjaxTop.php",
                data: "current="+999999999999+"&type="+typeArticle+"&typeAffichage=article",
                cache: false,
                success: function(html){
                    $("#moreTopID").remove(); // removing old more button
                    $("#allArticles").append(html);
                    $(".article").slideDown("slow");
                    $(".fastArticle").slideDown("slow");
                    FB.XFBML.parse();
                }
            });
        }
        document.title = "Simplified BMX";

        return false;
    });

    // Par liste
    $("#afficheListe").live("click", function() {
        var typeArticle = $("#articles").attr("alt");
        if($("#navigation").attr("alt") == "article") {
            $("#allArticles").html("<div id='moreArticleID'><center><h1>Chargement...</h1></center></div>");

            $.ajax({
                type: "POST",
                url: "/Controller/AjaxArticle.php",
                data: "lastID="+ 9999999999999+"&type="+typeArticle+"&typeAffichage=liste",
                cache: false,
                success: function(html){
                    $("#moreArticleID").remove(); // removing old more button
                    $("#allArticles").append(html);
                    $(".article").slideDown("slow");
                    $(".fastArticle").slideDown("slow");
                }
            });
        }
        else {
            $("#allArticles").html("<div id='moreTopID'><center><h1>Chargement...</h1></center></div>");

            $.ajax({
                type: "POST",
                url: "/Controller/AjaxTop.php",
                data: "current="+999999999999+"&type="+typeArticle+"&typeAffichage=liste",
                cache: false,
                success: function(html){
                    $("#moreTopID").remove(); // removing old more button
                    $("#allArticles").append(html);
                    $(".article").slideDown("slow");
                    $(".fastArticle").slideDown("slow");
                }
            });
        }
        document.title = "Simplified BMX";

        return false;
    });

    // Afficher plus d'informations
    $('.plusInfosA').live("click",function() {
        var id = $(this).attr("alt");
        var test = "plusInfos"+id;
        
        obj=document.getElementById(test).style;

        if(obj.display == "none" || obj.display == "") {
            $("#"+test).slideDown("slow");
        }
        else {
            $("#"+test).slideUp("slow");
        }

        return false;
    });

    // Partie concernant l'inscription et tt ca
    $("#boutonEnregistrer").live("click", function () {
        $(this).slideUp("fast", function () {
            $("#inscriptionInput").slideDown("slow");
        });

        return false;
    });

    $("#inscriptionRetour").live("click", function () {
        $("#inscriptionInput").slideUp("fast", function () {
            $("#boutonEnregistrer").slideDown("slow");
        });

        return false;
    });

    $("#demandePassOpen").live("click", function() {
        $("#connexionSousBloc").slideUp("fast", function () {
            $("#demandePassBloc").slideDown("slow");
        });

        return false;
    });

    $("#demandePassClose").live("click", function () {
        $("#demandePassBloc").slideUp("fast", function () {
            $("#connexionSousBloc").slideDown("slow");
        });

        return false;
    });

    // Demande de mot de passe
    $("#demandePassArrow").live("click", function () {
        var mail = $("#demandePass").val();

        if(mail) {
            $.ajax({
                type: "POST",
                url: "/Controller/Membre.php",
                data: "demandePass="+mail,
                cache: false,
                success: function(html){
                    if(html == "oui") {
                        $("#demandePass").val("Entrez votre adresse mail");
                        $("#demandePassBloc").slideUp("fast", function () {
                            $("#connexionSousBloc").slideDown("slow");
                        });
                    }
                    else {
                        $("#demandePass").val("L'adresse mail n'est pas engistrée");
                    }
                }
            });
        }

        return false;
    });

    // Enregistrer un membre
    $("#validerCompte").live("click", function() {
        var pseudo = $("#inscriptionPseudo").val();
        var pass = $("#inscriptionPass").val();
        var mail = $("#inscriptionMail").val();
        var nom = $("#inscriptionNom").val();
        var prenom = $("#inscriptionPrenom").val();
        var ville = $("#inscriptionVille").val();
        var bool = "";

        if(pseudo == "Votre pseudo" || pseudo == "") { bool = "pseudo"; }
        else if(!/^[a-zA-Z0-9._-]{3,16}$/.test(pseudo)) { bool = "pseudo"; }
        else if(!/^[a-zA-Z0-9._-]{3,16}$/.test(pass)) { bool = "mot de passe"; }
        else if(pass == "" || pass == "zdefzd") { bool = "mot de passe"; }
        else if(mail == "" || mail == "Votre mail") { bool = "mail"; }
        else if(!/^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,6}$/.test(mail)) { bool = "mail"; }
        else if(nom == "" || nom == "Votre nom") { bool = "nom"; }
        else if(prenom == "" || prenom == "Votre prénom") { bool = "prénom"; }
        else if(ville == "" || ville == "Votre ville") { bool = "ville"; }

        if(bool != "") {
            $("#erreurInscr").slideUp("fast", function() {
                $("#erreurInscr").html("Erreur avec le champs <i>"+bool+"</i>.<br /><span style='color:#000;font-weight:normal;'>Le <b>pseudo</b> et le <b>mot de passe</b> doivent faire entre 3 et 16 caractères et ne doit pas contenir de caractères spéciaux.</span>");
                $("#erreurInscr").slideDown("slow");
            });
        }
        else {
            $("#erreurInscr").slideUp("fast", function () {
                $("#erreurInscr").html("<font color='black'>Chargement...</font>");
                $("#erreurInscr").slideDown("slow");
                $.ajax({
                    type: "POST",
                    url: "/Controller/Membre.php",
                    data: "pseudo="+encodeURIComponent(pseudo)+"&pass="+encodeURIComponent(pass)+"&mail="+encodeURIComponent(mail)+"&nom="+encodeURIComponent(nom)+"&prenom="+encodeURIComponent(prenom)+"&ville="+encodeURIComponent(ville),
                    cache: false,
                    success: function(html){
                        $("#erreurInscr").html(html);
                        $("#erreurInscr").slideDown("slow");
                    }
                });
            });
        }

        return false;
    });

    // poste de commentaire
    $("#comAction").live("click", function() {
        var pseudo = $("#comPseudo").val();
        var com = $("#comPost").val();
        var idNews = $(this).attr("alt");
        var lastCom = $(this).attr("lastCom");

        $(this).val("Chargement...");
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxCommentaire.php",
            data: "pseudo="+encodeURIComponent(pseudo)+"&com="+encodeURIComponent(com)+"&idNews="+idNews+"&lastCom="+lastCom,
            cache: false,
            success: function(html){
                $(this).val("Commenter maintenant");
                $("#comPost").val("");
                $("#listeCommentaires").append(html);
                var newID = $("#listeCommentaires .comFull:last-child").attr("idCom");
                $("#comAction").attr("lastCom", newID);
            }
        });
    });

    // gestion du bloc com/forum de droite
    $("#commentaires").live("click", function () {
        $("#forumBloc").html("Chargement...");
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxInfosDroites.php",
            data : "type=commentaire",
            cache: false,
            success: function(html){
                $("#commentairesBloc").html(html);
            }
        });
    });
    $("#forum").live("click", function () {
        $("#commentairesBloc").html("Chargement...");
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxInfosDroites.php",
            data : "type=forum",
            cache: false,
            success: function(html){
                $("#forumBloc").html(html).fadeIn("slow");
            }
        });
    });
    $("#membres").live("click", function () {
        $("#annoncesBloc").html("Chargement...");
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxInfosDroites.php",
            data : "type=membre",
            cache: false,
            success: function(html){
                $("#membresBloc").html(html);
            }
        });
    });
    $("#annonces").live("click", function () {
        $("#membresBloc").html("Chargement...");
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxInfosDroites.php",
            data : "type=annonce",
            cache: false,
            success: function(html){
                $("#annoncesBloc").html(html).fadeIn("slow");
            }
        });
    });
    
    /* Espace connexion */
    $("#seConnecter").live("click", function() {
        var pseudo = $("#connexionPseudo").val();
        var pass = $("#connexionPass").val();
        
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxConnexion.php",
            data : "pseudo="+pseudo+"&pass="+pass,
            cache: false,
            success: function(html){
                if(html == 0) {
                    $("#erreurConnexion").html("Erreur :<br>La combinaison pseudo/pass est mauvaise ou votre comte n'a pas été validé").slideDown("slow");
                }
                else {
                    $("#connexionSousBloc").slideUp("fast", function() {
                        $("#connexionSousBloc").html(html);
                        $("#connexionSousBloc").slideDown("slow");
                    });
                    var expireDate = new Date();
                    expireDate.setTime(expireDate.getTime() + 360*24*3600*1000);
                    document.cookie = "connexionPersistance=1;expires=" + expireDate.toGMTString()+";path=/";
                    document.cookie = "connexionPersistance=1;expires=" + expireDate.toGMTString()+";path=/forum";
                    document.cookie = "connexionPersistance=1;expires=" + expireDate.toGMTString()+";path=/communaute";
                    document.cookie = "connexionPersistance=1;expires=" + expireDate.toGMTString()+";path=/annonces";
                    $.ajax({
                        type: "POST",
                        url: "/Controller/AjaxConnexion.php",
                        data : "recupererId=1&pseudo="+pseudo,
                        cache: false,
                        success: function(html){
                            document.cookie = "id="+html+";expires=" + expireDate.toGMTString()+";path=/";
                            document.cookie = "id="+html+";expires=" + expireDate.toGMTString()+";path=/forum";
                            document.cookie = "id="+html+";expires=" + expireDate.toGMTString()+";path=/communaute";
                            document.cookie = "id="+html+";expires=" + expireDate.toGMTString()+";path=/annonces";
                        }
                    });
                }
            }
        });
        return false;
    });
    
    $("#deconnexion").live("click", function() {
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxConnexion.php",
            data : "deconnexion=enculure",
            cache: false,
            success: function(html){
                $("#connexionSousBloc").slideUp("fast", function() {
                    $("#connexionSousBloc").html(html);
                    $("#connexionSousBloc").slideDown("slow");
                });
                var expireDate = new Date();
                expireDate.setTime(expireDate.getTime() + 360*24*3600*1000);
                document.cookie = "connexionPersistance=0;expires=" + expireDate.toGMTString()+";path=/";
                document.cookie = "connexionPersistance=0;expires=" + expireDate.toGMTString()+";path=/forum";
                document.cookie = "connexionPersistance=0;expires=" + expireDate.toGMTString()+";path=/communaute";
                document.cookie = "connexionPersistance=0;expires=" + expireDate.toGMTString()+";path=/annonces";
            }
        });
        return false;
    });
    
    // envoi de mail
    $("#contactAction").live("click", function() {
        var mail = $("#contactMail").val();
        var sujet = $("#contactSujet").val();
        var message = $("#contactContenu").val();
        
        if(mail == "Votre mail" || mail == "") {
            $("#notifMail").slideUp("fast", function() {
                $("#notifMail").html("<font color='red'><b>Le champ <i>mail</i> n'a pas &eacute;t&eacute; rempli</b></font>");
                $("#notifMail").slideDown("slow");
            });
        }
        else if(message == "") {
            $("#notifMail").slideUp("fast", function() {
                $("#notifMail").html("<font color='red'><b>Le <i>message</i> est vide</b></font>");
                $("#notifMail").slideDown("slow");
            });
        }
        else {
            $.ajax({
                type: "POST",
                url: "/Controller/AjaxContact.php",
                data : "mail="+mail+"&sujet="+sujet+"&message="+encodeURIComponent(message),
                cache: false,
                success: function(){
                    $("#notifMail").slideUp("fast", function() {
                        $("#notifMail").html("<b>Votre message a bien &eacute;t&eacute; envoy&eacute;, merci.</b>");
                        $("#notifMail").slideDown("slow");
                        $("#contactContenu").val("");
                    });
                }
            });
        }
        
        return false;
    });
    
    $("#choixCat").live("change", function() {
        var select = $(this).val();
        $("#allBlocForum").slideUp("fast");
        $.ajax({
            type: "POST",
            url: "/forum/Controller/AjaxForumSujet.php",
            data : "cat="+select,
            cache: false,
            success: function(html){
                $("#allBlocForum").html(html);
                $("#allBlocForum").slideDown("slow");
            }
        });
    });
    
    $(".moreSujet").live("click", function () {
       var lastPost = $(this).attr("id");
       var cat = $(this).attr("alt");
       
       $("#moreSujetID").html("<center><h1>Chargement...</h1></center>");
       
       $.ajax({
            type: "POST",
            url: "/forum/Controller/AjaxForumSujet.php",
            data : "categorie="+cat+"&lastPost="+lastPost,
            cache: false,
            success: function(html){
                $("#moreSujetID").remove();
                $("#allBlocForum").append(html).slideDown("slow");
            }
        });
       
       return false;
    });
    
    /* ajout d'un sujet sur le forum */
    $("#formNewSujet").live("submit", function () {
        var sujet = $("#newSujetTitre").val();
        var contenu = $("#newSujetPost").val();
        var cat = $("#newSujetCat").val();
        
        if(sujet == "" || sujet == "Titre du sujet") {
            $("#notifSujet").slideUp("fast", function() {
                $("#notifSujet").html("<font color='red'><b>Le champ <i>Titre</i> n'a pas &eacute;t&eacute; rempli</b></font>");
                $("#notifSujet").slideDown("slow");
            });
            
            return false;
        }
        else if(cat == 8) {
            $("#notifSujet").slideUp("fast", function() {
                $("#notifSujet").html("<font color='red'><b>Aucune <i>cat&eacute;gorie</i> n'a &eacute;t&eacute; choisie</b></font>");
                $("#notifSujet").slideDown("slow");
            });
            
            return false;
        }
        else if(contenu == "") {
            $("#notifSujet").slideUp("fast", function() {
                $("#notifSujet").html("<font color='red'><b>Le sujet est vide, ajoutez du <i>contenu</i> d'abord</b></font>");
                $("#notifSujet").slideDown("slow");
            });
            
            return false;
        }
        
        return true;
    });
    
    $("#forumPostAction").live("click", function() {
        var contenu = $("#forumPost").val();
        if(contenu == "") {
            $("#notifSujetPost").slideUp("fast", function() {
                $("#notifSujetPost").html("<font color='red'><b>Votre réponse est vide.</b></font>");
                $("#notifSujetPost").slideDown("slow");
            });
            return false;
        }
        
        return true;
    });
    
    // poste de commentaire Membre
    $("#comMembreAction").live("click", function() {
        var com = $("#comMembrePost").val();
        var idDest = $(this).attr("alt");
        var lastCom = $(this).attr("lastCom");

        $(this).val("Chargement...");
        $.ajax({
            type: "POST",
            url: "/communaute/Controller/AjaxCommentaireMembre.php",
            data: "com="+encodeURIComponent(com)+"&idDest="+idDest+"&lastCom="+lastCom,
            cache: false,
            success: function(html){
                $(this).val("Commenter maintenant");
                $("#comMembrePost").val("");
                $("#listeCommentaires").append(html);
                var newID = $("#listeCommentaires .comFull:last-child").attr("idCom");
                $("#comMembreAction").attr("lastCom", newID);
            }
        });
        
        return false;
    });
    
    $("#modifMembreGeneral").live("click", function() {
        var nom = $("#modifNom").val();
        var prenom = $("#modifPrenom").val();
        var ville = $("#modifVille").val();
        var jour = $("#jBirth").val();
        var mois = $("#mBirth").val();
        var annee = $("#aBirth").val();
        
        $(this).val("Chargement...");
        $.ajax({
            type: "POST",
            url: "/communaute/Controller/AjaxModifMembre.php",
            data: "nom="+encodeURIComponent(nom)+"&prenom="+encodeURIComponent(prenom)+"&ville="+encodeURIComponent(ville)+"&jour="+jour+"&mois="+mois+"&annee="+annee,
            cache: false,
            success: function(html){
                $("#modifMembreGeneral").val("Valider les modifications");
                $("#notifGenerales").slideUp("fast", function () {
                    if(html == 1) {
                        $("#notifGenerales").html("<br /><b>Les modifications ont bien été prises en compte.</b>");
                    }
                    else if(html == 0) {
                        $("#notifGenerales").html("<br /><font color='red'><b>Votre connexion a été perdue entre temps, reconnectez-vous et recommencez.</b></font>");
                    }
                    else {
                        $("#notifGenerales").html("<br /><font color='red'><b>Une erreur est survenue, rechargez la page et recommencez.</b></font>");
                    }
                    $("#notifGenerales").slideDown("slow");
                });
            }
        });
        
        return false;
    });
    
    $("#modifMembreAutre").live("click", function() {
        var riders = $("#modifRiders").val();
        var videos = $("#modifVideos").val();
        var music = $("#modifMusic").val();
        var jour = $("#modifJour").val();
        var mot = $("#modifMot").val();
        
        $(this).val("Chargement...");
        $.ajax({
            type: "POST",
            url: "/communaute/Controller/AjaxModifMembre.php",
            data: "riders="+encodeURIComponent(riders)+"&videos="+encodeURIComponent(videos)+"&music="+encodeURIComponent(music)+"&jour="+encodeURIComponent(jour)+"&mot="+encodeURIComponent(mot),
            cache: false,
            success: function(html){
                $("#modifMembreAutre").val("Valider les modifications");
                $("#notifAutres").slideUp("fast", function () {
                    if(html == 1) {
                        $("#notifAutres").html("<br /><b>Les modifications ont bien été prises en compte.</b>");
                    }
                    else if(html == 0) {
                        $("#notifAutres").html("<br /><font color='red'><b>Votre connexion a été perdue entre temps, reconnectez-vous et recommencez.</b></font>");
                    }
                    else {
                        $("#notifAutres").html("<br /><font color='red'><b>Une erreur est survenue, rechargez la page et recommencez.</b></font>");
                    }
                    $("#notifAutres").slideDown("slow");
                });
            }
        });
        
        return false;
    });
    
    $("#oneNewImg").live("click", function() {
        var lastImg = parseInt($(this).attr("alt"));
        lastImg += 1;
        $("#listeNewImg").append("<br /><input type='file' id='"+lastImg+"' name='"+lastImg+"' />").slideDown("slow");
        $(this).attr("alt", lastImg);
        return false;
    });
    
    $(".lienSuppr").live("click", function() {
        var id = $(this).attr("alt");
        $.ajax({
            type: "POST",
            url: "/communaute/Controller/AjaxModifMembre.php",
            data: "idPhoto="+id,
            cache: false,
            success: function(html){
                if(html == 1) {
                    $("#img"+id).fadeOut("slow", function() {
                        $("#img"+id).remove();
                    });
                }
                else if(html == 0) {
                    $("#notifPhotos").html("<br /><font color='red'><b>Votre connexion a été perdue entre temps, reconnectez-vous et recommencez.</b></font>");
                }
                else {
                    $("#notifPhotos").html("<br /><font color='red'><b>Une erreur est survenue, rechargez la page et recommencez.</b></font>");
                }
            }
        });
        return false;
    });
    
    $("#addProfilPhoto").live("click", function() {
        $("#photoProfilBoite").slideDown("slow");
        return false;
    });
    
    $(".moreVDJ").live("click", function () {
        var lastTime = $(this).attr("id");
        $(".moreVDJ").html("Chargement...");
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxVDJ.php",
            data: "lastTime="+lastTime,
            cache: false,
            success: function(html){
                $(".moreVDJ").remove(); 
                $("#allArticles").append(html);
                $(".blocVDJ").slideDown("slow");
            }
        });
        return false;
    });
    
    $(".moreActualites").live("click", function () {
        var lastTime = $(this).attr("id");
        $(".moreActualites").html("Chargement...");
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxActualites.php",
            data: "lastTime="+lastTime,
            cache: false,
            success: function(html){
                $(".moreActualites").remove(); 
                $("#allArticles").append(html);
                $(".plusInfos").slideDown("slow");
            }
        });
        return false;
    });
    
    $(".moreRecherche").live("click", function() {
        var lastTime = $(this).attr("id");
        var tag = $(this).attr("alt");
        
        $(".moreRecherche").html("Chargement...");
        $.ajax({
            type: "POST",
            url: "/Controller/AjaxRecherche.php",
            data: "lastTime="+lastTime+"&tag="+tag,
            cache: false,
            success: function(html){
                $("#boite"+lastTime).remove(); 
                $("#allArticles").append(html);
                $(".miniRecherche").slideDown("slow");
            }
        });
       return false; 
    });
    
    $(".moreMess").live("click", function () {
        var lastTime = $(this).attr("id");
        $(".moreMess").html("Chargement...");
        $.ajax({
            type: "POST",
            url: "/communaute/Controller/AjaxMess.php",
            data: "lastTime="+lastTime,
            cache: false,
            success: function(html){
                $("#moreMessID").remove(); 
                $("#allArticles").append(html);
                $(".messBoite").slideDown("slow");
            }
        });
        return false;
    });
    
    $(".messSuppr a").live("click", function() {
        var key = parseInt($(this).attr("id"));
        var id = parseInt($(this).attr("alt"));
        
        if(key == ((((id+3)*3)-4)*2)) {
            $.ajax({
                type: "POST",
                url: "/communaute/Controller/AjaxMess.php",
                data: "idSuppr="+id,
                cache: false,
                success: function(){
                    $("#message-"+id).slideUp("slow", function() {
                        $("#message-"+id).remove();
                    });
                }
            });
        }
        
        return false;
    });
    
    $(".messContenu a").live("click", function() {
        var key = parseInt($(this).attr("alt"));
        var id = parseInt($(this).attr("id"));
        $('html,body').animate({scrollTop: 0}, 'slow');
        $("#allArticles").html("<center><h1>Chargement...</h1></center>");
        
        if(key == ((((id+3)*3)-4)*2)) {
            $.ajax({
                type: "POST",
                url: "/communaute/Controller/AjaxMess.php",
                data: "idVisu="+id,
                cache: false,
                success: function(html){
                    $("#allArticles").html(html);
                    $(".plusInfos").slideDown("slow");
                }
            });
        }
        
        return false;
    });
    
    $("#messRepondre").live("click", function() {
       var dest = $("#messDestValue").val();
       var titre = "RE:"+$("#messTitreValue").val();
       var message = $("#messCorpsValue").val();
       var corps = $("#messRep").val();
       
       $.ajax({
            type: "POST",
            url: "/communaute/Controller/AjaxMess.php",
            data: "dest="+dest+"&titre="+encodeURIComponent(titre)+"&corps="+encodeURIComponent(corps)+"&message="+encodeURIComponent(message),
            cache: false,
            success: function(html){
                $(".plusInfos").slideUp("fast", function() {
                    if(html == 1) {
                        $(".plusInfos").html("Votre message a été envoyé avec succès.");
                    }
                    else {
                        $(".plusInfos").html("Votre message n'a pas pu être envoyé. Vérifiez que vous êtes connecté."+html);
                    }
                    $(".plusInfos").slideDown("slow");
                });
            }
        });
       
       return false;
    });
    
    $("#newMessRepondre").live("click", function() {
        var dest = $("#newMessDest").val();
        var titre = $("#newMessTitre").val();
        var corps = $("#messRep").val();
        var mess = "";
        
        if(dest == "0") {
            mess = "Aucun destinataire selectionné";
        }
        else if(titre == "Sujet du message") {
            mess = "Le champs titre est vide.";
        }
        else if(corps == "") {
            mess = "Le message est vide";
        }
        if(mess != "") {
            $("#notifMess").slideUp("fast", function() {
                $("#notifMess").html("<font color='#ff0000'><b>"+mess+"</b></font>");
                $("#notifMess").slideDown("slow");
            });
        }
        else {
            $.ajax({
                type: "POST",
                url: "/communaute/Controller/AjaxMess.php",
                data: "dest="+dest+"&titre="+encodeURIComponent(titre)+"&corps="+encodeURIComponent(corps),
                cache: false,
                success: function(html){
                    $(".plusInfos").slideUp("fast", function() {
                        if(html == 1) {
                            $(".plusInfos").html("Votre message a été envoyé avec succès.");
                        }
                        else {
                            $(".plusInfos").html("Votre message n'a pas pu être envoyé. Vérifiez que vous êtes connecté."+html);
                        }
                        $(".plusInfos").slideDown("slow");
                    });
                }
            });
        }
        return false;
    });
    
    $("#moreAnnonces a").live("click", function() {
        var lastId = $(this).attr("id");
        var mar = $(this).attr("mar");
        var cat = $(this).attr("cat");
        
        $("#moreAnnonces").html("<hr /><center><h1>Chargement...</h1></center>");
        
        $.ajax({
            type: "POST",
            url: "/annonces/Controller/AjaxAnn.php",
            data: "lastId="+lastId+"&marque="+mar+"&cat="+cat,
            cache: false,
            success: function(html){
                $("#moreAnnonces").remove();
                $("#listeAnnonces").append(html);
                $(".annPreBoite").slideDown("slow");
            }
        });
        return false;
    });
    
    $("#chercheAnn").live("click", function() {
        var mar = $("#marqueAnn").val();
        var cat = $("#catAnn").val();
        $("#listeAnnonces").html("<center><h1>Chargement...</h1></center>");
        
        $.ajax({
            type: "POST",
            url: "/annonces/Controller/AjaxAnn.php",
            data: "lastId="+99999999+"&marque="+mar+"&cat="+cat,
            cache: false,
            success: function(html){
                $("#moreAnnonces").remove();
                $("#listeAnnonces").html(html);
                $(".annPreBoite").slideDown("slow");
            }
        });
        return false;
    });
    
    $("#modifAnnButton").live("click", function() {
        var id = $(this).attr("alt");
        var cat = $("#modifAnnCat").val();
        var marque = $("#modifAnnMarque").val();
        var prix = $("#modifAnnPrix").val();
        var nom = $("#modifAnnNom").val();
        var desc = $("#modifAnnDesc").val();
        var notif = "";
        $("#notifAnnonce").slideUp("fast");
        
        if(prix == "") {
            notif = "<b><font color='#ff0000'>Le champs prix est vide.</font></b>";
        }
        else if(nom == "") {
            notif = "<b><font color='#ff0000'>Le champs nom est vide.</font></b>";
        }
        else if(desc == "") {
            notif = "<b><font color='#ff0000'>La description est vide.</font></b>";
        }
        if(notif == "") {
            $.ajax({
                type: "POST",
                url: "/annonces/Controller/AjaxModifAnn.php",
                data: "id="+id+"&cat="+cat+"&marque="+marque+"&prix="+encodeURIComponent(prix)+"&nom="+encodeURIComponent(nom)+"&desc="+encodeURIComponent(desc),
                cache: false,
                success: function(){
                    $("#notifAnnonce").html("<b>Votre annonce a bien été modifiée.</b>");
                    $("#notifAnnonce").slideDown("slow");
                }
            });
        }
        else {
            $("#notifAnnonce").html(notif);
            $("#notifAnnonce").slideDown("slow");
        }
        return false;
    });
    
    $("#newAnn").live("submit", function() {
        var prix = $("#newAnnPrix").val();
        var desc = $("#newAnnDesc").val();
        var marque = $("#newAnnMarque").val();
        var cat = $("#newAnnCat").val();
        var notif = "";
        $("#notifPhotosAnn").slideUp("fast");
        
        if(cat == 0) {
            notif = "<font color='#ff0000'><b>Aucune catégorie sélectionnée.</b></font>";
        }
        else if(marque == 0) {
            notif = "<font color='#ff0000'><b>Aucune marque sélectionnée.</b></font>";
        }
        else if(prix == "Prix souhaité" || prix == "") {
            notif = "<font color='#ff0000'><b>Le champ prix est vide.</b></font>";
        }
        else if(desc == "Description de votre annonce" || desc == "") {
            notif = "<font color='#ff0000'><b>Aucune description.</b></font>";
        }
        
        if(notif == "") {
            return true;
        }
        else {
            $("#notifPhotosAnn").html(notif);
            $("#notifPhotosAnn").slideDown("slow");
            return false;
        }
    });
    
    $(".supprAnn").live("click", function() {
        var id = $(this).attr("alt");
        
        $.ajax({
            type: "POST",
            url: "/annonces/Controller/AjaxModifAnn.php",
            data: "idSuppr="+id,
            cache: false,
            success: function(){
                $("#boiteAnn"+id).slideUp("slow", function() {
                    $("#boiteAnn"+id);
                });
            }
        });
    });
});


