jQuery(document).ready(function ($) {

	//detect-mac-os
	if(navigator.userAgent.indexOf('Mac') > 0){
		$('body').addClass('mac-os');  
	}
	if(navigator.userAgent.indexOf('Mobile') > 0){
		$('body').addClass('mobile');  
	}
	
	$('a').off('click');
	$('a').on('click',function(){
		var h = $(this).attr('href');
		if(h.indexOf('http') == -1)						{ /* open normally */ } 
		else if(h.indexOf(window.location.host) == -1)	{ window.open(h); return false; } 
		else											{ /* open normally */ }
	})
	
	$('body.mobile a').off('click');
	$('body.mobile a').on('click',function(){
		if($(this).hasClass('noMobileClick')) { return false; }
	})
	
	//menu-toggle
	$('.menu-toggle').click(function(){
		$('body').toggleClass('open-nav');
	});
	$('.btn-close').click(function(){
		$('body').removeClass('open-nav');
		$('.navigation > ul > li').removeClass('open-subnav');
	});
	$('.navigation > ul > li > a').click(function(){
		$(this).parents('.navigation > ul > li').addClass('open-subnav');
	});
	$('.btn-back').click(function(){
		$(this).parents('.navigation > ul > li').removeClass('open-subnav');
	});
	
	//home-slider
	$('.home-slider').owlCarousel({
		items:1,
		margin: 0,
		dots:true,
		nav: true,
		loop:true,
		autoplay:false,
		autoplayTimeout: 5000,
		smartSpeed:700,
		animateOut:'fadeOut',
		touchDrag: false,
		mouseDrag:false,
		navText: [' ',' ']	
	});
	
	//portfolio-slider
	$('.portfolio-slider').owlCarousel({
		items:1,
		margin: 0,
		dots:true,
		nav: true,
		loop:true,
		autoplay:false,
		autoplayTimeout: 5000,
		smartSpeed:700,
		navText: [' ',' ']
	});
	
	//step-sliders with forms
	if($('body').hasClass('mobile')) {
		var stepSlider = $('.step-slider.inquiry-form').owlCarousel({
			items:1,
			margin: 0,
			dots:true,
			nav: false,
			loop:false,
			autoplay:false,
			smartSpeed:700,
			//navContainer: '.owl-nav',
			//navText: ['<span>previous step</span>','<span>NEXT STEP</span>'],
			dotsData: true,
			autoHeight:true,
			touchDrag: false,
			mouseDrag:false,
			onInitialized: function(e) {
				//console.log('onInitialized');console.log(e);
				initStepFormsDots(); initStepFormsButtons(e);
			},
			onChanged: function(e) {
				//console.log('onChanged');console.log(e);
				if(e.item.index == 0 || e.item.index == null) { $('.owl-prev').addClass('disabled'); } else { $('.owl-prev').removeClass('disabled'); }
				if(e.item.index == (e.item.count - 1)) { $('.owl-prev').addClass('disabled'); } else { $('.owl-prev').removeClass('disabled'); }
				scrollToCarousel(); initStepFormsDots(); initStepFormsButtons(e);
			},
		});
	} else {
		var stepSlider = $('.step-slider.inquiry-form').owlCarousel({
			items:1,
			margin: 0,
			dots:true,
			nav: false,
			loop:false,
			autoplay:false,
			smartSpeed:700,
			//navContainer: '.owl-nav',
			//navText: ['<span>previous step</span>','<span>NEXT STEP</span>'],
			dotsData: true,
			autoHeight:false,
			touchDrag: false,
			mouseDrag:false,
			onInitialized: function(e) {
				//console.log('onInitialized');console.log(e);
				initStepFormsDots(); initStepFormsButtons(e);
			},
			onChanged: function(e) {
				//console.log('onChanged');console.log(e);
				if(e.item.index == 0 || e.item.index == null) { $('.owl-prev').addClass('disabled'); } else { $('.owl-prev').removeClass('disabled'); }
				if(e.item.index == (e.item.count - 1)) { $('.owl-prev').addClass('disabled'); } else { $('.owl-prev').removeClass('disabled'); }
				scrollToCarousel(); initStepFormsDots(); initStepFormsButtons(e);
			},
		});
	}
	//step-sliders without forms
	if($('body').hasClass('mobile')) {
		$('.step-slider.no-form').owlCarousel({
			items:1,
			margin: 0,
			dots:true,
			nav: true,
			loop:false,
			autoplay:false,
			smartSpeed:700,
			navText: ['<span>PREVIOUS STEP</span>','<span>NEXT STEP</span>'],
			dotsData: true,
			autoHeight:true,
			touchDrag: false,
			mouseDrag:false,
			onInitialized: function(e) {
				//console.log('onInitialized');console.log(e);
				var pb = $('.owl-prev'); if(e.item.index == 0)					{ pb.addClass('hid'); } else { pb.removeClass('hid'); }
				var nb = $('.owl-next'); if(e.item.index < (e.item.count - 1))	{ nb.removeClass('hid'); } else { nb.addClass('hid'); }
				//initStepFormsDots(); initStepFormsButtons(e);
			},
			onChanged: function(e) {
				//console.log('onChanged');console.log(e);
				var pb = $('.owl-prev'); if(e.item.index == 0)					{ pb.addClass('hid'); } else { pb.removeClass('hid'); }
				var nb = $('.owl-next'); if(e.item.index < (e.item.count - 1))	{ nb.removeClass('hid'); } else { nb.addClass('hid'); }
				if(e.item.index == 0 || e.item.index == null) { $('.owl-prev').addClass('disabled'); } else { $('.owl-prev').removeClass('disabled'); }
				if(e.item.index == (e.item.count - 1)) { $('.owl-prev').addClass('disabled'); } else { $('.owl-prev').removeClass('disabled'); }
				scrollToCarousel(); 
			},
		});	
	} else {
		$('.step-slider.no-form').owlCarousel({
			items:1,
			margin: 0,
			dots:true,
			nav: true,
			loop:false,
			autoplay:false,
			smartSpeed:700,
			navText: ['<span>PREVIOUS STEP</span>','<span>NEXT STEP</span>'],
			dotsData: true,
			autoHeight:false,
			touchDrag: false,
			mouseDrag:false,
			onInitialized: function(e) {
				//console.log('onInitialized');console.log(e);
				var pb = $('.owl-prev'); if(e.item.index == 0)					{ pb.addClass('hid'); } else { pb.removeClass('hid'); }
				var nb = $('.owl-next'); if(e.item.index < (e.item.count - 1))	{ nb.removeClass('hid'); } else { nb.addClass('hid'); }
				//initStepFormsDots(); initStepFormsButtons(e);
			},
			onChanged: function(e) {
				//console.log('onChanged');console.log(e);
				var pb = $('.owl-prev'); if(e.item.index == 0)					{ pb.addClass('hid'); } else { pb.removeClass('hid'); }
				var nb = $('.owl-next'); if(e.item.index < (e.item.count - 1))	{ nb.removeClass('hid'); } else { nb.addClass('hid'); }
				if(e.item.index == 0 || e.item.index == null) { $('.owl-prev').addClass('disabled'); } else { $('.owl-prev').removeClass('disabled'); }
				if(e.item.index == (e.item.count - 1)) { $('.owl-prev').addClass('disabled'); } else { $('.owl-prev').removeClass('disabled'); }
				scrollToCarousel(); 
			},
		});	
	}

	function initStepFormsButtons(e)
	{
		//console.log('initStepFormsButtons');console.log(e.item);
		var pb = $('.owl-prev'); if(e.item.index == 0)				{ pb.addClass('hid'); } else { pb.removeClass('hid'); }
		var nb = $('.owl-next'); if(e.item.index < (e.item.count - 2))	{ nb.removeClass('hid'); } else { nb.addClass('hid'); }
		$('.inquire-form-steps .owl-next').off('click');
		$('.inquire-form-steps .owl-next').on('click',function(e) {
			if(formVaild($('.step-slider.inquiry-form .owl-stage .owl-item.active'))==0) {
				stepSlider.trigger('next.owl.carousel');
			}
		});
		$('.inquire-form-steps .owl-prev').off('click');
		$('.inquire-form-steps .owl-prev').on('click',function(e) {
			stepSlider.trigger('prev.owl.carousel');
		})
	} 
	function initStepFormsDots() {
		// console.log('initStepFormsDots'); console.log($('body').attr('id'));
		if($('body#residential-design-build-steps').length==1)						{ var r = 8; }
		else if($('body#consultation-design-diy-design-build').length==1)			{ var r = 8; }
		else if($('body#existing-treehouse-maintenance-repair').length==1) 			{ var r = 7; }
		else if($('body#commercial-treehouse-design-build').length==1)				{ var r = 12; }
		else if($('body#personal-treehouse-design-build').length==1)				{ var r = 12; }
		else if($('body#vacation-rental-full-design-build-of-treehouse').length==1)	{ var r = 12; }
		if(r != undefined) { $('.owl-dots .owl-dot:nth-child('+r+')').remove(); }
	} 
	function scrollToCarousel() {
	    $([document.documentElement, document.body]).animate({
	        scrollTop: $('.owl-carousel').offset().top
	    },1000);
	}
	function formVaild(th) {
		var form = th; form.find('error').removeClass('active'); var formErr = 0;
		form.find('input[type="text"],input[type="tel"],input[type="radio"],input[type="checkbox"],input[type="hidden"],select,textarea').each(function(){
			var f = $(this), n = f.attr('name'); f.val($.trim(f.val()));
			if(f.is('input[type="text"],input[type="tel"],input[type="hidden"],textarea:not(.g-recaptcha-response),select'))
			{
				if(f.hasClass('req'))
				{
					if(f.val().length==0)	{ f.closest('.owl-item').find('.error').addClass('active');form.addClass('error'); formErr++; }
					else					{ f.closest('.owl-item').find('.error').removeClass('active');form.removeClass('error'); }
				}
				//if(f.val().length==0)		{ f.closest('.owl-item').find('.error').addClass('active'); formErr++; }
				//else					{ f.closest('.owl-item').find('.error').removeClass('active'); }
			}
			else if(f.is('input[type="radio"],input[type="checkbox"]'))
			{
				if(f.hasClass('req'))
				{
					if($('input[name="'+n+'"]:checked').length==0)	{ f.closest('.owl-item').find('.error').addClass('active');form.addClass('error');  formErr++; }
					else										{ f.closest('.owl-item').find('.error').removeClass('active');form.removeClass('error');  }
				}
			}
		});
		//console.log(formErr);
		return formErr;
	}
	
	// testimonial-slider
	$('.testimonial-slider').owlCarousel({
		items:1,
		margin: 0,
		dots:true,
		nav: true,
		loop:false,
		autoplay:false,
		smartSpeed:700,
		navText: [' ',' ']
	});
	
	// gallery-slider
	$('.gallery-slider').owlCarousel({
		items:1,
		margin: 0,
		dots:true,
		nav: true,
		loop:false,
		autoplay:false,
		smartSpeed:700,
		navText: [' ',' ']
	});
	
	// custom-tabs
	$('.tab_link').click(function(){
		var tab_id = $(this).attr('data-tab');	
		$('.tab_link').removeClass('current');
		$('.tab_desc').removeClass('current');	
		$(this).addClass('current');
		$("#"+tab_id).addClass('current');
	});
	
	// cart
	$('.cart').click(function(){
		$('body').addClass('open-cart');
	});
	$('.cart-close').click(function(){
		$('body').removeClass('open-cart');
	});
	
	// qty
	$('.inc').on('click',function(){
		var $qty=$(this).closest('.qty').find('#qty');
		var currentVal = parseInt($qty.val());
		if (!isNaN(currentVal)) {
			$qty.val(currentVal + 1);
		}
	});
	$('.dec').on('click',function(){
		var $qty=$(this).closest('.qty').find('#qty');
		var currentVal = parseInt($qty.val());
		if (!isNaN(currentVal) && currentVal > 1) {
			$qty.val(currentVal - 1);
		}
	});
	
	//Learn-slider
	$('.learn-slider').owlCarousel({
		items:1,
		margin: 0,
		dots:true,
		nav: true,
		loop:true,
		autoplay:false,
		autoplayTimeout: 5000,
		smartSpeed:700,
		navText: [' ',' ']
	});
	
	$('#sync1').owlCarousel({
		items:1,
		loop:false,
		dots:false,
		nav:false,
		URLhashListener:true,
		autoplay:false,
		startPosition: 'URLHash',
		thumbs: true,
		smartSpeed:700
	});
	
	$('#sync2').owlCarousel({
		loop:false,
		items :4,
		nav:false,
		dots:false,
		margin:30,
		autoplayHoverPause:false,
		smartSpeed:700,
		URLhashListener:true,
		autoplay:false,
		startPosition: 'URLHash',
		touchDrag: false,
		mouseDrag:false,
		responsive:{
			0:{
				margin:15
			},
			767:{
				margin:30
			}
		}
	});
	$('#sync2 .item > a').click(function(){
		$(this).parents('.owl-item').addClass('current');
		$(this).parents('.owl-item').siblings('.owl-item').removeClass('current');
	});
	
	//-home nav
	$('.home-header .navigation > ul > li > a').hover(function(){ $('.home-header').addClass('nav-hover')  });
	$('.home-header').mouseleave(function(){ $(this).removeClass('nav-hover'); });
	
	//blog-category-accrodion
	$('.blog-housecontent > h4').click(function(){
		$(this).parent('.blog-housecontent').toggleClass('cat-open');
		$(this).parent('.blog-housecontent').siblings().removeClass('cat-open');
		$(this).parent('.blog-housecontent').children('.blog-housecontent > ul').slideToggle();
		$(this).parent('.blog-housecontent').siblings().children('.blog-housecontent > ul').slideUp();
	});

	
	if($('body#our-story').length==1)
	{
		var topPosition = $('.floating-div').offset().top - 10;
		var floatingDivHeight = $('.floating-div').outerHeight();
		var footerFromTop = $('.feature-items').offset().top;
		var absPosition = footerFromTop - floatingDivHeight - 300;
		var win = $(window);
		var floatingDiv = $('.floating-div');
		
		win.scroll(function() {
			if(window.matchMedia('(min-width: 768px)').matches)
			{
				if((win.scrollTop() > topPosition) && (win.scrollTop() < absPosition)) {
					floatingDiv.addClass('sticky');
					floatingDiv.removeClass('abs');		
				} else if ((win.scrollTop() > topPosition) && (win.scrollTop() > absPosition)) {
					floatingDiv.removeClass('sticky');
					floatingDiv.addClass('abs');
				} else {
					floatingDiv.removeClass('sticky');
					floatingDiv.removeClass('abs');
				}
				var scrollDistance = $(window).scrollTop();
				// Assign active class to nav links while scolling
				$('.page-section').each(function(i) {
					if ($(this).position().top - 30 <= scrollDistance)
					{
						$('.navigation1 a.active').removeClass('active');
						$('.navigation1 ul li.active').removeClass('active');
						$('.navigation1 a').eq(i).addClass('active');
						$('.navigation1 ul li').eq(i).addClass('active');
					}
				});
			
			}
		});
	
		$(document).ready(function() {
			$(".navigation1 a").on('click', function(event) {
				/*
				if (this.hash !== "") {
					event.preventDefault();
					var hash = this.hash;
					$('.page-section').removeClass('active');
					$('#'+hash.substr(1)+'.page-section').addClass('active');
					$('html, body').animate({
						scrollTop: $(hash).offset().top 
					}, 800, function(){
						turn false;
						window.location.hash = hash;
					});
				}
				*/
				return false;
			});
		});		

	}
	
	if($('body#body#residential-full-design-build-of-treehouse').length==1)
	{
		$('body#residential-full-design-build-of-treehouse .owl-dots .owl-dot:nth-child(13)').remove();
	}
	if($('body#body#commercial-full-design-build-of-treehouse').length==1)
	{
		$('body#residential-full-design-build-of-treehouse .owl-dots .owl-dot:nth-child(11)').remove();
	}
	if($('body#body#vacation-rental-full-design-build-of-treehouse').length==1)
	{
		$('body#vacation-rental-full-design-build-of-treehouse .owl-dots .owl-dot:nth-child(11)').remove();
	}

	if($('body').hasClass('mobile'))
	{
		$('body#our-crew.mobile .img-grids .portf-img-wrap').attr('data-toggle','modal').attr('data-target','.modal.crew-modal');
		$('body#our-crew.mobile .img-grids .portf-img-wrap').off('click'); 
		$('body#our-crew.mobile .img-grids .portf-img-wrap').on('click',function(){ 
			var h = '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>'+$(this).find('.hover-box').html();
			$('.modal.crew-modal .modal-content').html(h);
		}); 
	}

	$('body.archive.tax-collection select.product-sort').off('change'); 
	$('body.archive.tax-collection select.product-sort').on('change',function(){ 
		var v = $(this).val(), c = $(this).data('c'), u = window.URI+'/collection/';
		if(v.length>0) { u += '/'+c+'/'+v+'/'; } else { u += '/'+c+'/'; } window.location = u;
	});

	$('body.blog-template-default .blog-post-wrap p').each(function(i) {
		var th = $(this); if(th.find('img').length>0) { th.addClass('has-img'); }
	});

	$('body#shop section.backToTop a.scrollToTop').off('click'); 
	$('body#shop section.backToTop a.scrollToTop').on('click',function(){ 
	    $([document.documentElement, document.body]).animate({
	        scrollTop: $('body').offset().top
	    },1000);
		return false;
	});

	$('.top-search .search_form_open, .top-search .search_form_close').off('click'); 
	$('.top-search .search_form_open, .top-search .search_form_close').on('click',function(){ 
		$('.top-search').toggleClass('open');
	});

	function controls() {
		$(this).addClass('selected').siblings('button').removeClass('selected');
		$('.slideDiv').animate({
			top: -$('.v-container').height() * $(this).index()
		}, 450);
	}

	/***Event Listeners***/
	const runCode = () => {
		const button = document.querySelectorAll('.button');
		if(button) {
			for ( var i = 0; i < button.length; i++ ) {
				button[i].addEventListener('click', controls, false);
			}
		}
	}

	runCode();

	if($(window).width() < 992) {
		$(document).ready(function(){
			$('.accordion').removeClass('width');
			$('.collapse ').removeClass('width');
		});
	} else {
		// console.log($('#accordionHorizontalExample').outerWidth());
		$('#accordionHorizontalExample').on('shown.bs.collapse', function () {
			// $('.accordion .card').removeClass('show');
			// $('.accordion .width.collapse.show').closest('.card').find('.card-header').addClass('show');
			// var total = $('#accordionHorizontalExample').outerWidth();
			// var sub = $('.accordion .width.collapse:not(.show) .card-body').width();
			// $('.accordion .width.collapse.show .card-body').css('width',(total-sub)+'px');
		})
	}

	if($('body').hasClass('products-post-type'))
	{
		wp.hooks.addFilter('misc.show.inventoryLevels', 'wpshopify', function() {
			return false;
		});
	}
	
	$(window).on('resize',function(){
		console.log('$(window).width() == '+$(window).width());
		if($(window).width() > 992) {
			// 	window.location = window.location.href;
			if($('body#nelson-treehouse').length==1)
			{
				document.body.style.zoom = 0.9999999; setTimeout(function(){document.body.style.zoom = 1;},10);
				$('#accordionHorizontalExample').collapse('dispose');$('#accordionHorizontalExample').collapse();
				// $('.accordion .card').removeClass('show');
				// $('.accordion .width.collapse.show').closest('.card').find('.card-header').addClass('show');
				// var total = $('#accordionHorizontalExample').outerWidth();
				// var sub = $('.accordion .width.collapse:not(.show) .card-body').width();
				// $('.accordion .width.collapse.show .card-body').css('width',(total-sub)+'px');
			}
		}
	});
	
});

