$( document ).ready(function() {

    if($('.indice-actu').length > 0){
        var indiceClicked = null;
        var pageInfotrafic = $('.indice-actu').attr("data-ulrinfotrafic");

        $('.actu').each(function (index) {
            var lines = $(this).attr("data-lines").split(',');
            if(typeof  $(this).attr("data-gravite") != undefined) {
                var gravite = $(this).attr("data-gravite");
                if (gravite > 0) {
                    $.each(lines, function (index, value) {
                        if (value != "") {
                            $('.indice[data-code=' + value + ']').parent().addClass('typalert'+gravite);
                        }
                    });
                }
            }
        });
$('.indice').on("keydown", function (e) {
    if (e.which == 13 || e.which == 32) {
        e.stopPropagation();
        $(e.target).click();
        return false;
    }
});

// $('.indice').attr("tabindex", 1);

        $('.indice').on("click", function () {
            $('div#if-depliant > details').removeAttr('open');
            $('.indice').removeClass("indice_on");
            $('.indice').attr("aria-expanded","false")
            $('.indice').attr("aria-pressed","false")
            indiceClicked = $(this).attr("data-code");
            $(this).addClass("indice_on");
            $(this).attr("aria-expanded","true")
            $(this).attr("aria-pressed","true")

            $('.actu').each(function(){
                var lines = $(this).attr("data-lines").split(',')
                if($.inArray(indiceClicked, lines) == -1 && indiceClicked != "all"){
                    $(this).hide()
                } else {
                    $(this).show()
                    $("a.lien_actu",this).attr("href",pageInfotrafic+"?indice="+indiceClicked)
                }
            });
            if ($('div#if-depliant > details').not('[style*="display: none"]').length == 1) {
                $('div#if-depliant > details').not('[style*="display: none"]').first().attr('open', 'open');
            }
        });

        if(typeof(preselect) != "undefined" && preselect != "" && preselect != "undefined"){
            $('.indice[data-code='+preselect+']').trigger("click")
        }else{
            $(".indice[data-code='all']").trigger("click")
        }
    }

});