/**
 * @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_news_ajax_trigger_submit() {
        clear_url_parameters();
        $('#views-exposed-form-nhlbi-all-news-block-1 .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);
        }
      }

      //Go through each filter parameters, if it's a select input, mark that input as checked
      function forEachParams(item, index){
        if(index !== "langcode" || index !== "field_publish_date_custom"){
          $("input[name='"+ index +"']").setAttribute('checked','true');
        }
      }

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



      $( "#news-type-filters" ).click(function(e) {
        if( e.target.className != "selected-filter-options") return;
        $( ".filters-lightbox-buttons-wrapper" ).hide();
        $( "#filters-wrapper-lightbox-news-type" ).show();
      });

      $( "#topic-filters" ).click(function(e) {
        if( e.target.className != "selected-filter-options") return;
        $( ".filters-lightbox-buttons-wrapper" ).hide();
        $( "#filters-wrapper-lightbox-topic" ).show();
      });

      $( "#language-filters" ).click(function(e) {
        if( e.target.className != "selected-filter-options") return;
        $( ".filters-lightbox-buttons-wrapper" ).hide();
        $( "#filters-wrapper-lightbox-language" ).show();
      });

      $( "#published-date-filters" ).click(function(e) {
        if( e.target.className != "selected-filter-options") return;
        $( ".filters-lightbox-buttons-wrapper" ).hide();
        $( "#filters-wrapper-lightbox-published-date" ).show();
      });




      $( ".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 input:checked').each(
          function (key, element) {
            if (!urlParams.has(element.name)) {
              element.checked = false;
            }
          }
        );
        $('.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 #news-type-button" ).click(function() {
        $( ".filters-lightbox-buttons-wrapper" ).hide();
        $( "#news_type_filter" ).hide();
        $( "#filters-wrapper-lightbox-news-type" ).show( );
        $( "#news_type_filter" ).show(250 );
      });

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

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

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


      $( document ).on("click","#delete-news-type-filter", function(e) {
        e.preventDefault();
        $('#news_type_filter input').removeAttr('checked');
        $(".selected-filter-options").hide("slide", { direction: "left" }, 500);
        nhlbi_all_news_ajax_trigger_submit();
      });

      $( document ).on("click","#delete-topic-filter", function(e) {
        e.preventDefault();
        $('#topic_filter input').removeAttr('checked');
        $(".selected-filter-options").hide("slide", { direction: "left" }, 500);
        nhlbi_all_news_ajax_trigger_submit();
      });

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

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





      $( "#edit-done-button-news-type" ).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_news_ajax_trigger_submit();
      });


      $( "#edit-done-button-topic" ).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_news_ajax_trigger_submit();
      });

      $( "#edit-done-button-language" ).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_news_ajax_trigger_submit();
      });

      $( "#edit-done-button-published-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_news_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_news_ajax_trigger_submit), 500);
            }
          }

          if ($.inArray(e.keyCode, discardKeyCode) === -1) {
            befTimeoutID && clearTimeout(befTimeoutID);
          }
        })
        /*     .change(function (e) {
         if ($.inArray(e.keyCode, discardKeyCode) === -1) {
         if (this.value != undefined & this.value.length > 1){
         befTimeoutID = setTimeout($.proxy(nhlbi_science_ajax_trigger_submit), 1000);
         }
         }
         })*/
        .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_news_ajax_trigger_submit), 1000);
              $('#text-input-cross').show();
            }else if(this.value.length < 2){
              //befSearchString = this.value;
              befTimeoutID = setTimeout($.proxy(nhlbi_all_news_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_news_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_news_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"