(function($) { 
	if($('body#youtube-videos').length==1 || $('body').hasClass('blog-template-default')) 
	{ 
		fluidvids.init({ 
			selector: ['iframe', 'object'], // runs querySelectorAll() 
			players: ['www.youtube.com','youtube.com'] // players to support 
		}); 
	} 
})(jQuery);

(function($) { function ajax_portfolio_init() { $('.portf-img-wrap .portf-desc button').off('click'); $('.portf-img-wrap .portf-desc button').on('click',function(){ var u = 'https://nelsontreehouse.com/wp-json/api/v2/portfolio/gallery/'+$(this).data('gallery')+'/'; $('.modal.portfolio-gallery .modal-content').load(u,function(responseTxt,statusxhr) { $('.gallery-slider').owlCarousel({ items:1,margin:0,dots:true,nav:true,loop:false,autoplay:false,smartSpeed:700,navText: [' ',' '] }); }); }); $('.portfolio-tabs .tab-pane .load-more').off('click'); $('.portfolio-tabs .tab-pane .load-more').on('click',function(){ var tx = $('.portfolio-tabs .tab-pane.active').attr('id'); var pgn = parseInt($(this).data('pg')), pg = pgn+1; $(this).data('pg',pg); var ajxData = {}; ajxData.action = 'more_portfolio'; ajxData.n = $(this).data('n'); ajxData.pg = pg; ajxData.tx = tx; $.ajax({ method: 'post', url: feAJX.ajaxurl, dataType: 'html', data: ajxData, cache: false, success: function(h) { $('.portfolio-tabs .tab-pane.active .img-grids').append(h); ajax_portfolio_init(); } }); }); } ajax_portfolio_init(); })(jQuery);
