jQuery(function($) {
	
	// Woocommerce category hook
	$('.products .product-category').last().after('<div style="width: 100%; display: block; clear: both;"></div>');
		
	$(document).ready(function() {	

		// Setup
		setup_nav();
		menuHeightHook();
		removeResponsiveMenu();
		
		// Slider 
		$('#slider').slick({
			slidesToShow	: 1,
			slidesToScroll	: 1,
			dots			: true,
			arrows			: false,
			autoplay		: true,
			fade			: true, 
			autoplaySpeed	: 7000,
			speed			: 1000,
			responsive: [
				{
					breakpoint: 960,
					settings: {
						slidesToShow	: 1,
						slidesToScroll	: 1,
						dots			: true,
						arrows			: false,
					},
				}
			]
		});
		
		
		// Announcements
		$('.announcements').slick({
			slidesToShow	: 1,
			slidesToScroll	: 1,
			dots			: false,
			arrows			: false,
			pauseOnHover	: false, 
			autoplay		: true,
			autoplaySpeed	: 5000,
			speed			: 500,
			responsive: [
				{
					breakpoint: 960,
					settings: {
						slidesToShow	: 1,
						slidesToScroll	: 1,
						dots			: false,
						arrows			: false,
					},
				}
			]
		});
		
		
		// Fancybox
		$('.woocommerce-product-gallery__wrapper a').fancybox().attr('data-fancybox', 'gallery');
		$('[data-fancybox], .woocommerce-product-gallery__wrapper a').fancybox({
			loop: true,
			thumbs: {
				autoStart: true,
			},
			mobile : {
				thumbs : {
					autoStart: false
				}
			},
			buttons: [
				"zoom",
				//"share",
				//"slideShow",
				//"fullScreen",
				//"download",
				"thumbs",
				"close"
			],
		});
		
		$('.close_fancybox').click(function() { 
			$.fancybox.close(); 
		});
		
		
		// RWD button
		$('.rwd_button').click(function() {
			if ($('body').hasClass('rwd_menu__opened')) {
				$('body').removeClass('rwd_menu__opened');
			} else {
				$('body').addClass('rwd_menu__opened');
			}
		});
		
		$('#rwd_menu__close').click(function() {
			$('body').removeClass('rwd_menu__opened');
		});	
		
		$('#rwd_menu li.menu-item-has-children>a').on('click', function(){
			$(this).removeAttr('href');
			var element = $(this).parent('li');
			if (element.hasClass('open')) {
				element.removeClass('open');
				element.find('li').removeClass('open');
				element.find('ul').slideUp();
			}
			else {
				element.addClass('open');
				element.children('ul').slideDown();
				element.siblings('li').children('ul').slideUp();
				element.siblings('li').removeClass('open');
				element.siblings('li').find('li').removeClass('open');
				element.siblings('li').find('ul').slideUp();
			}
		});
		
		
		// Scroll
		$('.scroll_somewhere').click(function(evt) {
			evt.preventDefault();
			
			var distance = 0,
				winWidth = $(window).width();
			
			if ((winWidth <= 960) && (winWidth > 0)) {
				var distance = 100;
			} else {
				var distance = 90;
			}
			
			$('html, body').stop().animate({
				scrollTop: $($(this).attr('href')).offset().top - distance
			}, 1000);
		});
		
		
		// Edit button hack
		$('a.post-edit-link').attr('target', '_blank');
		
		
		// Add to cart hook
		$('.add_to_cart_button').not('.outofstock .add_to_cart_button').click(function() {
			
			if ($('body').hasClass('single-product')) {
				var thisName 	= $('.summary h1').text(), 
					thisImage 	= $('.images .woocommerce-product-gallery__image:first-child a img').attr('src'), 
					thisPrice 	= $('.summary .price').html();
					
			} else { 
				var thisName 	= $(this).closest('.item').find('h3').text(), 
					thisImage 	= $(this).closest('.item').find('img').attr('src'), 
					thisPrice 	= $(this).closest('.item').find('.price').html();
			}
			
			$('#modal_added_to_cart .ordered_product h4').html(thisName);
			$('#modal_added_to_cart .ordered_product .thumbnail img').attr('src', thisImage);
			$('#modal_added_to_cart .ordered_product .price').html(thisPrice);
			$('body').addClass('open-notification');
			
			$.fancybox.open({
				src: '#modal_added_to_cart', 
				smallBtn: true,
				touch : {
					vertical : false,
				}
			});
		});
		
		
		// Iframe hook
		$('iframe').not('body.page-template-individual-cpl-doors iframe').parent('p').addClass('iframe_wrapper');
		
		
		// Animation hook
		$('.has_animation').each(function() {
			var thisDelay = $(this).attr('data-delay');
			$(this).css({
				'-webkit-transition-delay'	: thisDelay,
				'-moz-transition-delay'		: thisDelay,
				'-o-transition-delay'		: thisDelay,
				'transition-delay'			: thisDelay
			});
		});
		
		
		
		// Terms error notice hook
		$(document).ajaxComplete(function() { 
			if ($('body').hasClass('woocommerce-checkout')) {
				if (!$('#terms').is(':checked')) { 
					$('html, body').stop();
					$('ul.woocommerce-error').insertAfter('ul.payment_methods');
				}
			}
		});
		
		
		// Form hook
		$('span.wpcf7-form-control-wrap').hover(function() {
			$(this).find('span.wpcf7-not-valid-tip').fadeOut();
		});
		
		
		// Sidebar hook
		$('.open_sidebar').click(function() {
			if ($(this).hasClass('active')) {
				$('body').removeClass('opened_sidebar');
				$(this).find('span').html('Szűrő megnyitása');
				$(this).removeClass('active');
			} else {
				$('body').addClass('opened_sidebar');
				$(this).find('span').html('Szűrő bezárása');
				$(this).addClass('active');
			}
		});
		
		$('.close_sidebar').click(function() {
			$('body').removeClass('opened_sidebar');
			$('.open_sidebar').removeClass('active');
			$('.open_sidebar span').html('Szűrő megnyitása');
		});
		
		
		// Check if link is external
		$("a.button:not([data-fancybox])").each(function() { 
			if (link_is_external(this)) { 
				$(this).addClass('link_is_external');
			}
		});
	});
	
	
	// Resize
	jQuery(window).resize(function() {	
	
		// Setup
		setup_nav();
		menuHeightHook();
		removeResponsiveMenu();
	});
	
	
	// Menu
	function setup_nav() {
		var nav 		= $('#header'), 
			winWidth 	= $(window).width(),
			nav_orig_y 	= $('#header').position().top + 1, 
			window_y 	= 0;
		
		jQuery(window).scroll(function() { 
			window_y = jQuery(window).scrollTop();
			if (window_y >= nav_orig_y) {
				$('body').addClass('fixed_header');
			} else if(window_y < nav_orig_y) {
				$('body').removeClass('fixed_header');
			}
		}).trigger('scroll');
	}
	
	
	// Menu height hook
	function menuHeightHook() {
		var winWidth = $(window).width(),
			parentHeight = $('#header ul.menu > li > ul').height();
			
		//$('#header ul.menu > li > ul > li > ul').height(parentHeight - 60);
	}
	
	
	// Remove RWD menu
	function removeResponsiveMenu() {
		var winWidth = $(window).width();
		if (winWidth > 960) {
			$('body').removeClass('rwd_menu__opened');
		}
	}
	
	
	// If link is external
	function link_is_external(link_element) {
		return (link_element.host !== window.location.host);
	}
});
