/**
 * @file
 * Global utilities.
 *
 */
(function($, Drupal) {

  'use strict';

  Drupal.behaviors.nifc_theme = {
    attach: function(context, settings) {

      $('.block-inline-blockslideshow-with-links').once().each(function(){

        var numberSlides = 3;
        if ($(this).find('.image-style-slideshow-grid-5').length) {
          numberSlides = 5;
        }

        $(this).find('.content').slick({
          slidesToShow: numberSlides,
          slidesToScroll: 1,
          autoplay: true,
          autoplaySpeed: 2000,
          responsive: [
            {
              breakpoint: 674,
              settings: {
                slidesToShow: 1,
                slidesToScroll: 1,
              }
            }
          ]
        });

        console.log('testing');
      })

      

      var navbar = $('#header');

      var extentions = ["txt","rtf","doc","docx","ppt","pptx","xls","xlsx","pdf","odf","odg","odp","ods","odt","fodt","fods","fodp","fodg","key","numbers","pages"];

      extentions.forEach(openNewTab);

      if (navbar.length) {
          var elmHeight = $('#header').outerHeight(true);
          $(window).scroll(function() {
              var scrolltop = $(window).scrollTop();
              if (scrolltop > elmHeight) {
                  if (!navbar.hasClass('sticky')) {
                      navbar.addClass('sticky');
                  }
              } else {
                  navbar.removeClass('sticky');
              }
          });
      }

      var isHotsheetExpandAll = document.cookie.replace(/(?:(?:^|.*;\s*)hotsheet_expand_all\s*\=\s*([^;]*).*$)|^.*$/, '$1');

      if (isHotsheetExpandAll) {
        $('#hotsheet_expandCollapseAll').text('Collapse All');
        $(".collapse").collapse("show");
      }

      if($('.user-logged-in').length) {
        $('.header-bottom-container').once().each(function() {
          var headerBottomHeight = $(this).outerHeight(true);
          $('#main-wrapper').css('margin-top', headerBottomHeight + 20);
        })
      } else {
        $('.header-bottom-container').once().each(function() {
          var headerBottomHeight = $(this).height() + $('#navbar-top').height();
          $('#main-wrapper').css('margin-top', headerBottomHeight + 60);
        })
      }

      $(document).ready(function() {
        $(window).trigger('resize');
      });
      /*
      $(window).resize(function() {
        console.log('testing');
        if($('.user-logged-in').length) {
          $('.header-bottom-container').once().each(function() {
            var headerBottomHeight = $(this).outerHeight(true);
            $('#main-wrapper').css('margin-top', headerBottomHeight + 20);
          })
        } else {
          $('.header-bottom-container').once().each(function() {
            var headerBottomHeight = $(this).height() + $('#navbar-top').height();
            $('#main-wrapper').css('margin-top', headerBottomHeight + 60);
          })
        }
      });*/

      $('.webform-submission-form').once().each(function() {
        $(this).find('fieldset').each(function(){
          var fieldTitle = $(this).find('label').text();
          $(this).prepend( "<legend class='visually-hidden'>"+ fieldTitle +"</legend>" );

        })
      })



      $('.fb-read-more').once().each(function() {
        $(this).click(function(e) {
          e.preventDefault();
          if ($(this).text() == 'Show More') {
            $(this).parent().parent().find('.default-hide').show();
            $(this).parent().parent().find('.dots').hide();
            $(this).text('Show Less');
          } else {
            $(this).parent().parent().find('.default-hide').hide();
            $(this).parent().parent().find('.dots').show();
            $(this).text('Show More');
          }
        })
      })

      $('#hotsheet_expandCollapseAll').once().each(function() {
        $(this).click(function() {
          if($(this).text() == 'Expand All') {
            $(this).text('Collapse All');
            setCustomcookie("hotsheet_expand_all","1",1);
            $(".collapse").collapse("show");
          } else {
            $(this).text('Expand All');
            setCustomcookie("hotsheet_expand_all","1",0);
            $(".collapse").collapse("hide");
          }
        })

      })

      var isDocsExpandAll = document.cookie.replace(/(?:(?:^|.*;\s*)docs_expand_all\s*\=\s*([^;]*).*$)|^.*$/, '$1');

      if (isDocsExpandAll) {
        $('#docs_expandCollapseAll').text('Collapse All');
        $(".collapse").collapse("show");
      }


      $('#docs_expandCollapseAll').once().each(function() {
        $(this).click(function() {
          if($(this).text() == 'Expand All') {
            $(this).text('Collapse All');
            setCustomcookie("docs_expand_all","1",1);
            $(".collapse").collapse("show");
          } else {
            $(this).text('Expand All');
            setCustomcookie("docs_expand_all","1",0);
            $(".collapse").collapse("hide");
          }
        })

      })


      var isFaqsExpandAll = document.cookie.replace(/(?:(?:^|.*;\s*)faqs_expand_all\s*\=\s*([^;]*).*$)|^.*$/, '$1');

      if (isFaqsExpandAll) {
        $('#faqs_expandCollapseAll').text('Collapse All');
        $(".collapse").collapse("show");
      }

      var expandAllClicked = false;

      $('.paragraph--bp-accordion-container').once().each(function() {
        $(this).find('button.bp-accordion-button').click(function() {
	  if (expandAllClicked) {
            $(this).text('Expand All');
            $(".collapse").each(function(){
		$(this).collapse("hide");
	    })
	    $(".panel-title").each(function(){
		$(this).find('a.display').addClass("collapsed");
		$(this).find('a.display').attr("aria-expanded", "false");
	    })
          } else {
            $(this).text('Collapse All');
	    $(".panel-collapse").each(function(){
		$(this).addClass("show");
	    })

	    $(".panel-title").each(function(){
		$(this).find('a.display').removeClass("collapsed");
		$(this).find('a.display').attr("aria-expanded", "true");
	    })
          }
          expandAllClicked = !expandAllClicked;
        })
      })


      $('#faqs_expandCollapseAll').once().each(function() {
        $(this).click(function() {
          if($(this).text() == 'Expand All') {
            $(this).text('Collapse All');
            setCustomcookie("faqs_expand_all","1",1);
            $(".collapse").collapse("show");
          } else {
            $(this).text('Expand All');
            setCustomcookie("faqs_expand_all","1",0);
            $(".collapse").collapse("hide");
          }
        })

      })

      function openNewTab(value, index, array) {
        $('a[href$=".'+ value +'"]').each(function(){
          $(this).attr("target", "_blank");
        })
      }

      function setCustomcookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        var expires = "expires="+d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
      }
    }
  }

  // Video change when click item
  var playItem = $('.video-item');
  playItem.on('click', function() {
    var playItemVideoId = $(this).attr('data-media-id');

    if(playItemVideoId.length) {
      $('.videos-player > div').removeClass('is-active').addClass('in-active');
      $('.videos-player > div[data-media-id=' + playItemVideoId +']').addClass('is-active').removeClass('in-active');

      playItem.removeClass('is-active').addClass('in-active');
      $(this).addClass('is-active').removeClass('in-active');
    }

    StopEmbedVideo();
  });

  function StopEmbedVideo() {
    $('.video-embed.in-active').each(function() {
      var $frame = $(this).find('iframe.media-oembed-content');
      var vidsrc = $frame.attr('src');
      $frame.attr('src','');
      $frame.attr('src', vidsrc);
    })
  }

  // Dropdown list
  var sectionTitle = $('.section-playlist > h3');
  sectionTitle.click(function() {
    var $this = $(this);
    if($this.hasClass('is-active')) {
      $this.removeClass('is-active').addClass('is-hide');
    } else {
      $this.addClass('is-active').removeClass('is-hide');
    }
  });

})(jQuery, Drupal, drupalSettings);