

function getSpaceFromLetter(letter,idReject) {

    //reload news
    $.ajax({
        type: "POST",
        url: urlConfig+"/wm-public/scripts/recommendations-getSpaceFromLetter.ajax.php",
        data: "letter="+letter+"&idReject="+idReject,
        success: function(data){
            $('.letter').removeClass("active");
            $('.let'+letter).addClass("active");
            $("#ulSpace").html(data);
        }
    });
}

function getSpaceFromId(id) {

    //reload news
    $.ajax({
        type: "GET",
        url: urlConfig+"/wm-public/scripts/recommendations-getSpaceFromId.ajax.php",
        data: "idSpace="+id,
        success: function(data){
            $("#recommandations-display").html(data);
        }
    });
}

function setRecommandations(id,spaces) {


    $("#recommandations-display4").html("Confirmer la(les) recommandation(s) ?");

            $("#recommandations-display4")
            .dialog("destroy")
                    .dialog({
                        autoOpen: true,
                        height: 'auto',
                        width: '500',position : [450,300],
                        top : 0,
                        modal: true,
                        resizable: false,
                        buttons: {
                            "Confirmer" : function() {
                                 $.ajax({
                                            type: "POST",
                                            url: urlConfig+"/wm-public/scripts/recommendations-get.ajax.php",
                                            data: "idSpace="+id+"&spaces="+spaces,
                                            success: function(data){

                                                $("#recommandations-display3").html(data);
                                                $("#recommandations-display2").dialog( 'close' );
                                                $("#recommandations-display3")
                                                .dialog("destroy")
                                                        .dialog({
                                                            autoOpen: true,
                                                            height: 'auto',
                                                            width: '500',position : [450,300],
                                                            top : 0,
                                                            modal: true,
                                                            resizable: false,
                                                            buttons: {
                                                                "Quitter" : function() {
                                                                    $( this ).dialog( "close" );
                                                                }
                                                            }
                                                        });
                                            },
                                            error : function(){

                                                $("#recommandations-display3").html("Une erreur inconnue est survenue, veuillez réessayer.");

                                                $("#recommandations-display3")
                                                .dialog("destroy")
                                                        .dialog({
                                                            autoOpen: true,
                                                            height: 'auto',
                                                            width: '500',position : [450,300],
                                                            top : 0,
                                                            modal: true,
                                                            resizable: false,
                                                            buttons: {
                                                                "Reessayer" : function() {
                                                                    setRecommandations(id,spaces);
                                                                },
                                                                "Quitter" : function() {

                                                                    $( this ).dialog( "close" );
                                                                }


                                                            }
                                                        });


                                            }
                                        });
                                        
                                        $( this ).dialog( "close" );


                            },
                            "Annuler" : function() {

                                $( this ).dialog( "close" );
                            }


                        }
                    });
}

function getSpaceInfos(idReject) {

    //reload news
    $.ajax({
        type: "POST",
        url: urlConfig+"/wm-public/scripts/recommendations-getSpaceInfos.ajax.php",
        data : "idReject="+idReject,
        success: function(data){

            $("#recommandations-display2").html('<p><strong>Sélectionnez la (les) fiche(s) qui vont recommander ce membre :</strong></p>'+data);
        }
    });
}

function Step2(rel) {

    getSpaceInfos(rel);
    $("#recommandations-display2")
        .dialog("destroy")
                .dialog({
                    autoOpen: true,
                    height: 'auto',
                    width: '500',position : [450,300],
                    modal: true,
                    resizable: false,
                    buttons: {
                        "Envoyer la demande de recommandation" : function() {

                            SpaceID = "";

                            $('input:checkbox[name="SpaceID[]"]:checked').each(function(){

                                if(SpaceID!="")SpaceID = SpaceID+","+$(this).val();
                                    else SpaceID = $(this).val();
                            })

                            if(SpaceID=="") {

                                $("#recommandations-display2").prepend('<strong style="color:red">Veuillez sélectionner une fiche</strong>');
                                $("html:not(:animated),body:not(:animated)").animate({ scrollTop: "0px"}, 250);

                            }
                            else {
                                $( this ).dialog( "close" );
                                setRecommandations(rel,SpaceID);
                            }

                            
                        },
                        "Annuler" : function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
}

function confirmDelete(id,space) {

    $.ajax({
        type: "POST",
        url: urlConfig+"/wm-public/scripts/recommendations-delete-ajax.php",
        data: "id="+id,
        success: function(data){
            
            $("#recommandations-display2").html(data);
            $("#recommandations-display2").dialog( "close" );
            $("#recommandations-display2")
            .dialog("destroy")
                .dialog({
                    autoOpen: true,
                    height: 'auto',
                    width: '500',
                    position : [450,300],
                    modal: true,
                    resizable: false,
                    buttons: {
                        "Quitter" : function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
        }
    });
    
    $("#li"+id).remove();

}

$(document).ready( function () {

    $(".consult-recom").click(function (){

            var rel = $(this).attr("rel");

            $.ajax({
                type: "POST",
                url: urlConfig+"/wm-public/scripts/recommendations-getFromUser-ajax.php",
                data: "idSpace="+rel,
                success: function(data){
                    $("#recommandations-display").html(data);
                }
            });

            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0+"px"}, 1100);

    })

    $("#reco .picto-remove").click(function (){

        title = $(this).attr("title");
        id = $(this).attr("rel");
        space = $(this).attr("space");

        $("#recommandations-display2").html("Voulez-vous vraiment supprimer "+title+" ?");

        $("#recommandations-display2")
            .dialog("destroy")
                .dialog({
                    autoOpen: true,
                    height: 'auto',
                    width: 500,
                    top : 0,
                    modal: true,
                    resizable: false,
                    buttons: {
                        "Supprimer" : function() {
                            confirmDelete(id,space);

                        },
                        "Annuler" : function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
        })

   /* $(".recommandationsCenter a.dialog").click(function (){

        var rel = $(this).attr("rel");
        getSpaceFromId(rel);

        $("#recommandations-display")
        .dialog("destroy")
                .dialog({
                    autoOpen: true,
                    height: 'auto',
                    width: '500',position : [450,300],
                    top : 0,
                    modal: true,
                    resizable: false,
                    buttons: {
                        "Etape suivante" : function() {
                             Step2(rel);
                             $( this ).dialog( "close" );
                        },
                        "Annuler" : function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });

    });*/

    $("#modDialog").click(function (){

        var rel = $(this).attr("rel");
        getSpaceFromId(rel);

        $("#recommandations-display")
        .dialog("destroy")
                .dialog({
                    autoOpen: true,
                    height: 'auto',
                    width: '500',position : [450,300],
                    top : 0,
                    modal: true,
                    resizable: false,
                    buttons: {
                        "Etape suivante" : function() {
                             Step2(rel);
                             $( this ).dialog( "close" );
                        },
                        "Annuler" : function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });

    });
});
