/* bacha, login  by se mel otevirat pres .cart-signin, je tam neco extra */

/*
příklad použití:
{include "focus/focus.sizetable.tpl" class="sizetable" sizeTables=$sizeTables}

se automaticky napáruje na:

<a href="" data-focus-opener="sizetable">
   Jakou velikost?
</a>

 */

$('body').on('click', '[data-focus-opener]', function() {
   const openerName = $(this).data('focus-opener');
   const $focus = $(`.focus.${openerName}`);
   const $onDemandLink = $focus.find('a[data-ondemand]');
   const $onDemandContentLink = $focus.find('a[data-ondemand-content]');

   $focus.focus({
      addBodyPadding: true
   });

   $focus.data('last-focused', document.activeElement);
   $focus.focus('show');

   if ($onDemandLink && $onDemandLink.attr('href')) {
      const url = $onDemandLink.attr('href');

      $onDemandLink.remove();

      wpj.domUtils.reloadPartsFromUrl(url, $focus.find('[data-reload]'), undefined, () => {
         const $form = $(`[data-form="${openerName}"]`);
         if (!$form.hasClass('bv-form')) {
            $form
               .bootstrapValidator({
                  feedbackIcons: {
                     valid: 'fc fc-check',
                     invalid: 'fc fc-remove',
                     validating: 'fc fc-refresh'
                  }
               })
               .on('success.form.bv', function(e) {
                  e.preventDefault();

                  const $this = $(e.target);
                  const data = $this.serialize();
                  const url = $this.attr('action');

                  wpj.domUtils.reloadPartsFromUrl(url, $this.closest('[data-reload]'), data);
               });
         }
      });
   }

   if ($onDemandContentLink && $onDemandContentLink.attr('href')) {
      const url = $onDemandContentLink.attr('href');

      $onDemandContentLink.remove();

      wpj.domUtils.reloadPartsFromUrl(url, $focus.find('[data-reload]'), undefined, () => {});
   }

   return false;
});
