$(function() {
  'use strict';

  var $body = $('body');

  $body.on('click', '.control__item__open', function(event) {
    event.preventDefault();
    var $button = $(this);
    var $control_item = $button.parents('.control__item');
    $control_item.addClass('loading');

    $.get($button.attr('data-href'), function(data) {
      var $control_item_content = $control_item.find('.control__item__content');
      var $control_item_content_main = $control_item_content.find('.control__item__content__main');

      var cleaned_data = data.replace(/autofocus/g, '');
      $control_item_content_main.html(cleaned_data);

      $control_item_content.height($control_item_content_main.outerHeight(true));

      $control_item.addClass('active');

      window.on_transitionend($control_item_content, function(event) {
        if (!event || event.target === this) {
          $control_item_content.off(window.transitionend);
          $control_item_content.removeAttr('style');
          $control_item.removeClass('loading');
        }
      });
    });
  });

  $body.on('click', '.control__item__close', function(event) {
    event.preventDefault();
    var $button = $(this);
    var $control_item = $button.parents('.control__item');

    var $control_item_content = $control_item.find('.control__item__content');
    var $control_item_content_main = $control_item_content.find('.control__item__content__main');
    $control_item_content.height($control_item_content_main.outerHeight(true));

    window.on_transitionend($control_item_content, function(event) {
      if (!event || event.target === this) {
        $control_item_content.off(window.transitionend);
        $control_item_content_main.html('');
      }
    });

    window.requestAnimationFrame(function() {
      $control_item.removeClass('active');
      $control_item_content.removeAttr('style');
    });
  });

  $body.on('submit', '.control__item form', function(event) {
    event.preventDefault();
    var $form = $(this);
    $form.addClass('loading');
    $.ajax({
      type: $form.attr('method'),
      url: $form.attr('action'),
      data: $form.serialize(),
      success: function(data) {
        $form.replaceWith(data);
      }
    });
  });

  var $task_form = $('.task__form');

  if ($task_form.hasClass('errors')) {
    window.location = '#form';
  }

  // $task_form.formset();
  $task_form.on('formAdded', function(event) {
    var $title = $(event.target).find('h3');
    var id = parseInt($title.attr('data-id').match(/\d+/)[0]);
    $title.html($title.html().replace('%(id)', id + 1));
  });

  $task_form.on('formDeleted', function(event) {
    $(event.target).hide();
  });

  /* DOWNLOADS */

  $('.downloads__frame').each(function() {
    var $frame = $(this);

    var download_texts = [];

    $frame.find('.download__item__title').each(function() {
      var text = $(this).text().toLowerCase();
      text = text + ' ' + $(this).next().text().toLowerCase();
      text = text + ' ' + $(this).next().next().text().toLowerCase();
      download_texts.push({
        $element: $(this).parents('li'),
        text: text
      });
    });

    $frame.on('input', '.downloads_search', function(event) {
      var query = $(this).val().toLowerCase();
      var query_list = $.trim(query).split(' ');
      var matches = [];

      for (var i = 0; i < download_texts.length; i++) {
        var download_text_item = download_texts[i];
        var matched = false;
        if (!matched) {
          for (var ii = 0; ii < query_list.length; ii++) {
            var query_item = query_list[ii];
            if (download_text_item.text.indexOf(query_item) >= 0) {
              matched = true;
              matches.push(download_text_item.$element);
            }
          }
        }
      }

      $frame.find('li').each(function() {
        $(this).parents('.downloads__section').css('display', 'none');
        $(this).css('display', 'none');
      });
      for (i = 0; i < matches.length; i++) {
        matches[i].removeAttr('style');
        matches[i].parents('.downloads__section').removeAttr('style');
      }
    });

    $frame.find('#downloads_search').trigger('input');
  });
});