function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
$('#share-line').on( 'click', function(){
	var share_url =  $(location).attr("href");
	var share_text =  $('title').html();
	window.open('https://social-plugins.line.me/lineit/share?url=' + share_url + '&text= '+share_text, '_blank', 'width=520,height=420');
	return false;
});
$('#share-facebook').on( 'click', function(){  
	var share_url =  $(location).attr("href");
	var share_text =  $('title').html();
	window.open('//www.facebook.com/sharer/sharer.php?u=' + share_url + '&text= '+share_text, '_blank', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=520,width=420');
	return false;
});
$('#share-twitter').on( 'click', function(){  
	var share_url =  $(location).attr("href");
	var share_text =  $('title').html();
	window.open('//twitter.com/share?url=' + share_url + '&text= '+share_text, '_blank', 'width=520,height=300');
	return false; 
});
$('#share-email').on( 'click', function(){
	var share_url =  $(location).attr("href");
	var share_text =  $('title').html();
	window.open('//mail.google.com/mail/?tf=cm&fs=1&to=&su= '+ share_text + '&body= '+ share_text + ' : ' + share_url, '_blank', 'width=520,height=420');
	return false;
});
$('.share-copy').on( 'click', function(){
	var share_url =  $(location).attr("href");
	var share_text =  $('title').html();
	var temp = $("<input>");
	$("body").append(temp);
	temp.val(share_url).select();
	document.execCommand("copy");
	alert("Copy ลิงค์สำหรับแชร์ เรียบร้อยแล้ว");
	temp.remove();
	return false;
});
var $el, $ps, $up, totalHeight;
$(".content-readmore .btn").click(function () {
	totalHeight = 0
	$el = $(this);
	$p = $el.parent();
	$up = $('.box-content');
	$ps = $up.find("p:not('.box-content')");
	$ps.each(function () {
		totalHeight += $(this).css("margin-bottom");
	});
	$up
			.css({
				"height": "auto",
				"max-height": 99999,
				"overflow": "visible"
			})
	$p.fadeOut();
	return false;
});
/*$('.image-gallery').each(function() {
    var selector = $(this),
    popup = selector.find('.gallery-icon > img, .gallery-icon > .mySwipercontent > .swiper-wrapper > .swiper-slide > img, .gallery-icon > p > img, img.photo-item, .image-gallery > .gallery-icon > .MsoNormal img');
    popup.each(function() {
        $(this).wrap('<a href="' + $(this).attr('src') + '" class="overlay-wrapper"></a>');
        $(this).parent().append('<div class="overlay"><i class="ot-flaticon-add"></i></div>');
    });
    selector.lightGallery({
        selector: '.overlay-wrapper',
        share: false,
        pager: true,
        download: false,
    });
});*/
$('.image-gallery').each( function(){
	var selector = $(this),
	popup   = selector.find('.gallery-icon > a, .gallery-icon > .mySwipercontent > .swiper-wrapper > .swiper-slide > a, .gallery-icon > p > a, a.photo-item, .image-gallery > .gallery-icon > .MsoNormal');
	popup.append('<a href="#" class="overlay"><i class="ot-flaticon-add"></i></a>');
	selector.lightGallery({
		selector: popup,
		share: false,
		pager: true, 
		download: false, 
	});
});
$("#btnloadmoredt").click(function(){
	var pgno = +$("#pageno").val() + 1;
	var pgno = (pgno-2);
	var maxpgno = $("#maxpageno").val();
	if(pgno == maxpgno){
		$("#btnloadmoredt").hide();
	}
});
$("#btnloadmores").click(function(){
	var spgno = +$("#spageno").val() + 1;
	var spgno = (spgno - 2);
	var smaxpgno = $("#saxpageno").val();
	if(spgno == smaxpgno){
		$("#btnloadmores").hide();
	}
});
$(document).ready(function () {
	$("#table").hide();
	$("#ta3").hide();
	$("#ta4").hide();
	$("#ta5").hide();
	$("#ta6").hide();
	$("#ta7").hide();
	$("#menu_0").click(function () {
		$("#fdatron2").show();
		$("#ta3").hide();
		$("#ta4").hide();
		$("#ta5").hide();
		$("#ta6").hide();
		$("#ta7").hide();
		$("#menu_1").removeClass('active');
		$("#menu_2").removeClass('active');
		$("#menu_3").removeClass('active');
		$("#menu_4").removeClass('active');
		$("#menu_5").removeClass('active');
	})
	$("#menu_1").click(function () {
		$("#ta3").show();
		$("#fdatron2").hide();
		$("#ta4").hide();
		$("#ta5").hide();
		$("#ta6").hide();
		$("#ta7").hide();
		$("#menu_0").removeClass('active');
		$("#menu_2").removeClass('active');
		$("#menu_3").removeClass('active');
		$("#menu_4").removeClass('active');
		$("#menu_5").removeClass('active');
	})
	$("#menu_2").click(function () {
		$("#fdatron2").hide();
		$("#ta3").hide();
		$("#ta4").show();
		$("#ta5").hide();
		$("#ta6").hide();
		$("#ta7").hide();
		$("#menu_0").removeClass('active');
		$("#menu_1").removeClass('active');
		$("#menu_3").removeClass('active');
		$("#menu_4").removeClass('active');
		$("#menu_5").removeClass('active');
	})
	$("#menu_3").click(function () {
		$("#fdatron2").hide();
		$("#ta3").hide();
		$("#ta4").hide();
		$("#ta5").show();
		$("#ta6").hide();
		$("#ta7").hide();
		$("#menu_0").removeClass('active');
		$("#menu_2").removeClass('active');
		$("#menu_4").removeClass('active');
		$("#menu_1").removeClass('active');
		$("#menu_5").removeClass('active');
	})
	$("#menu_4").click(function () {
		$("#fdatron2").hide();
		$("#ta3").hide();
		$("#ta4").hide();
		$("#ta5").hide();
		$("#ta6").show();
		$("#ta7").hide();
		$("#menu_0").removeClass('active');
		$("#menu_2").removeClass('active');
		$("#menu_3").removeClass('active');
		$("#menu_1").removeClass('active');
		$("#menu_5").removeClass('active');
	})
	$("#menu_5").click(function () {
		$("#fdatron2").hide();
		$("#ta3").hide();
		$("#ta4").hide();
		$("#ta5").hide();
		$("#ta6").hide();
		$("#ta7").show();
		$("#menu_0").removeClass('active');
		$("#menu_2").removeClass('active');
		$("#menu_3").removeClass('active');
		$("#menu_4").removeClass('active');
		$("#menu_1").removeClass('active');
	})
	$("#btntable").click(function () {
		$("#btnsummary").removeClass('active');
		$("#btntable").addClass('active');
		$("#table").show();
		$("#summary").hide();
	})
	$("#btnsummary").click(function () {
		$("#btntable").removeClass('active');
		$("#btnsummary").addClass('active');
		$("#table").hide();
		$("#summary").show();
	})
});
$("#reset").on("click", function () {
	$('.top').removeClass('active');
	$('.top').prop("checked", false);
	$('#price_max').prop('disabled', false);
	$('.btn-spower').removeClass('active');
	$('.btn-spowerm').removeClass('active');
});
$("#aeasy,#amain,#abudget,#reset-brand").on("click", function () {
	$('.brand-input').prop("checked", false);
});
$("#btn-main").click(function(){
	$('#btn-main').html('<i class="fa fa-spinner fa-spin"></i> กรุณารอสักครู่ ...').fadeIn();
	setTimeout(function () {
		$('#btn-main').html('Submit');
	}, 600);
});
$("#btn-easy").click(function(){
	$("#btn-easy").html('<i class="fa fa-spinner fa-spin"></i> กรุณารอสักครู่ ...').fadeIn();
	setTimeout(function () {
		$('#btn-easy').html('Submit');
	}, 200);
});
for (var i = 0; i < 15 ;i++){
	$("#budgetm_"+ i).click(function(){
			for (var j = 0; j < 15 ;j++){
				$('#budgetm_'+ j).addClass('opa');
			}
			setTimeout(function () {
				for (var j = 0; j < 15 ;j++){
					$('#budgetm_'+ j).addClass('opa-1');
				}
			}, 1200);
		$('#spin').html('<i class="fas fa-spinner"></i>').fadeIn();
		setTimeout(function () {
			$('#spin').html('<i class="fas fa-spinner"></i>').fadeOut();
		}, 200);
	});
	$("#budget_"+ i).click(function(){
		$('#spin').html('<i class="fas fa-spinner"></i>').fadeIn();
		for (var j = 0; j < 15 ;j++){
			$("#budget_" + j).addClass('opa');
		}
	});
}
var btn = $('#totop');
$(window).scroll(function () {
	if ($(window).scrollTop() > 300) {
		btn.addClass('show');
	} else {
		btn.removeClass('show');
	}
});
btn.on('click', function (e) {
	e.preventDefault();
	$('html, body').animate({scrollTop: 0}, '300');
});
// GA Tracking
$("#btnloadmore").click(function() {
	var share_url =  $(location).attr("href");
	var share_text =  $('title').html(); 
	gtag('event','article_readmore',{
		'event_category': 'click',
		'event_label':'Read More Article',
		'value': share_url,
	});
});
$("#btnloadmoref").click(function() {
	var share_url =  $(location).attr("href");
	var share_text =  $('title').html();
	gtag('event','readmore',{
		'event_category': 'click',
		'event_label':'Read More Article',
		'value': share_url,
	});
});
$(".tags").click(function() {
	var text = $(this).text();
	gtag('event','article_tags',{
		'event_category': 'click',
		'event_label':'Read More Tags',
		'value': text,
	}); 
});
$(".photo-popup").click(function() {
	var share_url =  $(location).attr("href");
	gtag('event','article_photo_popup',{
		'event_category': 'click',
		'event_label':'View Photo Popup',
		'value': share_url,
	}); 
});
$(".ads").click(function() {
	var share_url =  $(this).attr("href");
	var share_text =  $(this).attr("title");
	gtag('event','ads_click',{
		'event_category': 'ads',
		'event_label':share_text,
		'value': share_url,
	});  
});
$(".key-search").click(function() {
	var share_url =  $(this).attr("href");
	var share_text =  $(this).attr("title");
	gtag('event','search_click',{
		'event_category': 'search',
		'event_label':share_text,
		'value': share_url,
	});  
});
$("#navbarSearch").click(function() {
	var share_url =  $(this).attr("href");
	var share_text =  $(this).attr("title");
	console.log(share_url);
	gtag('event','search_click',{
		'event_category': 'search',
		'event_label':share_text,
		'value': share_url,
	});  
});
function priceselect() {
    var min = $('#price_min').val();
    var max = $('#price_max');
        min = parseInt(min, 10);
        max.children().each(function() {
			var v = parseInt($(this).val(), 10);
			if(!isNaN(v) && v <= min) {
				$(this).prop('disabled', true);
			}
        });
		max.val('');
}
$('#price_min').change(priceselect);
priceselect();
function engineselect() {
    var min = $('#engine_min').val();
    var max = $('#engine_max');
        min = parseInt(min, 10);
        max.children().each(function() {
			var v = parseInt($(this).val(), 10);
			if(!isNaN(v) && v <= min) {
				$(this).prop('disabled', true);
			}
        });
		max.val('');
}
$('#engine_min').change(engineselect);
engineselect();
function bodyselect(x, ka) {
	$('#b_' + x).toggleClass('active');
	if ($('#b_' + x).hasClass('active')) {
		$('#v_' + x).val(ka);
	} else {
		$('#v_' + x).val('');
	}
}
function sfp(x, ka) {
	$('#b_' + x).toggleClass('active');
	if ($('#b_' + x).hasClass('active')) {
		$('#v_' + x).val(ka);
	} else {
		$('#v_' + x).val('');
	}
}
function cookies(){
	$.post( "/", { ac: "cookies" })
	  .done(function( data ) {
		$('#cookies').hide();
	  });
}
function setCookie(cname, cvalue, exdays) {
	var d = new Date();
	d.setTime(d.getTime() + (exdays*24*60*60*1000));
	var expires = "expires="+d.toUTCString();
	document.cookie = cname + "=" + cvalue + "; " + expires;
	document.getElementById("cookies").remove("cookies");
}
function urlreplace(url) {
	return url.split(' ').join('+');
}
function editor(x) {
	if (x != '') {
		$('#menu_' + x).addClass('active');
	} else {
		$("#menu_0").removeClass('active');
		$("#menu_1").removeClass('active');
		$("#menu_2").removeClass('active');
		$("#menu_3").removeClass('active');
		$("#menu_4").removeClass('active');
		$("#menu_5").removeClass('active');
	}
	var txt = $('#n' + x).val();
	$("#neditor").html(txt);
}
function ckselectbrand() {
	var totalSeen = $("input.brand-input:checked").length;
	if (totalSeen != 0) {
		$("#ck-brand").html('<i class="fa fa-spinner fa-spin"></i> กรุณารอสักครู่ ...').fadeIn();
		setTimeout(function () {
			$('#ck-brand').html('Submit');
		}, 200);
		this.form.submit();
	} else {
		alert("กรุณาเลือกเเบรนด์ที่สนใจ");
		return false;
	}
}
$('.img-responsive.relate').css({
    'object-fit' : 'cover',
    'width' : '100%',
   'height' : '147px'
});
var showyear =$("#showyear").val();
var showyear2 =$("#showyear2").val();
var month =$("#showmonth").val();
var month2 =$("#showmonth2").val();
$(document).ready(function(){
	if(showyear){
		$("#showyear2 option[value=" + showyear + "]").prop('disabled', true);;
	}
	if(month){
		$("#showmonth2 option[value=" + month + "]").prop('disabled', true);;
	}
})
$('select.year').change(function () {
	$('option').prop('disabled', false);
	$('select.year').each(function () {
		var $loopSelect = $(this);
		if ($loopSelect.val()) {
			$('select')
					.not($loopSelect)
					.find('option[value="' + $loopSelect.val() + '"]')
					.prop('disabled', true);
		}
	});
});
var select1 = $("#showmonth");
var select2 = $("#showmonth2");
select1
.change(function () {
  var select1Value = parseInt($(this).val());
  select2.children().each(function () {
	$(this).prop('disabled', false);
  });
  select2.children().filter(function () {
	return $(this).attr("value") <= select1Value;
  }).each(function () {
	$(this).prop('disabled', true);
  });
}).change();
$("#othseach").click(function () {
	$('#salescustom').toggle(1000);
	$('#othseach .feather-chevron-down').toggleClass("rotate");
});
function urlreplace(url) {
	return url.split(' ').join('+');
}
function cksalse() {
	var fm = document.scar;
	if (document.getElementById('showcarcate').value == "") {
		alert('กรุณาเลือกประเภทรถยนต์');
		document.getElementById("showcarcate").focus();
		return false;
	} else if (document.getElementById('showmonth').value == "") {
		alert('กรุณาเลือกเดือน');
		document.getElementById("showmonth").focus();
		return false;
	} else if (document.getElementById('showyear').value == "") {
		alert('กรุณาเลือกปี');
		document.getElementById("showyear").focus();
		return false;
	} else if (document.getElementById('showyear2').value == "") {
		alert('กรุณาเลือกปีที่ต้องการเปรียบเทียบ');
		document.getElementById("showyear2").focus();
		return false;
	} else {
		return true;
	}
}
function cksalset() {
	var fm = document.scart;
	if (document.getElementById('showmontht').value == "") {
		alert('กรุณาเลือกเดือน');
		document.getElementById("showmontht").focus();
		return false;
	} else if (document.getElementById('showyeart').value == "") {
		alert('กรุณาเลือกปี');
		document.getElementById("showyeart").focus();
		return false;
	} else {
		return true;
	}
}
function formatCurrency(number) {
	number = parseFloat(number);
	return number.toFixed(2).replace(/./g, function (c, i, a) {
		return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c;
	});
}
$('#salescustom').hide();
$('#collapse2').hide();
$('#collapse3').hide();
$('#collapse4').hide();
$('#collapse5').hide();
$('#collapse6').hide();
$('#collapse7').hide();
$('#collapse8').hide();
$('#collapse9').hide();
$('#collapse10').hide();
$(document).ready(function () {
	$('#total_vehicle_sales_1').click(function () {
		$('#collapse1').toggle();
	});
	$('#total_vehicle_sales_2').click(function () {
		$('#collapse2').toggle();
	});
	$('#total_vehicle_sales_3').click(function () {
		$('#collapse3').toggle();
	});
	$('#total_vehicle_sales_4').click(function () {
		$('#collapse4').toggle();
	});
	$('#total_vehicle_sales_5').click(function () {
		$('#collapse5').toggle();
	});
	$('#total_vehicle_sales_6').click(function () {
		$('#collapse6').toggle();
	});
	$('#total_vehicle_sales_7').click(function () {
		$('#collapse7').toggle();
	});
	$('#total_vehicle_sales_8').click(function () {
		$('#collapse8').toggle();
	});
	$('#total_vehicle_sales_9').click(function () {
		$('#collapse9').toggle();
	});
	$('#total_vehicle_sales_10').click(function () {
		$('#collapse10').toggle();
	});
});
function urlreplace(url) {
	return url.split(' ').join('+');
}
$(function () {
	$('input[type="text"]').change(function () {
		this.value = $.trim(this.value);
	});
});
function searchSubmit() {
	var nsearch = document.getElementById('nsearch');
	if (nsearch.value == "") {
		alert('กรุณาใส่คำที่ต้องการค้นหา');
		document.getElementById('nsearch').focus();
		return false;
	} else if (nsearch.value.length < 3) {
		alert('กรุณาป้อนตัวอักษรอย่างน้อย 3 ตัวอักษรขึ้นไป');
		return;
	} else {
		var uri = document.getElementById('nsearch').value;
		location.href = 'search/' + urlreplace(uri.replace(/^\s+|\s+$/g, ''));
	}
}
function searchSubmittest() {
	var nsearch = document.getElementById('nsearchtest');
	if (nsearch.value == "") {
		alert('กรุณาใส่คำที่ต้องการค้นหา');
		document.getElementById('nsearchtest').focus();
		return false;
	} else if (nsearch.value.length < 3) {
		alert('กรุณาป้อนตัวอักษรอย่างน้อย 3 ตัวอักษรขึ้นไป');
		return;
	} else {
		var uri = document.getElementById('nsearchtest').value;
		location.href = 'search_test/' + urlreplace(uri.replace(/^\s+|\s+$/g, ''));
	}
}
function searchSubmitm() {
	var nsearchm = document.getElementById('nsearchm');
	if (nsearchm.value == "") {
		alert('กรุณาใส่คำที่ต้องการค้นหา');
		document.getElementById('nsearchm').focus();
		return false;
	} else if (nsearchm.value.length < 3) {
		alert('กรุณาป้อนตัวอักษรอย่างน้อย 3 ตัวอักษรขึ้นไป');
		return;
	} else {
		var uri = document.getElementById('nsearchm').value;
		location.href = 'search/' + urlreplace(uri.replace(/^\s+|\s+$/g, ''));
	}
}
$(document).ready(function(){
	$(".btn-loadmore").click(function(){
		$("#read-more").hide();
	});
});
//*$(".box-content img").prop("alt", $(".content_header").text());
$(".box-content a").prop("rel", "nofollow");
$(".box-content img").each(function() {
	//$(this).attr("data-lazy-src",$(this).attr("src"));
	//$(this).removeAttr("src");
	//$(this).attr("src", "/images/grey.gif");
});
function buyertab(dev) {
	$('#smain').hide();
	$('#seasy').hide();
	$('#sbrand').hide();
	$('#sbudget').hide();
	$('#s' + dev).show();
	$('#amain').removeClass('active');
	$('#aeasy').removeClass('active');
	$('#abrand').removeClass('active');
	$('#abudget').removeClass('active');
	$('#a' + dev).addClass('active');
}
