$(document).ready(function () {

    //Recherche depuis l'accueil
    $('.home-search').on('change', function (e) {
        var url = $(this).data('url');
        var search = $(this).val();
        url = URI(url).addSearch({ dtsearch: search }).toString()
        window.location.href = url;
    });

    //Paramétrage des data-tables
    $('.datatable').DataTable({
        "language": {
            "url": "/Scripts/dataTables.french.lang"
        },
        "search": {
            "search": URI(window.location.href).search(true)['dtsearch']
        },
        responsive: {
            details: false
        },
        dom: 'Bfrtip',
        buttons: [
            'excel', 'pdf'
        ]
    });

    // Une datatable responsive simplifiée (sans pagination, recherche...etc).
    $('.simple-datatable').DataTable({
        "language": {
            "url": "/Scripts/dataTables.french.lang"
        },
        responsive: {
            details: true
        },
        paging: false,
        searching: false,
        ordering: true
    });

    $('.datatable, .simple-datatable').on('click', 'tbody>tr', function () {
        var sel = getSelection().toString(); // verifie qu'il ne s'agit pas d'une selection de texte
        if (!sel && $(this).attr('data-url') !== undefined) {
            var url = $(this).data('url');
            window.location.href = url;
        }
    });

    //Conservation de la trace (pages d'admin)
    $("a.start-trace").on("click", function () {
        sessionStorage.setItem("trace", window.location);
    });

    $("body").on("click", "a", function () {
        if ($("a.keep-trace").length > 0 & sessionStorage.getItem("trace")===null) {
            sessionStorage.setItem("trace", $("a.keep-trace").first().attr("href"));
        }
    });

    if ($("a.keep-trace").length > 0) {
        if (sessionStorage.getItem("trace") !== null) {
            $("a.keep-trace").attr("href", sessionStorage.getItem("trace"));
        }
    }


});
