$ = jQuery;
var API;

function paramReplace(name, string, value) {
  // Find the param with regex
  // Grab the first character in the returned string (should be ? or &)
  // Replace our href string with our new value, passing on the name and delimeter
  var re = new RegExp("[\\?&]" + name + "=([^&#]*)"),
	  delimeter = re.exec(string)[0].charAt(0),
	  newString = string.replace(re, delimeter + name + "=" + value);
 
  return newString;
}

$(document).ready(function(){
	
	$(".selectpicker").selectpicker({
		style : 'form-control btn-select'
	});
	
	$(".btn-select .caret").removeClass("caret").addClass("glyphicon glyphicon-triangle-bottom");

	$(".section_arrow").click(function() {
		var target = $(this).parents(".section").eq(0).next();
		$('html, body').animate({
			scrollTop : $(target).offset().top
		}, 500);
	})
	
	$(".ch_display_half").insertAfter(".ch_select_half");
	
	$('input, textarea').placeholder();

	$("input").focus(function(){
		$(this).parent().addClass("focus");
	}).blur(function(){
		$(this).parent().removeClass("focus");
	});
	
	$("textarea").focus(function(){
		$(this).parent().addClass("focus");
	}).blur(function(){
		$(this).parent().removeClass("focus");
	});
	
	$('ul.sf-menu').superfish({
		delay: 300,	
		speed: 100,
	});
	
	$('header .no_link > a').click(function(e) {
        e.preventDefault();
    });
	
	$("#main-nav").mmenu({
		offCanvas : {
			position : "left",
		},
	}, {
		clone : true
	});
	
	API = $("#mm-main-nav").data("mmenu");
	
	window.onpageshow = function(event) {
		if (event.persisted) {
			if($('#mm-main-nav').hasClass('mm-opened')) {
				API.close();
				console.log('close');
			}
		}
	};
	
	$('.home_box_bottom').matchHeight();
	
	$('.pg_navigation .next').parent().addClass('nxt_li');
	$('.pg_navigation .prev').parent().addClass('prev_li');
	
	$('.testimonials_slider').slick({
	  dots: true,
	  infinite: true,
	  speed: 300,
	  slidesToShow: 1,
	  adaptiveHeight: true
	});
	
	$('.tooltip_button').click(function(e) {
		e.stopPropagation();
        if($(this).find('.show_tooltip').length) {
			$(this).find('.tooltip_wr').removeClass('show_tooltip');
			$(this).removeClass('tooltip_active');
		} else {
			$('.tooltip_wr').removeClass('show_tooltip');
			$('.tooltip_button').removeClass('tooltip_active');
			$(this).find('.tooltip_wr').addClass('show_tooltip');
			$(this).addClass('tooltip_active');
		}
    });
	
	$(window).click(function() {
		if($('html').find('.show_tooltip').length) {
			$('.tooltip_wr').removeClass('show_tooltip');
			$('.tooltip_button').removeClass('tooltip_active');
		}
	});
	
	$('.rotate_ul a').click(function(e) {
        e.preventDefault();
		$('.rotate_ul li').removeClass('active_rotator_thumb');
		var nElement = $(this).parents('li').index()+1;
		$(".rotate_ul li:nth-child("+nElement+")").addClass('active_rotator_thumb');
		$('.rotate_chair').removeClass('active_rotator');
		$(".rotate_chair:nth-child("+nElement+")").addClass('active_rotator');
    });
	
	$('.faq_row').click(function(e) {
        $(this).toggleClass('faq_active');
		$(this).siblings().removeClass('faq_active');
    });
	
	
	$('.product_thumbs a').click(function(e) {
        e.preventDefault();
		$(this).parents('ul').find('li a').removeClass('active_thumb');
		$(this).addClass('active_thumb');
		var nElement = $(this).parents('li').index()+1;
		$(this).parents('.product_images_box').find('.main_images_wr .single_image_box').removeClass('active_image');
		$(this).parents('.product_images_box').find('.main_images_wr .single_image_box:nth-child('+nElement+')').addClass('active_image');
    });
	
	$('.color_ul a').click(function(e) {
        e.preventDefault();
		$(this).parents('ul').find('li').removeClass('active_color');
		$(this).parent().addClass('active_color');
		var nElement = $(this).parents('li').index()+1;
		$('.product_images_wr').find('.active_set .product_images_box').removeClass('active_chair');
		$('.product_images_wr').find('.active_set .product_images_box:nth-child('+nElement+')').addClass('active_chair');
		
		var selected_qs_color = $('.product_images_wr').find('.active_set .product_images_box:nth-child('+nElement+') .chair_query_etring').text();
		$('.purchase_button').attr('href',paramReplace('chr_color', $('.purchase_button').attr('href'), selected_qs_color));
		
    });
	
	$( ".slct" ).selectmenu({
	  change: function( event, ui ) {
		var nElement = ui.item.index + 1;
		var selected_qs_material = $(this).val(); // or this.value
		$('.product_images_wr').find('.active_set').removeClass('active_set');  
		$('.product_images_wr').find('.matherial_chairs_set:nth-child('+nElement+')').addClass('active_set');
		$('.colors_thumbs_wrapper').find('.active_select_wrapper').removeClass('active_select_wrapper');
		$('.colors_thumbs_wrapper').find('.color_select_wrapper:nth-child('+nElement+')').addClass('active_select_wrapper');
		
		$('.purchase_button').attr('href',paramReplace('chr_fabric', $('.purchase_button').attr('href'), selected_qs_material));
		var selected_qs_color2 = $('.product_images_wr').find('.active_set .product_images_box.active_chair .chair_query_etring').text();
		
		var int_string = paramReplace('chr_color', $('.purchase_button').attr('href'), selected_qs_color2)
		
		var f_url = '';
		
		if($('.page-template-build-your-chair-petite').length) {
			if($(this).val() == 'Leather') {
				f_url = int_string.replace("https://order.", "https://shop.");	
			} else {
				f_url = int_string.replace("https://shop.", "https://order.");	
			}
		} else {
			if($(this).val() == 'Leather') {
				f_url = int_string.replace("cart", "buy");	
			} else {
				f_url = int_string.replace("buy", "cart");	
			}
		}
		
		$('.purchase_button').attr('href',f_url);
	  }
	});
	
	$('.toggle_button').click(function(e) {
        e.preventDefault();
		$(this).slideToggle(200);
		$('.slide_fc').slideToggle(200);
    });
	
	$('.close_section').click(function(e) {
		$('.toggle_button').slideToggle(200);
		$('.slide_fc').slideToggle(200);
    });
	
	$('.remote_button').click(function(e) {
        e.preventDefault();
		$('.remote_button').removeClass('active');
		$(this).addClass('active');
		var imgsource = $(this).data('imgsrc');
		$('.gif_img').attr('src',imgsource);
		console.log($(this).data('imgsrc'));
		
    });
	
	if($('.remote_button').length) {
		
		var imgArray = [];
		
		$('.remote_button').each(function(index, element) {
            imgArray.push($(this).data('imgsrc'));
        });
		
		function preload(arrayOfImages) {
			$(arrayOfImages).each(function(){
				$('<img/>')[0].src = this;
			});
		}
		
		preload(imgArray);
		
		console.log('Prealoaded gif images');
	}
	
	$('.heat_rotate').each(function(){
		if ($(this).visible() && !$(this).hasClass('v_played')) {
			$(this).addClass('v_played');
			$(this).attr('src',$(this).attr('src'));
		}
	})
	
});

$(window).scroll(function() {
		
	$('.heat_rotate').each(function(){
		if ($(this).visible() && !$(this).hasClass('v_played')) {
			$(this).addClass('v_played');
			$(this).attr('src',$(this).attr('src'));
		}
	})
	
});