jQuery(function () {
    var $overlay = jQuery('.navbar-collapse');
    var $toggle = jQuery('#open-button');
    var toggleOverlay = function (evt) {
        if (!jQuery(evt.target).closest($overlay).length) {
            jQuery("body").removeClass("show-menu");
            $overlay.addClass('hidden');
            $toggle.removeClass('menu-button--open');
        }
        else {
            jQuery(document).one('click', toggleOverlay);
        }
    };

    $toggle.click(function (evt) {
        jQuery("body").toggleClass("show-menu");
        $toggle.toggleClass('menu-button--open');
        evt.preventDefault();
        evt.stopPropagation();
        $overlay.toggleClass('hidden');
        jQuery(document).one('click', toggleOverlay);
    });

    $('.menu-1').bind("click", function () {
        var menu = this, submenu = $(menu).closest("li").find(".submenu");
        $(".submenu").not(submenu).addClass("hidden");
        $(menu).closest("li").find(".submenu").toggleClass('hidden');
    });

    
    $('header .icon-search').bind("click", function () {
        $("header .input-search").toggle('fast');
    });

    $("#select-annee").on("click", function (event) {
        $("#select-annee").hide();
        $("#yearpicker").show();

        $.ajax({
            type: "GET",
            url: "/Annees/GetList",
            data: '',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var comboBox = $("#yearpicker");
                comboBox.html("");
                $.each(data, function (index, itemData) {
                    var option = $("<option />");
                    option.val(itemData.Id);
                    option.html(itemData.Libelle);
                    if (itemData.Libelle === comboBox.data("default")) {
                        option.attr('selected', 'selected');
                    }
                    comboBox.append(option);
                });
            }
        });

        $("#yearpicker").focus();
        event.preventDefault();
    });

    $("#yearpicker").on("change", function () {
        var annee_id = $("#yearpicker").val();
        window.location.href = "/Annees/Details/" + annee_id;
    });

    $("#yearpicker").on("focusout", function () {
        $("#yearpicker").hide();
        $("#select-annee").show();
    });




});