'use strict';

$(function () {

    var options = {
        url: "/ajax/ajax_locations.php",
        getValue: "name",
        list: { maxNumberOfElements: 8, match: { enabled: true } },
        theme: "square"
    };
    $(".select_cities").easyAutocomplete(options);
    
  var $num = $('.b_nav ._item').length,
      $ItemWidth = 100/$num;
  
  $('.b_nav ._item').css('width', $ItemWidth+'%');
  
  console.log($num);
  
	$('.b_basket_contents').stick_in_parent({
		'offset_top': 20
	});

	$('.close_popup').click(function (e) {
		e.preventDefault();
		$.magnificPopup.close();
	});

	$('.b_filters ._filters_view a').click(function (e) {
		e.preventDefault();
		$(this).addClass('-active').siblings().removeClass('-active');
		$($(this).attr('href')).show().siblings().hide();
	});

	$('.b_main_slider ._slider').owlCarousel({
		items: 1,
		smartSpeed: 1000,
		autoplay: true,
		dots: true,
		loop: true
	});

	$('.b_partners ._slider').owlCarousel({
		smartSpeed: 1000,
		autoplay: true,
		loop: true,
		margin: 20,
		nav: true,
		responsive: {
			768: {
				items: 8
			}
		}
	});

	$('.b_promo_item ._slider, .b_slider.-products').owlCarousel({
		smartSpeed: 1000,
		// autoplay: true,
		margin: 20,
		nav: true,
		responsive: {
			1000: {
				items: 4
			},
			768: {
				items: 3
			},
			0: {
				items: 1
			}
		},
		onInitialized: function onInitialized() {
			$.fn.matchHeight._update();
		}
	});

	$('.b_product_card .b_slider').owlCarousel({
		smartSpeed: 300,
		margin: 20,
		nav: true,
		responsive: {
			1200: {
				items: 4
			},
			768: {
				items: 3
			},
			0: {
				items: 3
			}
		},
		onInitialized: function onInitialized() {
			$.fn.matchHeight._update();
		}
	});

	$('.b_slider ._remove_button').click(function () {
		var $this = $(this),
		    $slider = $this.closest('.owl-carousel'),
		    $slide = $this.closest('.owl-item');
		$slider.trigger('remove.owl.carousel', $slide.index());
	});

	$('.b_seo ._more').click(function (e) {
		e.preventDefault();
		$('.b_seo ._text').toggleClass('-hidden');
		$(this).find('span').toggle();
		$(this).toggleClass('btn_2');
		$(this).toggleClass('btn_1');
	});

	if( $('.b_seo ._text').height() > $('.b_seo ._text_inner').height() ){
		$('.b_seo ._text').removeClass('-hidden');
		$('.b_seo ._more').hide();
	}

	$('.b_brands_list ._more').click(function () {
		var $this = $(this);
		$this.closest('._item').find('._description_container').toggleClass('-folded');
		$this.find('span').toggle();
	});

	$('.smoothscroll').click(function () {
		var target = $(this.hash);
		target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
		if (target.length) {
			$('html, body').animate({
				scrollTop: target.offset().top
			}, 1000);
			return false;
		}
	});

	$('input[type=tel]').mask('+7 (999) 999-99-99');


	var flag = false;
	$('.b_menu_toggler').bind('touchstart click', function(){
		if (!flag) {
			flag = true;
			setTimeout(function(){ flag = false; }, 100);
			// do something
			$('.b_menu_toggler').toggleClass('menu-is-active');
			$('.b_mobile_nav').fadeToggle();
		}

		return false
	});


	/*$('.b_menu_toggler').on('touchstart, click', function (e) {
		e.preventDefault();
		$('.b_menu_toggler').toggleClass('menu-is-active');
		$('.b_mobile_nav').fadeToggle();
	});*/

	$('.b_header ._search').validate();
	// $('.b_footer_subs ._form').validate({
	// 	submitHandler: function submitHandler() {
	// 		$.magnificPopup.open({
	// 			items: {
	// 				src: '#thx_subscribed_popup'
	// 			},
	// 			type: 'inline'
	// 		});
	// 	}
	// });
	$('.b_enter_popup ._form').validate({
		invalidHandler: function (event, validator) {
			$('#win8_wrapper').hide();
		}
	});
	$('.b_reg_popup ._form').validate({
		invalidHandler: function (event, validator) {
			$('#win8_wrapper').hide();
		}
	});
	$('#forgot_popup ._form').validate({
		invalidHandler: function (event, validator) {
		$('#win8_wrapper').hide();
	}});

	$('.b_callback_popup ._form').validate({
		invalidHandler: function (event, validator) {
			$('#win8_wrapper').hide();
		},
		submitHandler: function submitHandler() {
			$('#callback_form').submit();
		}
	});

	$('.b_for_partners ._form').validate({
		invalidHandler: function (event, validator) {
			$('#win8_wrapper').hide();
		},
		submitHandler: function submitHandler() {
			$.magnificPopup.open({
				items: {
					src: '#thx_popup'
				},
				type: 'inline'
			});
		}
	});

	$(document).on('click', '#reg_popup_btn', function (e) {
		e.preventDefault();
		$.magnificPopup.open({
			items: {
				src: '#reg_popup'
			},
			type: 'inline',
			callbacks: {
				open: function() {

				},
				close: function() {
					if (typeof $('#captchaSid')!== typeof undefined) {
						if ($('#captchaSid').length <= 0) {
							document.location.reload();
						}
					}

				}
			}
		});
	});

	$('.b_hor_form ._form').validate({
		invalidHandler: function (event, validator) {
			$('#win8_wrapper').hide();
		}
	});

	$('div._vacancy form._form').each(function () {
		$(this).validate({
			invalidHandler: function (event, validator) {
				$('#win8_wrapper').hide();
			}
		})
	});

	$('.popup').magnificPopup({
		type: 'inline',
		mainClass: 'mfp-no-margins mfp-with-zoom',
		zoom: {
			enabled: true,
			duration: 300
		}
	});

	/*$('body').magnificPopup({
		delegate: '.img_popup',
		type: 'image',
		mainClass: 'mfp-no-margins mfp-with-zoom',
		zoom: {
			enabled: true,
			duration: 300
		}
	});*/


	// Тип Image - галерея картинок
	$('._image_wrapper').magnificPopup({
		delegate: 'a',
		type: 'image',
		tLoading: 'Загрузка изображения #%curr%...',
		gallery: {
			enabled: true,
			navigateByImgClick: true,
			preload: [0, 1] // Will preload 0 - before current, and 1 after the current image
		}
	});

	$('.iframe_popup').magnificPopup({
		mainClass: 'mfp-no-margins mfp-with-zoom',
		type: 'iframe',
		zoom: {
			enabled: true,
			duration: 300
		}
	});

	$(".only_numbers").keydown(function (e) {
		if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 || e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true) || e.keyCode >= 35 && e.keyCode <= 40) {
			return;
		}

		if ((e.shiftKey || e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105)) {
			e.preventDefault();
			return;
		}
	});
	console.log($('.b_orders_list ._switcher'));
	$('.b_tabs ._tab, .b_contacts ._tab, .b_orders_list ._switcher').click(function (e) {
		e.preventDefault();
		var target = $(this).attr('href');
		$(target).show().siblings().hide();
		$(this).addClass('-active').siblings().removeClass('-active');
	});

	$('.b_nav ._item.-with_dropdown').hover(function () {
		$('.b_nav').addClass('-highlighted');
	}, function () {
		$('.b_nav').removeClass('-highlighted');
	});

	$('.b_main_side_nav ._nav_item_link').click(function (e) {
		// e.preventDefault();
		if(!$(e.target).is('a')) {
			$(this).closest('._nav_item').toggleClass('-opened').siblings().removeClass('-opened');
		}
	});

	$('.b_main_side_nav ._nav_sub_item').click(function (e) {
		if (!$(e.target).is('a') && e.target.className !== '_nav_sub_sub_sub_item_name') {
			$(this).toggleClass('-opened').siblings().removeClass('-opened');
		}
	});

	$('.b_main_side_nav ._nav_sub_item ._nav_sub_sub_item').click(function (e) {
		if(!$(e.target).is('a')){
			$(this).toggleClass('-opened').siblings().removeClass('-opened');
		}
	});

	$('.b_basket ._amount_select, .b_reg_popup ._input.-region, .b_profile_form ._region_select').selectric();

	var $spinnerInput = $('.b_spinner ._spinner_input');
	$spinnerInput.length && $spinnerInput.numeric().spinner({ min: 1 });

	$('.number_input').numeric();

	$('.b_basket ._remove').click(function () {
		var id = $(this).data("id");
		$.ajax({
			type: "POST",
			url: '/ajax/ajax_delete_basket_item.php',
			data: {id: id},
			dataType: "html",
			beforeSend: function() {
				$('#win8_wrapper').show();
			},
			success: function () {

				$("#basket_item_id_"+id).remove();
				reloadBasketLine();
			},
			complete: function(jqXHR, textStatus) {
				var count = 0;
				$('#basket_items_list div._tr').each(function () {
					count++;
				});

				if (count == 0){
					location.reload();
				} else {
					$('#win8_wrapper').hide();
				}

			}
		});


	});

	$('.b_basket ._select_mode').change(function () {
		$('.b_basket ._table').toggleClass('-compact');
	});

	$('.b_dashboard #xls_order').validate({
		submitHandler: function submitHandler() {
			$.magnificPopup.open({
				items: {
					src: '#thx_popup'
				},
				type: 'inline'
			});
		}
	});

	$('.b_togglable_content ._header').click(function () {
		$(this).closest('.b_togglable_content').toggleClass('-closed');
	});

	$(".b_product_card ._thumbnail").click(function () {
		var $this = $(this),
		    $thumbnailImg = $this.find('._content.-image').clone(),
		    bigImgHref = $this.find('._content.-image').attr('data-big');

		var other_pictures = '';

		$('._img_slider .owl-item').each(function () {
			if ($(this).find('img').attr('data-big') !== $this.find('img').attr('data-big')){
				other_pictures += '<a href="'+$(this).find('img').attr('data-big')+'" rel="alternate"></a>';
			}
		});

		if ($thumbnailImg.length) {
			$thumbnailImg.addClass('_main_image');
			$this.closest('.b_product_card ._images_block').find('._image_wrapper').html($thumbnailImg);
			$this.closest('.b_product_card ._images_block').find('._image_wrapper img').wrap('<a href="' + bigImgHref + '" class="img_popup"></a>');
			$this.closest('.b_product_card ._images_block').find('._image_wrapper').append(other_pictures);
		} else if ($this.find('.-three-dimensional')) {
			var data = $this.data();
			$('.b_product_card ._main_image').reel({ src: data.src, images: data.images });
		}
	});

	$(".b_product_card ._form").validate();

	$(".b_dashboard ._excel_order_button").click(function () {
		$(this).next("._xls_management").slideToggle();
	});

	$(".b_profile_form ._form.-profile_form").validate();
	$(".b_profile_form ._form.-password_form").validate();

	$(".b_product").magnificPopup({
		delegate: 'a._product_img',
		type: 'image',
		closeOnContentClick: false,
		closeBtnInside: false,
		mainClass: 'mfp-with-zoom mfp-img-mobile',
		image: {
			verticalFit: true
		},
		zoom: {
			enabled: true,
			duration: 300,
			opener: function opener(element) {
				return element.find('img');
			}
		}
	});
});
//# sourceMappingURL=main.js.map
