(function($){

  /* Auto formats select inputs */
  $("select.form__input").each(function(){
    if(!$(this).hasClass("select--styled")){
      $(this).wrap('<div class="form__select-wrapper"></div>');
    }
  });
  $('.form__select-wrapper').append('<i class="fal fa-angle-down" aria-hidden="true"></i>');

  // Datepicker
  $(".form__date").each(function(){
    $(this).wrap('<div class="form__date-wrapper"></div>');
  });
  $('.form__date-wrapper').append('<i class="far fa-calendar-alt" aria-hidden="true"></i>');
  $('.form__date').datepicker();

  // Attaches an icon that line breaks with the last word in the targeted element
  // data-attach-icon takes font awesome icon class name as the value (excluding the fa prefix)
  $('*[data-attach-icon-after], *[data-attach-icon-before]').each(function(index, element) {
    var text = $(element), word_array, last_word, first_word, first_part;
    var attr = $(this).attr('data-attach-icon-after');
    if(typeof attr !== typeof undefined && attr !== false){
      var icon = $(this).attr('data-attach-icon-after');
      word_array = text.html().split(/\s+/);
      last_word = word_array.pop();
      first_part = word_array.join(' ');
      text.html([first_part, ' <span class="no-break">', last_word, '<i class="' + icon + '" aria-hidden="true"></i></span>'].join(''));
    }else{
      var icon = $(this).attr('data-attach-icon-before');
      word_array = text.html().split(/\s+/);
      first_word = word_array.shift();
      first_part = word_array.join(' ');
      text.html([' <span class="no-break"><i class="' + icon + '" aria-hidden="true"></i>', first_word, '</span> ', first_part].join(''));
    }
  });


  // Controls functionality for dropdown content
  $(document).on("click", ".dropdown__toggle", function(event){
    event.preventDefault();
    $(this).toggleClass("open");
    var targetContent = '#' + $(this).attr('aria-controls');
    $(targetContent).toggleClass("open");
    if($(targetContent).hasClass("open")){
      $(this).closest('.form__group-wrapper').find('.dropdown__toggle').not(this).removeClass('open').attr('aria-expanded', 'false');
      $(targetContent).siblings('.dropdown__content').removeClass('open').attr('aria-expanded', 'false').hide();   
      $(targetContent).slideDown('fast').attr("aria-expanded", "true").attr('tabindex', '-1');
      $(this).attr("aria-expanded", "true");
    }else{
      $(targetContent).slideUp('fast').attr("aria-expanded", "false");
      $(this).attr("aria-expanded", "false");
    }
    $(this).focus();
  });

  $(document).on("click", ".dropdown__close", function(event){
    $(this).closest('.dropdown').find('.dropdown__toggle, .dropdown__content').removeClass('open').attr('aria-expanded', 'false');
    $(this).closest('.dropdown').find('.dropdown__content').slideUp('fast');
  });

  // Controls functionality for sidebar toggle
  $(document).on("click", ".sidebar__toggle", function(event){
    event.preventDefault();
    $(".sidebar__toggle, .sidebar").toggleClass("open");
    if($(".sidebar").hasClass("open")){
      $(".sidebar__toggle, .sidebar").attr("aria-expanded", true);
      $(".main").addClass("main--sidebar-open");
      $('.sidebar__content').show();
    }else{
      $(".sidebar__toggle, .sidebar").attr("aria-expanded", false);
      $(".main").removeClass("main--sidebar-open");
      setTimeout(function(){
        $('.sidebar__content').hide();
      }, 200);
    }
  });

  // Close dropdown when it loses focus
  $('body').focusin(function(e) {
    $('.dropdown').each(function(){
      if(!$(e.target).is($(this).find('*'))){
        $(this).find('.dropdown__toggle').attr('aria-hidden', 'true'); // Voiceover safari fix
        $(this).find('.dropdown__content').slideUp('fast').removeClass('open').attr('aria-expanded', 'false');
        $(this).find('.dropdown__toggle').attr('aria-expanded', 'false');
        $(this).find('.dropdown__toggle').removeClass('open');
        setTimeout(function(){ 
          $('.dropdown__toggle').attr('aria-hidden', 'false'); // Voiceover safari fix
        }, 400);
      }
    });
  });

  // Controls functionality for tabbed content
  $(document).on('click', '.tabs__tab-control', function(e){
    e.preventDefault();
    var target = $(this).attr("href");
    $(".tabs__tab-control").removeClass("active").attr("aria-selected", "false");
    $(this).addClass("active").attr("aria-selected", "true");
    $(".tabs__content").removeClass("active");
    $(target).addClass("active");
  });


  // Date range picker
  $('.daterange').wrap('<span class="daterange__wrapper"></span>');
  $('.daterange__wrapper').prepend('<i class="fas fa-calendar" ariahidden="true"></i>');
  $('.daterange__wrapper').append('<i class="fal fa-angle-down" ariahidden="true"></i>');
  $('.daterange').each(function(){
    $(this).css('width', (($(this).val().length + 1) * 6.7) + 'px');
  });
  $('.daterange').on('change', function(){
    $(this).css('width', (($(this).val().length + 1) * 6.7) + 'px');
  });
  $('.daterange').on('keypress', function(){
    $(this).css('width', (($(this).val().length + 1) * 6.7) + 'px');
  });
  $('.daterange').daterangepicker({
    opens: 'left',
    locale: {
      format: 'MMMM D, YYYY'
    }
  }, function(start, end, label) {
    console.log("A new date selection was made: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
  });


  // Table formatting
  $(".table__scroll .table").each(function(){
    $(this).clone(true).appendTo($(this).closest('.table__scroll')).addClass('table__clone').attr('aria-hidden', 'true');
    $(this).clone(true).appendTo($(this).closest('.table__scroll')).addClass('table__clone').addClass('table__clone--right').attr('aria-hidden', 'true');
  });

  $(".table__clone *[aria-controls], .table__clone *[aria-labelledby]").each(function(){
    if(typeof $(this).attr('aria-controls') !== typeof undefined && $(this).attr('aria-controls') !== false) {
      $(this).attr('aria-controls', $(this).attr('aria-controls') + '-clone');
      $(this).attr('id', $(this).attr('id') + '-clone');
    }
    if(typeof $(this).attr('aria-labelledby') !== typeof undefined && $(this).attr('aria-labelledby') !== false) {
      $(this).attr('aria-labelledby', $(this).attr('aria-controls') + '-clone');
      $(this).attr('id', $(this).attr('id') + '-clone');
    }
  });
  $(".table__clone--right *[aria-controls], .table__clone--right *[aria-labelledby]").each(function(){
    if(typeof $(this).attr('aria-controls') !== typeof undefined && $(this).attr('aria-controls') !== false) {
      $(this).attr('aria-controls', $(this).attr('aria-controls') + '-2');
      $(this).attr('id', $(this).attr('id') + '-2');
    }
    if(typeof $(this).attr('aria-labelledby') !== typeof undefined && $(this).attr('aria-labelledby') !== false) {
      $(this).attr('aria-labelledby', $(this).attr('aria-controls') + '-2');
      $(this).attr('id', $(this).attr('id') + '-2');
    }
  });    

})(jQuery);