

jQuery(document).ready(function($) {
	$(".tbang-loading").fadeOut();

	// AOS.init({
	// 	easing: 'ease-out-back',
	// 	duration: 1000
	// });

	$('.list-new-cat').slick({
		rows: 2,
		dots: false,
		arrows: true,
		infinite: true,
		speed: 300,
		slidesToShow: 6,
		slidesToScroll: 6,
		responsive: [
			{
				breakpoint: 1024,
				settings: {
				slidesToShow: 4,
				slidesToScroll: 4,
				infinite: true,
				dots: true
				}
			},
			{
				breakpoint: 600,
				settings: {
				slidesToShow: 2,
				slidesToScroll: 2
				}
			},
		]		
	});	

	$('.list-slider').slick({
		fade: true,
		infinite: true,
		autoplay: true,
		arrows: false,
		autoplay: true,
		dots: true
	});

	$('.slider-for').slick({
		slidesToShow: 1,
		slidesToScroll: 1,
		arrows: false,
		fade: true,
		asNavFor: '.slider-nav'
	});
	
	$('.slider-nav').slick({
		slidesToShow: 4,
		slidesToScroll: 1,
		asNavFor: '.slider-for',
		dots: false,
		arrows: false,
		focusOnSelect: true
	});	

	$('#main-menu').slick({
		slidesToShow: 7,
		arrows: false,
		autoplay: true,
		dots: false,
		responsive: [
			{
				breakpoint: 1024,
				settings: {
					slidesToShow: 5,
				}
			},
			{
				breakpoint: 600,
				settings: {
					slidesToShow: 3,
				}
			},
		]		
	});	

	$( '.list-sort span' ).on( 'click', function( e ){
		let data_sort = $( this ).data( 'sort' );
		$( '.list-sort span' ).removeClass( 'active' );
		$( this ).addClass( 'active' );

		location.href = URL_add_parameter( location.href, 'sort', data_sort );
	} )


	/**
     * Ajax Suggested search
     */
	let typingTimer;
	let doneTypingInterval  = 500;
	let $input              = $( '.search-form .input-search' );

	$input.on('keyup', function () {
		_this = $( this );
		clearTimeout( typingTimer );
		typingTimer = setTimeout( function(){ doneTyping( _this ) }, doneTypingInterval );
	});

	$input.on('keydown', function () {
		clearTimeout(typingTimer);
	});

	function doneTyping( _this ) {
		
		let data = _this.val();
		let quicklinks  = _this.closest('.search-bar').find( '#search-result' );
		$( '.search-bar' ).addClass( 'loading' );

		$.ajax({
			type: "POST",
			url: tbang.ajaxurl,
			data: { action: 'global_search_ajax', data: data },
			dataType: "JSON",
			success: function (res) {
				render_search_result( res.result, quicklinks );
				$( '.search-bar' ).removeClass( 'loading' );
			}
		});
	}

	function render_search_result( data, field ){
		if( ! data ){
			field.html( '<div class="item">Không tìm thấy sản phẩm.</a>' ); 
			return;
		}

		let html_field = '';

		data.forEach( function( item ) {
			html_field += '<a class="item" href="' + item.url + '" target="' + item.target + '"> ' + item.label + ' </a>';			
		});

		field.html( html_field );
	}

	$input.on( 'focus', function( e ){
		$( '.search-bar' ).addClass( 'ajax-search-active' );
	})


    $(document).on( 'click', function(e){
        var container = $( '.search-bar' );

        if ( ! container.is( e.target ) && container.has( e.target ).length === 0) {
			$( '.search-bar' ).removeClass( 'ajax-search-active' );
        }
    })

	$( '.tivi-item' ).each( function( e ){
		let url = $( this ).find( 'iframe' ).attr( 'src' );
		$( this ).find( '.video-wrap' ).attr( 'video-url', url );
	} )

	var $videoSrc;
	$( '.video-wrap' ).click(function() {
		$videoSrc = $( this ).attr( 'video-url' );
	})

	$( '.modal-video' ).on( 'shown.bs.modal', function(e) {
		$( '.video-youtube-wrap iframe' ).attr( 'src' , $videoSrc );
	})

	$( '.modal-video' ).on( 'hide.bs.modal', function(e) {
		$( '.video-youtube-wrap iframe' ).attr( 'src' , "" ); 
	});	
});

function URL_add_parameter(url, param, value){
    var hash       = {};
    var parser     = document.createElement('a');

    parser.href    = url;

    var parameters = parser.search.split(/\?|&/);

    for(var i=0; i < parameters.length; i++) {
        if(!parameters[i])
            continue;

        var ary      = parameters[i].split('=');
        hash[ary[0]] = ary[1];
    }

    hash[param] = value;

    var list = [];  
    Object.keys(hash).forEach(function (key) {
        list.push(key + '=' + hash[key]);
    });

    parser.search = '?' + list.join('&');
    return parser.href;
}

// $ = jQuery;
// $(window).bind('scroll', function () {
// 	if( $(window).scrollTop() > 44 ) {
// 		$( '.site-header' ).addClass( 'is_fixed' );
// 	}else{
// 		$( '.site-header' ).removeClass( 'is_fixed' );
// 	}
// });

// $(window).on('load', function () {
// 	AOS.refresh();
// });

// $(function () {
// 	AOS.init({
// 		easing: 'ease-out-back',
// 		duration: 1200
// 	});
// });