var win_height = $(window).height();
var win_width = $(window).width();

$(document).ready(function()
{
  var header_height = $('header').outerHeight();
  var $title_elem = $('.site-title-header');
  if ($title_elem.length == 1)
  {
  var title_pos = $title_elem.position().top;
  var title_height = $title_elem.outerHeight();
  var title_pos_max = header_height - title_height - 150;
  }
  var scroll_pos = 0;
  var diff_h = 0;
  var scroll_start = title_pos*.8;

  //width 767
  if (win_width <= 767)
  {
    $('.mobile-prepend').prepend($('.top-menu-wrap'));
    $('body').append('<aside id="mobile-menu-wrap" class="hide"></aside>');
    $('#mobile-menu-wrap').append($('.menu-top-wrap:eq(0) nav'));
    $('#mobile-menu-wrap').append($('.menu-top-wrap:eq(1) nav ul.menu-top'));
    $('.menu-top:gt(0)').children().appendTo('.menu-top:eq(0)');

    $('#mobile-menu-wrap li a.has-submenu').parent().append('<a href="#" class="show-submenu"><i class="fas fa-chevron-down"></i></a>');
    $('.show-submenu').on('click', function(e)
    {
      e.preventDefault();
      $(this).toggleClass('active');
      $(this).prev().slideToggle();
    });
  }
  //koniec width 767
  

  if ($('a[href^="#"]').length > 0)
  {
    $('a[href^=\\#]').each(function()
    {
      $(this).on('click', function(e)
      {
        e.preventDefault();
        var anchor_dest = $(this).attr('href').replace('#', '');
        if (anchor_dest != '') {
          if ($('#'+anchor_dest).length == 1) {
            $('html,body').animate({scrollTop: $('#'+anchor_dest).offset().top - 100});
          }
        }
      });
    });
  }

  //zakladki przy terminach ofert
  if ($('#tabs-term-line').length == 1)
  {
    $('#tabs-term-line a:first').addClass('active');

  	$('#tabs-term-line ul a').click(function(e)
  	{
  		e.preventDefault();
      let actual_month = $(this).attr('data-tab');
  	  $('#tabs-term-line a').removeClass('active');
  		$(this).addClass('active');
  		$('.offer-terms-tab tr[data-tab-month]').hide();
  		if ($(this).is('[data-tab-special]'))
  		{
  			$('.offer-terms-tab tr[data-tab-month='+actual_month+'][data-tab-special='+$(this).attr("data-tab-special")+']').fadeIn();
  		}
  		else
  		{
  			$('.offer-terms-tab tr[data-tab-month='+actual_month+']').removeClass('hide').fadeIn();
  		}
  	});
  }

  $('.offers-search-show-more-btn').on('click', function(e)
  {
    e.preventDefault();
    var $parent_ = $(this).parents('.offers-search-box-container');
    var offer_box = '.offer-search-box';
    var to_show = $(this).attr('data-to-show');
    var $offers_to_show = $parent_.find(offer_box+':hidden');
    var count_all = $offers_to_show.length;
    var max_to_show = to_show <= count_all ? to_show : count_all;

    if (win_width <= 767) {
      var $last_v_box = $parent_.find(offer_box+':visible:last');
      $('html,body').animate({scrollTop: $last_v_box.offset().top + $last_v_box.height()});
    }
    $parent_.find(offer_box+':hidden:lt('+max_to_show+')').show();
    if (count_all < to_show) {
      $(this).hide();
    }

  });

  $('#mobile-menu-trigger').click(function(e)
  {
  	e.preventDefault();
    let $aside_menu = $('#mobile-menu-wrap');
    if ($aside_menu.is(':hidden'))
    {
      $aside_menu.show().animate({'left':0});
    }
    else {
      $aside_menu.animate({'left':'-100%'}, function()
      {
        $aside_menu.hide();
      });
    }
  	$(this).parent().toggleClass('active');
  	$('#svg-icon').toggleClass('active');
  });

/*
$('#mobile-menu-wrap ul a').click(function(e) {
	e.preventDefault();
	//var target_ = $(this).attr('href');
	$('html,body').animate({scrollTop: $(target_).offset().top-60}, function() {
		if ($('#mobile-menu-trigger').is(':visible')) {
		$('#mobile-menu-trigger').parent().removeClass('active').next().slideUp(); //onclick="this.classList.toggle('active')"
		$('#svg-icon').removeClass('active');
		}
	});
});
*/

});
