$(document).ready(function(){
	
	if($(".mn_child_02").length){
		$(".mn_child_01").mouseover(function() {
			$(".mn_child_01 .mn_child_02").attr("style","left:"+$(this).width()+"px");
		});	
	}
	
	/***********************************************************************************/
	if($(".gallery-top").length){
		var galleryThumbs = new Swiper('.gallery-thumbs', {
			direction: 'horizontal',
			spaceBetween: 1,
			slidesPerView: "4",
			watchSlidesVisibility: false,
			watchSlidesProgress: true,
			centerInsufficientSlides: false
		});
		var galleryTop = new Swiper('.gallery-top', {
			autoplay: {
				delay: 5000
			},
			navigation: {
				nextEl: '.swiper-button-next-1-plus',
				prevEl: '.swiper-button-prev-1-plus',
			},
			spaceBetween: 10,
			slidesPerView: 1,
			thumbs: {
				swiper: galleryThumbs
			 }
		});
	}
	
	if($(".gallery-top2").length){
		var galleryThumbs2 = new Swiper('.gallery-thumbs2', {
			direction: 'horizontal',
			spaceBetween: 10,
			slidesPerView: "5",
			watchSlidesVisibility: false,
			watchSlidesProgress: true,
			centerInsufficientSlides: false,
			navigation: {
				nextEl: '.swiper-button-next-2-plus',
				prevEl: '.swiper-button-prev-2-plus',
			},
			breakpoints: {
				320: {
					slidesPerView: "auto",
					centerInsufficientSlides: true
				}
			}
		});
		var galleryTop2 = new Swiper('.gallery-top2', {
			autoplay: {
				delay: 5000
			},
			spaceBetween: 10,
			slidesPerView: 1,
			thumbs: {
				swiper: galleryThumbs2
			 }
		});
	}
	
	if($(".swiper1").length){
		var swiper1 = new Swiper('.swiper1', {
			navigation: {
				nextEl: '.swiper-button-next-1',
				prevEl: '.swiper-button-prev-1',
			},
			watchOverflow: true,
			preloadImages: false,
			lazy: {
				loadPrevNext: true,					
			},
			slidesPerView: 5,
			spaceBetween: 5,
			breakpoints: {
				320: {
					slidesPerView: 2,
					spaceBetween: "15%"
				},
				640: {
					slidesPerView: 3,
					spaceBetween: "15%"
				},
				1024: {
					slidesPerView: 5,
					spaceBetween: 5
				}
			}
		});
	}
	
	/***********************************************************************************/
	
	$(".show_des_cata_prod").click(function() {
		var val_show_des_cata_prod=$(this).attr("val_show_des_cata_prod"); 
		if(val_show_des_cata_prod==0){
			$(".des_cata_prod").attr("style","max-height: inherit;");
			$(this).attr("val_show_des_cata_prod","1").text("Thu gọn").addClass("active");
		}else{
			$(".des_cata_prod").attr("style","max-height: 200;");
			$(this).attr("val_show_des_cata_prod","0").text("Xem thêm").removeClass("active");
		}
	});
	
	$(".show_nd_mt_prod_D").click(function() {
		var val_nd_mt_prod_D=$(this).attr("val_nd_mt_prod_D"); 
		if(val_nd_mt_prod_D==0){
			$(".nd_mt_prod_D .f-detail").attr("style","max-height: inherit;");
			$(this).attr("val_nd_mt_prod_D","1").text("Thu gọn").addClass("active");
			$(".mask_nd_mt_prod_D").addClass("active")
		}else{
			$(".nd_mt_prod_D .f-detail").attr("style","max-height: 400;");
			$(this).attr("val_nd_mt_prod_D","0").text("Xem thêm").removeClass("active");
			$(".mask_nd_mt_prod_D").removeClass("active")
		}
	});
	
	/***********************************************************************************/	
	
	$(".click_sosanh").keyup(function() {
		var val_ht=$(this).attr("val_ht");
		var val_key=$(this).val();
		var val_iddm=$(this).attr("val_iddm");
		if(val_key!=''){
			var val_key=encodeURIComponent(val_key);
			$.get("librarys/content_load.php?kieu=5&val_ht="+val_ht+"&val_key="+val_key+"&val_iddm="+val_iddm,function(data) {
				if(data!='0'){$(".load_data_ss").html(data).show();}
				else{$(".load_data_ss").hide();}
				
				$(".click_ss_now").click(function() {
					var val_ht=$(this).attr("val_ht");
					var val_ss=$(this).attr("val_ss");
					if(val_ht!=''){document.location.href = 'so-sanh/'+val_ht+','+val_ss+'.htm';}
					else{document.location.href = 'so-sanh/'+val_ss+'.htm';}
				});
				
			});
		}else{$(".load_data_ss").hide();}
	});
	
	String.prototype.replaceAll = function(strTarget,strSubString){
		var strText = this;
		var intIndexOfMatch = strText.indexOf(strTarget);
		while (intIndexOfMatch != -1){
			strText = strText.replace(strTarget, strSubString)
			intIndexOfMatch = strText.indexOf(strTarget);
		}
	return( strText );
	}
	
	$(".remove_ss").click(function() {
		var val_ht='['+$(this).attr("val_ht")+']';
		var val=$(this).attr("val");
		var val_new = val_ht.replaceAll(','+val,'');
		var val_new = val_new.replaceAll('['+val,'');
		var val_new = val_new.replaceAll('[','');
		var val_new = val_new.replaceAll(']','');
		
		var val_new='['+val_new+']';
		var val_new = val_new.replaceAll('[,','');
		var val_new = val_new.replaceAll(',]','');
		var val_new = val_new.replaceAll('[','');
		var val_new = val_new.replaceAll(']','');
		var val_new = val_new.replaceAll(',,','');
		if(val_new==''){document.location.href = 'http://'+document.domain;}
		else{document.location.href = 'so-sanh/'+val_new+'.htm';}
	});
	
	$(".click_ss_now").click(function() {
		var val_ht=$(this).attr("val_ht");
		var val_ss=$(this).attr("val_ss");
		if(val_ss!=0){document.location.href = 'so-sanh/'+val_ht+','+val_ss+'.htm';}
		else{document.location.href = 'so-sanh/'+val_ht+'.htm';}
	});
	
	$(".click_search").keyup(function() {
		var val_kieu=$("#search_kieu").val();
		var val_key=$(this).val();
		if(val_key!=''){
			var val_key=encodeURIComponent(val_key);
			$.get("librarys/content_load.php?kieu=7&val_kieu="+val_kieu+"&val_key="+val_key,function(data) {
				if(data!='0'){$(".search_load").html(data).show();}
				else{$(".search_load").hide();}
			});
		}else{$(".search_load").hide();}
	});
	
	/***********************************************************************************/	

	$(".close_ss").click(function(){
		var answer=confirm("Bạn có muốn xóa danh sách so sánh không?"); 
		if(answer){
			$.get('librarys/sec.php?kieu=2',function(data){
				location.reload();
			});	
		}
	});
	
	/***********************************************************************************/
	
	if($(".fcb_link_show_tskt").length){
		$('.fcb_link_show_tskt').fancybox({
			iframe : {
				css : {width : '1000px', height: '100%'}
			}
		});
	}
	
	if($(".fcb_goituvan").length){
		$('.fcb_goituvan').fancybox({
			iframe : {
				css : {width : '300px', height: '100px'}
			}
		});
	}
	
	if($(".fcb_ss").length){
		$('.fcb_ss').fancybox({
			iframe : {
				css : {width : '70%', height: '100%'}
			}
		});
	}
	
	$(".close_ss").click(function(e) {
		$('.o_ss').hide();
	});
	
	/***********************************************************************************/
	
	$(".show_filter_rwd").click(function(e) {
		$val_show_filter_rwd=$(".show_filter_rwd").attr("val_show_filter_rwd");
		if($val_show_filter_rwd==0){
			$(this).attr("val_show_filter_rwd",1);
			$('.sb_f_prod').addClass("active");
		}
	});
	$(".close_sb_prod_rwd").click(function() {
		$(".show_filter_rwd").attr("val_show_filter_rwd",0);
		$('.sb_f_prod').removeClass("active");
	});
	
	/***********************************************************************************/
	
	$(".icon_menu_mobile").click(function(e) {
		$val=$(".icon_menu_mobile").attr("val");
		if($val==0){
			$(".menu_mobile").attr("style","visibility: visible;");
			$(this).attr("val",1);
			$('body').addClass("show_menu_mobile");
		}
	});
	$(".close_menu_mobile").click(function() {
		$(".menu_mobile").removeAttr("style");
		$(".icon_menu_mobile").attr("val",0);
		$('body').removeClass("show_menu_mobile");
	});
	
	/***********************************************************************************/	
	
	var didScroll;
	var lastScrollTop = 0;
	var delta = 5;
	var navbarHeight = $('#header').outerHeight();
	
	$(window).scroll(function(event){
		didScroll = true;
	});
	
	setInterval(function() {
		if (didScroll) {
			hasScrolled();
			didScroll = false;
		}
	}, 0);
	
	function hasScrolled() {
		var st = $(this).scrollTop();
		
		if(Math.abs(lastScrollTop - st) <= delta)
			return;		
		if (st > lastScrollTop && st > navbarHeight){
			$('#header').removeClass('nav-down').addClass('nav-up');
			$('body').addClass('active');
		} else {
			if(st + $(window).height() < $(document).height()) {
				$('#header').removeClass('nav-up').addClass('nav-down');
				$('body').removeClass('active');
			}
		}
		
		lastScrollTop = st;
	}
	
});