/**
 * @file
 * A JavaScript file for the theme.
 *
 * In order for this JavaScript to be loaded on pages, see the instructions in
 * the README.txt next to this file.
 */



// JavaScript should be made compatible with libraries other than jQuery by
// wrapping it with an "anonymous closure". See:
// - https://drupal.org/node/1446420
// - http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
(function ($, Drupal, window, document, undefined) {

Drupal.behaviors.nhlbi_foa_filters = {
    attach: function (context) {
        function nhlbi_all_events_ajax_trigger_submit() {
            clear_url_parameters();
            $('#views-exposed-form-nhlbi-all-events-block-9 .form-actions .form-submit').trigger('click');
        }

        function clear_url_parameters(){
            var uri = window.location.toString();
            if (uri.indexOf("?") > 0) {
                var clean_uri = uri.substring(0, uri.indexOf("?"));
                window.history.replaceState({}, document.title, clean_uri);
            }
        }

        $( "#edit-add-other-filters" ).click(function() {
            $( ".filters-lightbox-buttons-wrapper" ).show();
        });



        $( "#start-date-filters" ).click(function(e) {
            if( e.target.className != "selected-filter-options") return;
            $( ".filters-lightbox-buttons-wrapper" ).hide();
            $( "#filters-wrapper-lightbox-start-date" ).show();
           // $( ".form-item-field-objective-target-id" ).show( );
            //$( "#division_filter" ).hide();
            // $( ".filters-wrapper" ).css("top",  150);
        });

        $( ".close-button" ).click(function() {
            $( ".filters-lightbox-buttons-wrapper" ).hide();
            $( ".filters-wrapper-lightbox" ).hide();

            //Retrieve array of parameters
            const queryString = window.location.search;
            const urlParams = new URLSearchParams(queryString);
            $('.filters-wrapper-lightbox select').each(
                function (key, element) {
                    if (!urlParams.has(element.name)) {
                        // Filter from the select is not in the url, remove it.
                        element.value = null;
                    }
                    else {
                        // If the filter is in the url, reset it to that value.
                        let selectValue = urlParams.get(element.name);
                        element.value = selectValue;
                    }
                }
            );

        });




        $( ".filters-lightbox-buttons #start-date-button" ).click(function() {
            $( ".filters-lightbox-buttons-wrapper" ).hide();
            $( "#start_date_filter" ).hide();
            $( "#filters-wrapper-lightbox-start-date" ).show( );
            $( "#start_date_filter" ).show(250 );
        });


        $( document ).on("click","#delete-start-date-filter", function(e) {
            e.preventDefault();
            // $('#expiration_date_filter input').removeAttr('checked');
            $("[name='field_event_date_custom']").val('');
            $(".selected-filter-options").hide("slide", { direction: "left" }, 500);
            nhlbi_all_events_ajax_trigger_submit();
        });





        $( "#edit-done-button-start-date" ).click(function(e) {
            e.preventDefault();
           // var serializedDOCTypeFilterNew = $('#doc_type_filter').serialize();
           // var serializedDOCTypeFilterOld = localStorage.getItem('serializedDocTypeFilter');
            $(".filters-lightbox-buttons-wrapper").hide();
            $(".filters-wrapper-lightbox").hide();
            nhlbi_all_events_ajax_trigger_submit();
        });





        var discardKeyCode = [
            16, // shift
            17, // ctrl
            18, // alt
            20, // caps lock
            33, // page up
            34, // page down
            35, // end
            36, // home
            37, // left arrow
            38, // up arrow
            39, // right arrow
            40, // down arrow
            9, // tab
            13, // enter
            27  // esc
        ];
        var befTimeoutID = 0;
        //var befSearchString = '';
        // Don't wait for change event on textfields.
        $(once('nhlbi-auto-submit', '.nhlbi-searchbar .js-form-item-title .form-text', context)).each(function (context) {
          $(this).bind('keydown keyup', function (e) {
                if(e.keyCode == 13){
                    e.preventDefault();
                    if (this.value != undefined & this.value.length > 1){
                        //befSearchString = this.value;
                        befTimeoutID = setTimeout($.proxy(nhlbi_all_events_ajax_trigger_submit), 500);
                    }
                }

                if ($.inArray(e.keyCode, discardKeyCode) === -1) {
                    befTimeoutID && clearTimeout(befTimeoutID);
                }
            })
            .keyup(function (e) {
                if ($.inArray(e.keyCode, discardKeyCode) === -1) {
                    if (this.value != undefined & this.value.length > 1){
                        // befSearchString = this.value;
                        befTimeoutID = setTimeout($.proxy(nhlbi_all_events_ajax_trigger_submit), 1000);
                        $('#text-input-cross').show();
                    }else if(this.value.length < 2){
                        //befSearchString = this.value;
                        befTimeoutID = setTimeout($.proxy(nhlbi_all_events_ajax_trigger_submit), 1000);
                    }
                }
            });
        });
        $( document ).on("click","#text-input-cross", function(e) {
            e.preventDefault();
            $('.nhlbi-searchbar .js-form-item-title .form-text').val('');
            $('#text-input-cross').hide();
            nhlbi_all_events_ajax_trigger_submit();
        });



        $(document).ajaxComplete(function(e, xhr, settings)
        {
            var url = settings.url;
            var prefix = '/views/ajax'
            if(url.lastIndexOf(prefix,0) == 0 && $('.nhlbi-searchbar .js-form-item-title .form-text')[0].value.length > 1){
                $('#text-input-cross').show();
            }
        });

        $('[data-drupal-selector="edit-items-per-page"]').change( function(e) {
            e.preventDefault();
            nhlbi_all_events_ajax_trigger_submit();
        });

    }


}



// To understand behaviors, see https://drupal.org/node/756722#behaviors
})(jQuery, Drupal, this, this.document);

//"field_related_sfas_target_id%5B270%5D=1&field_related_sfas_target_id%5B271%5D=1"
//"field_related_sfas_target_id%5B270%5D=1&field_related_sfas_target_id%5B271%5D=1"