// window.addEventListener( "pageshow", function ( event ) {
//   var historyTraversal = event.persisted || ( typeof window.performance != "undefined" && window.performance.navigation.type === 2 );
//   if ( historyTraversal ) {
//     window.location.reload();
//   }
// });

function getURLVar(key) {
  var value = [];
  
  var query = String(document.location).split('?');
  
  if (query[1]) {
    var part = query[1].split('&');
    
    for (i = 0; i < part.length; i++) {
      var data = part[i].split('=');
      
      if (data[0] && data[1]) {
        value[data[0]] = data[1];
      }
    }
    
    if (value[key]) {
      return value[key];
    } else {
      return '';
    }
  }
}

$(document).ready(function () {


  // CLD Wishlist
	$('.add-to-wl').click(function() {
		var product_id = $(this).data('id');
		var product_size_id = $(this).data('size');
		$.ajax({
			url: 'index.php?route=account/wishlist/add',
			type: 'post',
			data: 'product_id=' + product_id,
			dataType: 'json',
			success: function(json) {
				$('.alert-dismissible').remove();
				if (json['redirect']) {
					location = json['redirect'];
				}
				if (json['success']) {
					$('.add-to-wl[data-id=\''+product_id+'\']').toggleClass('wl-add');
					$('.wl-prod-block .add-to-wl[data-id=\''+product_id+'\'] span').html(json['add_text']);
					
					if (json['total'] > 0) {
					$('.cld_wishlist span').html(json['total']);
					$('.cld_wishlist span').removeClass('hide');	
					} else {
					$('.cld_wishlist span').toggleClass('hide');	
					}
					
					//Mindbox					
					if (json['action_type'] == 'add') {
					mindbox("async", {
					  operation: "Website.AddToWishList",
					  data: {
					  addProductToList: {
						product: {
						  ids: {
							website: product_size_id
						  }
						},
						pricePerItem: json['prod_price']
					  }
					},
					  onSuccess: function() { },
					  onError: function(error) { }
					});
					} else {
					mindbox("async", {
					  operation: "Website.RemoveFromWishList",
					  data: {
					  removeProductFromList: {
						product: {
						  ids: {
							website: product_size_id
						  }
						},
						pricePerItem: json['prod_price']
					  }
					},
					  onSuccess: function() { },
					  onError: function(error) { }
					});	
					}
					
				}
				

			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	});
	$('.rem-wl').click(function() {
		var product_id = $(this).data('id');
		var product_size_id = $(this).data('size');
		$.ajax({
			url: 'index.php?route=account/wishlist/add',
			type: 'post',
			data: 'product_id=' + product_id,
			dataType: 'json',
			success: function(json) {
				$('.alert-dismissible').remove();
				if (json['redirect']) {
					location = json['prod_price'];
				}
				if (json['success']) {

				if (json['total'] > 0) {
				$('.cld_wishlist span').html(json['total']);
				$('.cld_wishlist span').removeClass('hide');	
				} else {
				$('.cld_wishlist span').toggleClass('hide');	
				}	

				//Mindbox
				mindbox("async", {
				  operation: "Website.RemoveFromWishList",
				  data: {
				  removeProductFromList: {
					product: {
					  ids: {
						website: product_size_id
					  }
					},
					pricePerItem: json['prod_price']
				  }
				},
				  onSuccess: function() { },
				  onError: function(error) { }
				});	

				location.reload();

				}

			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	});    
  
  
  let login_popup_hash = location.hash.match(/#login-popup/);
  
  if (login_popup_hash !== null) {
    open_login_popup();
  }
  
  // Highlight any found errors
  $('.text-danger').each(function () {
    var element = $(this).parent().parent();
    
    if (element.hasClass('form-group')) {
      element.addClass('has-error');
    }
  });
  
  /* Search */
  $('#top-menu .search-input input[name=\'search\']').parent().find('button').on('click', function () {
    var url = $('base').attr('href') + 'index.php?route=product/search';
    
    var value = $('#top-menu .search-input input[name=\'search\']').val();
    
    if (value) {
      url += '&search=' + encodeURIComponent(value);
    }
    
    location = url;
  });
  
  $('#top-menu .search-input input[name=\'search\']').on('keydown', function (e) {
    if (e.keyCode == 13) {
      $('#top-menu .search-input input[name=\'search\']').parent().find('button').trigger('click');
    }
  });
  
  // Checkout
  $(document).on('keydown', '#collapse-checkout-option input[name=\'email\'], #collapse-checkout-option input[name=\'password\']', function (e) {
    if (e.keyCode == 13) {
      $('#collapse-checkout-option #button-login').trigger('click');
    }
  });
  
  // tooltips on hover
  $('[data-toggle=\'tooltip\']').tooltip({container: 'body'});
  
  // Makes tooltips work on ajax generated content
  $(document).ajaxStop(function () {
    $('[data-toggle=\'tooltip\']').tooltip({container: 'body'});
  });
});



function ab_links(brand, test) {
	$.ajax({
		url: 'index.php?route=scripts/main/ab_links_insert',
		type: 'post',
		data: 'brand=' + brand + '&test=' + test,
		dataType: 'json',
		success: function(json) {
			if (json['success']) {

			}
		}
	});
} 




var multicart = {
  'add': function (product_id, quantity) {
    $.ajax({
      url: 'index.php?route=checkout/cart/add',
      type: 'post',
      data: 'product_id=' + product_id + '&quantity=' + (typeof (quantity) != 'undefined' ? quantity : 1),
      dataType: 'json',
      success: function (json) {
        $('.alert-dismissible, .text-danger').remove();
        
        if (json['redirect']) {
          location = json['redirect'];
        }
        
        if (json['success']) {
          setTimeout(function () {
            $('#top-menu #cart').html('<span id="cart-total">' + json['total'] + '</span>');
            $('#mobile-menu #cart').html('<span id="cart-total">' + json['total'] + '</span>');
          }, 100);
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  }
}





// Cart add remove functions
var cart = {
  'add': function (product_id, quantity) {
    $.ajax({
      url: 'index.php?route=checkout/cart/add',
      type: 'post',
      data: 'product_id=' + product_id + '&quantity=' + (typeof (quantity) != 'undefined' ? quantity : 1),
      dataType: 'json',
      success: function (json) {
        $('.alert-dismissible, .text-danger').remove();
        
        if (json['redirect']) {
          location = json['redirect'];
        }
        
        if (json['success']) {
          setTimeout(function () {
            $('#top-menu #cart').html('<span id="cart-total">' + json['total'] + '</span>');
            $('#mobile-menu #cart').html('<span id="cart-total">' + json['total'] + '</span>');
          }, 100);
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  },
  'update': function (key, quantity) {
    $.ajax({
      url: 'index.php?route=checkout/cart/edit',
      type: 'post',
      data: 'key=' + key + '&quantity=' + (typeof (quantity) != 'undefined' ? quantity : 1),
      dataType: 'json',
      success: function (json) {
        // Need to set timeout otherwise it wont update the total
        setTimeout(function () {
          $('#top-menu #cart').html('<span id="cart-total">' + json['total'] + '</span>');
          $('#mobile-menu #cart').html('<span id="cart-total">' + json['total'] + '</span>');
        }, 100);
        
        if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
          location = 'index.php?route=checkout/cart';
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  },
  'remove': function (key) {
    $.ajax({
      url: 'index.php?route=checkout/cart/remove',
      type: 'post',
      data: 'key=' + key,
      dataType: 'json',
      success: function (json) {
        // Need to set timeout otherwise it wont update the total
        setTimeout(function () {
          $('#top-menu #cart').html('<span id="cart-total">' + json['total'] + '</span>');
          $('#mobile-menu #cart').html('<span id="cart-total">' + json['total'] + '</span>');
        }, 100);
        
        if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
          location = 'index.php?route=checkout/cart';
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  }
}

var wishlist = {
  'add': function (product_id) {
    $.ajax({
      url: 'index.php?route=account/wishlist/add',
      type: 'post',
      data: 'product_id=' + product_id,
      dataType: 'json',
      success: function (json) {
        $('.alert-dismissible').remove();
        
        if (json['redirect']) {
          open_login_popup(1);
        } else {
          if (json['success']) {
            $('div[data-product-id="' + product_id + '"]').find('.line button').html('<img src="/image/icons/in-wishlist.svg" alt="В избранном">');
            $('#button-wishlist').html('<span>В избранном</span><img src="/image/icons/in-wishlist.svg" alt="В избранном">');
          }
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  },
  'remove': function (product_id) {
    $.ajax({
      url: 'index.php?route=account/wishlist/remove',
      type: 'post',
      data: 'product_id=' + product_id,
      dataType: 'json',
      success: function (json) {
        $('.alert-dismissible').remove();
        
        if (json['redirect']) {
          open_login_popup(1);
        } else {
          if (json['success']) {
            $('div[data-product-id="' + product_id + '"]').find('.line button').html('<img src="/image/icons/to-wishlist.svg" alt="В избранное">');
            $('#button-wishlist').html('<span>Добавить в избранное</span><img src="/image/icons/to-wishlist.svg" alt="Добавить в избранное">');
            setTimeout(function () {
              $('div[data-product-id="' + product_id + '"]').remove();
            }, 500)
            
            setTimeout(function () {
              if (!$('#account-wishlist .products-grid .item').length) {
                $('#account-wishlist .inner').append('<div class="empty-prodcuts">У вас нет еще избранных товаров</div>');
              }
            }, 500)
          }
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  }
}
















/*var lastScrollTop = 0;
$(window).scroll(function (event) {
  if (!$('#top-category, .pulka_landing, .pulka_split').length) {
    
    var st = $(this).scrollTop();
    
    if (st > lastScrollTop) {
      if (!$('html.menu-open').length) {
        if ($(window).width() > 1024) {
          $('#top-menu.open').css('transform', 'translateY(-60px)');
          $('#top-menu-placeholder').addClass('active');
          $('#top-menu-placeholder').removeClass('active');
          $('#top-menu').removeClass('open');
          $('#top-menu').removeAttr('style');
        }
        
        if ($(window).width() < 1024) {
          $('#mobile-menu.open').css('transform', 'translateY(-60px)');
          $('#mobile-menu-placeholder').addClass('active');
          $('#mobile-menu-placeholder').removeClass('active');
          $('#mobile-menu').removeClass('open');
          $('#mobile-menu').removeAttr('style');
        }
      }
    } else {
      if (!$('html.menu-open').length) {
        if ($(window).width() > 1024) {
          if ($(window).scrollTop() > 12) {
            $('#top-menu-placeholder').addClass('active');
            $('#top-menu').addClass('open');
          } else {
            $('#top-menu-placeholder').removeClass('active');
            $('#top-menu').removeClass('open');
          }
        }
        
        if ($(window).width() < 1024) {
          if ($(window).scrollTop() > 12) {
            $('#mobile-menu-placeholder').addClass('active');
            $('#mobile-menu').addClass('open');
          } else {
            $('#mobile-menu-placeholder').removeClass('active');
            $('#mobile-menu').removeClass('open');
          }
        }
      }
    }
    
    lastScrollTop = st;
  }
});*/


$(window).resize(function () {
if (!$('.pulka_landing, .pulka_split').length) {	
  if (!$('html.menu-open').length) {
    if (!$('#top-category').length) {
      if ($(window).width() < 1024) {
        if ($(window).scrollTop() > 0) {
          $('#top-menu').addClass('open');
        } else {
          $('#top-menu').removeClass('open');
        }
      }
      
      if ($(window).width() < 1024) {
        if ($(window).scrollTop() > 0) {
          $('#mobile-menu').addClass('open');
        } else {
          $('#mobile-menu').removeClass('open');
        }
      }
    }
  }
}  
});











$(document).ready(function () {
	//if ($('.pulka_split').length) {
		var cld_top_point = $('#top-menu').offset().top+12;
		var cld_top_mobile_point = $('#mobile-menu').offset().top-1;
		$(window).scroll(function (event) {

			if ($(window).width() > 1024) {
			  if ($(window).scrollTop() > cld_top_point) {
				$('#top-menu-placeholder').addClass('active');
				$('#top-menu').addClass('open');
			  } else {
				$('#top-menu-placeholder').removeClass('active');
				$('#top-menu').removeClass('open');
			  }
			}
			
			if ($(window).width() < 1024) {
			  if ($(window).scrollTop() > cld_top_mobile_point) {
				$('#mobile-menu-placeholder').addClass('active');
				$('#mobile-menu').addClass('open');
				$('#mobile-menu-brick').addClass('active');
			  } else {
				$('#mobile-menu-placeholder').removeClass('active');
				$('#mobile-menu').removeClass('open');
				$('#mobile-menu-brick').removeClass('active');
			  }
			}
				
		});
	//}
});




$(function(){
	
	$('#scroll_top').hide();
	
	$(window).scroll(function(){
		if($(window).scrollTop() > 400) {
			$('#scroll_top').show();
		} else {
			$('#scroll_top').hide();
		}
	});
 
	$('#scroll_top').click(function(){
		$('html, body').animate({scrollTop: 0}, 600);
		return false;
	});
});








$(function () {
  $('#top-menu .inner .left a.header_left_link').on('mouseover', function () {
    $('.drop-menu .container .inner > div').css('display', 'none');
    $('#top-menu .inner .left a').removeClass('open');
    
    $(this).addClass('open');
    
    var growDiv = document.querySelector('.drop-menu');
    var data_menu_id = $(this).data('menu-id');
    
    $('.drop-menu .container .inner > div.block-' + data_menu_id).css('display', 'flex');
    
    var wrapper = document.querySelector('.drop-menu .container .inner');
    growDiv.style.height = wrapper.clientHeight + "px";
    
    //$('.drop-menu .container .inner > div.block-' + data_menu_id + ' .section.second').css('height', $('.drop-menu .container .inner').height()).fadeIn();


  });
 
  /*$('#top-menu .inner .left a:not(\'.header_left_link\')').on('mouseover', function () {
    var growDiv = document.querySelector('.drop-menu');
    growDiv.style.height = 0;
    $('.drop-menu .container .inner > div .section.second').css('height', 0).hide();
    
    $('#top-menu > .container .inner .left a,#top-menu > .container .inner .right a').removeClass('open');
    

  });*/
  
  $('#top-menu .inner .right a,#top-menu .inner .left a:not(\'.header_left_link\')').on('mouseover', function () {
    var growDiv = document.querySelector('.drop-menu');
    growDiv.style.height = 0;
    $('.drop-menu .container .inner > div .section.second').css('height', 0).hide();
    
    $('#top-menu > .container .inner .left a,#top-menu > .container .inner .right a').removeClass('open');
    
    jQuery.fn.reverse = [].reverse;
    $('.drop-menu .container .inner .item').reverse().each(function (i) {
      var delay_ms = ($(this).find('ul li, div').length > 10) ? 20 : 100;
      $(this).find('ul li, div').reverse().each(function (i) {
        $(this).delay(delay_ms * i).animate({opacity: 0}, 50);
      });
    });
  });
  
  $('#top-menu').on('mouseleave', function () {
    var growDiv = document.querySelector('.drop-menu');
    growDiv.style.height = 0;
    $('.drop-menu .container .inner > div .section.second').css('height', 0).hide();
    
    $('#top-menu > .container .inner .left a,#top-menu > .container .inner .right a').removeClass('open');
    
    jQuery.fn.reverse = [].reverse;
    $('.drop-menu .container .inner .item').reverse().each(function (i) {
      var delay_ms = ($(this).find('ul li, div').length > 10) ? 20 : 100;
      $(this).find('ul li, div').reverse().each(function (i) {
        $(this).delay(delay_ms * i).animate({opacity: 0}, 50);
      });
    });
  });
  
  $('.ssp .products-grid .item,.ssp .products .item').on('mouseover', function (e) {
	if (!$('#account-wishlist').length) {
    var product_id = $(this).data('product-id'),
      el = this;
    
    if ($(window).width() > 1024) {
      if (!$(el).hasClass('processed')) {
        $.ajax({
          type: 'post',
          url: 'index.php?route=product/product/get_product_option_size',
          data: {product_id: product_id},
          dataType: 'json',
          beforeSend: function () {
            $(el).addClass('processed');
          },
          success: function (json) {
            $(el).find('.line.options').addClass('active');
            if (json['options']) {
              $(el).find('.line.options.active').html('<div class="head-options">Доступные размеры</div><div class="list-options">' + json['options'] + '</div>');
            } else {
              $(el).find('.line.options').html('<div class="head-options">Доступные размеры</div><div class="list-options empty">нет размеров</div>');
            }
          },
          error: function (xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
          }
        });
      }				  
    }
	}
  });
  
  
   /*$('.products .item,.products-grid .item').on('mouseleave', function () {
    if ($(this).find('video').length) {
      $(this).find('video').remove();
    }	
	$(this).removeClass('processed2');
  }); */
  
  
  
  $('.products .item .inner .image > a,.products-grid .item .inner .image > a').on('mouseenter mouseleave', function () {
    if ($(this).find('img.transparent').length) {
      $(this).find('img').toggleClass('transparent');
    }
  });
  
  
  
  $('#cld_home_recommended .tabs-items button').on('click', function () {
    var type = $(this).data('target');
    
    if (type == '1') {
      $('#cld_home_recommended').find('.tabs-items button[data-target="2"]').removeClass('active');
	  $('#cld_home_recommended').find('.tabs-items button[data-target="3"]').removeClass('active');
      $('#cld_home_recommended').find('.tabs-items button[data-target="1"]').addClass('active');
      
      $('#cld_home_recommended').find('#pr-2').removeClass('active');
	  $('#cld_home_recommended').find('#pr-3').removeClass('active');
      $('#cld_home_recommended').find('#pr-1').addClass('active');
    }
    
    if (type == '2') {
      $('#cld_home_recommended').find('.tabs-items button[data-target="2"]').addClass('active');
      $('#cld_home_recommended').find('.tabs-items button[data-target="1"]').removeClass('active');
	  $('#cld_home_recommended').find('.tabs-items button[data-target="3"]').removeClass('active');
      
      $('#cld_home_recommended').find('#pr-2').addClass('active');
      $('#cld_home_recommended').find('#pr-1').removeClass('active');
	  $('#cld_home_recommended').find('#pr-3').removeClass('active');
    }
	
    if (type == '3') {
	  $('#cld_home_recommended').find('.tabs-items button[data-target="3"]').addClass('active');	
      $('#cld_home_recommended').find('.tabs-items button[data-target="2"]').removeClass('active');
      $('#cld_home_recommended').find('.tabs-items button[data-target="1"]').removeClass('active');
      
	  $('#cld_home_recommended').find('#pr-3').addClass('active');
      $('#cld_home_recommended').find('#pr-2').removeClass('active');
      $('#cld_home_recommended').find('#pr-1').removeClass('active');
    }	
  });  
  
  
  
  
  $('#popular-block .tabs-items button').on('click', function () {
    var type = $(this).data('target');
    
    if (type == 'girl') {
      $('#popular-block').find('.tabs-items button[data-target="boy"]').removeClass('active');
      $('#popular-block').find('.tabs-items button[data-target="girl"]').addClass('active');
      
      $('#popular-block').find('#products-boy').removeClass('active');
      $('#popular-block').find('#products-girl').addClass('active');
    }
    
    if (type == 'boy') {
      $('#popular-block').find('.tabs-items button[data-target="boy"]').addClass('active');
      $('#popular-block').find('.tabs-items button[data-target="girl"]').removeClass('active');
      
      $('#popular-block').find('#products-boy').addClass('active');
      $('#popular-block').find('#products-girl').removeClass('active');
    }
  });
  
  $('#mobile-sidebar .main-level-has-children a.main-item-link').on('click', function (e) {
    e.preventDefault();
    $(this).parent().addClass('expanded');
    $('#mobile-sidebar .main-level').addClass('children-expanded');
    $(this).parent().find('.main-level-children').show();
    $('#mobile-sidebar button.go-back').css('display', 'flex');
  });
  
  $('#mobile-sidebar button.go-back').on('click', function () {
    //$('#mobile-sidebar button.go-back').hide();
    $('#mobile-sidebar .main-level').find('.expanded').removeClass('expanded');
    $('#mobile-sidebar .main-level').removeClass('children-expanded');
    setTimeout(function () {
      $('#mobile-sidebar .main-level-children').hide();
    }, 300)
  });
});

function load_js(src, src_type, callback) {
  var element = ((document.getElementById(src_type + '-js')) ? true : false);
  
  if (!element) {
    var s = document.createElement('script');
    
    s.src = src;
    s.id = src_type + '-js';
    s.onreadystatechange = s.onload = function () {
      var state = s.readyState;
      if (!callback.done && (!state || /loaded|complete/.test(state))) {
        callback.done = true;
        callback();
      }
    };
    
    document.getElementsByTagName('head')[0].appendChild(s);
  } else {
    callback.done = true;
    callback();
  }
}

function load_css(src, src_type, callback) {
  var element = ((document.getElementById(src_type + '-css')) ? true : false);
  
  if (!element) {
    var s = document.createElement('link');
    
    s.rel = 'stylesheet';
    s.type = 'text/css';
    s.href = src;
    s.id = src_type + '-css';
    s.onreadystatechange = s.onload = function () {
      var state = s.readyState;
      if (!callback.done && (!state || /loaded|complete/.test(state))) {
        callback.done = true;
        callback();
      }
    };
    
    document.getElementsByTagName('head')[0].appendChild(s);
  } else {
    callback.done = true;
    callback();
  }
}

function autoHeightAnimate(element, time) {
  let curHeight = element.height(),
    autoHeight = element.css('height', 'auto').height();
  
  element.height(curHeight);
  element.stop().animate({height: autoHeight}, time);
}

function open_subscribe_popup() {
  setTimeout(function () {
    load_css('/catalog/view/javascript/jquery/magnific/magnific-popup.css?d=' + Math.random(), 'magnific', function () {
      load_js('/catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js', 'magnific', function () {
        $.magnificPopup.open({
          tLoading: '<div class="hollow-dots-spinner"><div class="dot"></div><div class="dot"></div><div class="dot"></div></div>',
          items: {src: 'index.php?route=account/subscribe/popup'},
          type: 'ajax',
          closeOnContentClick: 0,
          closeOnBgClick: 0,
          closeBtnInside: 0,
          enableEscapeKey: 1,
          alignTop: 0,
          showCloseBtn: 0,
          fixedContentPos: 1,
          fixedBgPos: 1,
          removalDelay: 300,
          mainClass: 'mfp-move-from-bottom-to-center',
          callbacks: {
            beforeOpen: function () {
              this.wrap.removeAttr('tabindex');
              $('.mfp-container').removeClass('mfp-ajax-holder').addClass('mfp-iframe-holder');
            },
            open: function () {
              $('body').css('overflow', 'hidden');
              $('.mfp-content').addClass('mfp-with-anim');
            },
            close: function () {
              $('body').css('overflow', 'auto');
            },
          }
        });
      });
    });
  }, 1);
}

function open_register_popup() {
  setTimeout(function () {
    load_css('/catalog/view/javascript/jquery/magnific/magnific-popup.css?d=' + Math.random(), 'magnific', function () {
      load_js('/catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js', 'magnific', function () {
        $.magnificPopup.open({
          tLoading: '<div class="hollow-dots-spinner"><div class="dot"></div><div class="dot"></div><div class="dot"></div></div>',
          items: {src: 'index.php?route=account/register/popup'},
          type: 'ajax',
          closeOnContentClick: 0,
          closeOnBgClick: 0,
          closeBtnInside: 0,
          enableEscapeKey: 1,
          alignTop: 0,
          showCloseBtn: 0,
          fixedContentPos: 1,
          fixedBgPos: 1,
          removalDelay: 300,
          mainClass: 'mfp-move-from-bottom-to-center',
          callbacks: {
            beforeOpen: function () {
              this.wrap.removeAttr('tabindex');
              $('.mfp-container').removeClass('mfp-ajax-holder').addClass('mfp-iframe-holder');
            },
            open: function () {
              $('body').css('overflow', 'hidden');
              $('.mfp-content').addClass('mfp-with-anim');
              
            },
            close: function () {
              $('body').css('overflow', 'auto');
            },
          }
        });
      });
    });
  }, 1);
}

function open_forgotten_popup() {
  setTimeout(function () {
    load_css('/catalog/view/javascript/jquery/magnific/magnific-popup.css?d=' + Math.random(), 'magnific', function () {
      load_js('/catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js', 'magnific', function () {
        $.magnificPopup.open({
          tLoading: '<div class="hollow-dots-spinner"><div class="dot"></div><div class="dot"></div><div class="dot"></div></div>',
          items: {src: 'index.php?route=account/forgotten/popup'},
          type: 'ajax',
          closeOnContentClick: 0,
          closeOnBgClick: 0,
          closeBtnInside: 0,
          enableEscapeKey: 1,
          alignTop: 0,
          showCloseBtn: 0,
          fixedContentPos: 1,
          fixedBgPos: 1,
          removalDelay: 300,
          mainClass: 'mfp-move-from-bottom-to-center',
          callbacks: {
            beforeOpen: function () {
              this.wrap.removeAttr('tabindex');
              $('.mfp-container').removeClass('mfp-ajax-holder').addClass('mfp-iframe-holder');
            },
            open: function () {
              $('body').css('overflow', 'hidden');
              $('.mfp-content').addClass('mfp-with-anim');
              
            },
            close: function () {
              $('body').css('overflow', 'auto');
            },
          }
        });
      });
    });
  }, 1);
}

function open_login_popup(reload = '') {
  var url = (reload) ? 'index.php?route=account/login/popup&reload=1' : 'index.php?route=account/login/popup';
  setTimeout(function () {
    load_css('/catalog/view/javascript/jquery/magnific/magnific-popup.css?d=' + Math.random(), 'magnific', function () {
      load_js('/catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js', 'magnific', function () {
        $.magnificPopup.open({
          tLoading: '<div class="hollow-dots-spinner"><div class="dot"></div><div class="dot"></div><div class="dot"></div></div>',
          items: {src: url},
          type: 'ajax',
          closeOnContentClick: 0,
          closeOnBgClick: 0,
          closeBtnInside: 0,
          enableEscapeKey: 1,
          alignTop: 0,
          showCloseBtn: 0,
          fixedContentPos: 1,
          fixedBgPos: 1,
          removalDelay: 300,
          mainClass: 'mfp-move-from-bottom-to-center',
          callbacks: {
            beforeOpen: function () {
              this.wrap.removeAttr('tabindex');
              $('.mfp-container').removeClass('mfp-ajax-holder').addClass('mfp-iframe-holder');
            },
            open: function () {
              $('body').css('overflow', 'hidden');
              $('.mfp-content').addClass('mfp-with-anim');
              
            },
            close: function () {
              $('body').css('overflow', 'auto');
            },
          }
        });
      });
    });
  }, 1);
}

function open_information_popup(information_id) {
  setTimeout(function () {
    load_css('/catalog/view/javascript/jquery/magnific/magnific-popup.css?d=' + Math.random(), 'magnific', function () {
      load_js('/catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js', 'magnific', function () {
        $.magnificPopup.open({
          tLoading: '<div class="hollow-dots-spinner"><div class="dot"></div><div class="dot"></div><div class="dot"></div></div>',
          type: 'ajax',
          ajax: {
            settings: {
              type: 'post',
              data: {information_id: information_id}
            }
          },
          items: {src: 'index.php?route=information/information/popup'},
          closeOnContentClick: 0,
          closeOnBgClick: 0,
          closeBtnInside: 0,
          enableEscapeKey: 1,
          alignTop: 0,
          showCloseBtn: 0,
          fixedContentPos: 1,
          fixedBgPos: 1,
          removalDelay: 300,
          mainClass: 'mfp-move-from-bottom',
          callbacks: {
            beforeOpen: function () {
              this.wrap.removeAttr('tabindex');
              $('.mfp-container').removeClass('mfp-ajax-holder').addClass('mfp-iframe-holder');
            },
            open: function () {
              $('body').css('overflow', 'hidden');
              $('.mfp-content').addClass('mfp-with-anim');
              
            },
            close: function () {
              $('body').css('overflow', 'auto');
            }
          }
        });
      });
    });
  }, 1);
}

function menu(action) {
  if (action == 'open') {
    $('.main-block').find('.children-expanded').removeClass('children-expanded');
    $('#mobile-sidebar, #mobile-sidebar-bg').addClass('open');
    $('html,body').addClass('menu-open');
    $('#mobile-sidebar').attr('tabindex', '-1');
  }
  
  if (action == 'close') {
    $('#mobile-sidebar, #mobile-sidebar-bg').removeClass('open');
    $('#mobile-sidebar').removeAttr('tabindex');
    $('html,body').removeClass('menu-open');
  }
  
  
    setTimeout(function () {
      //$('#mobile-sidebar button.go-back').hide();
      $('#mobile-sidebar .main-level').find('.expanded').removeClass('expanded');
      $('#mobile-sidebar .main-level').removeClass('children-expanded');
      $('#mobile-sidebar .main-level-children').hide();
    },200)
  
  
}

$(document).on('click', function (e) {
  if ($(e.target).closest('#mobile-sidebar').length === 0) {
    if ($('#mobile-sidebar').hasClass('open')) {
      document.onkeydown = function (evt) {
        evt = evt || window.event;
        if (evt.keyCode == 27) {
          $('#mobile-sidebar, #mobile-sidebar-bg').removeClass('open');
          $('#mobile-sidebar').removeAttr('tabindex');
          $('html,body').removeClass('menu-open');
        }
      }
    }
  }
});

$(document).on('click', '#mobile-sidebar-bg', function (e) {
  if ($('#mobile-sidebar-bg.open').length) {
    $('#mobile-sidebar, #mobile-sidebar-bg').removeClass('open');
    $('#mobile-sidebar').removeAttr('tabindex');
    $('html,body').removeClass('menu-open');
  }
});


$(document).mouseup(function (e) {
  var container = $(".custom-options");
  
  if (!container.is(e.target) && container.has(e.target).length === 0) {
    $('.custom-select').removeClass('open');
  }
});

