function AjaxShopNav(Url, IncludeFilters) {

    $('html, body').animate({
		scrollTop: $("main").offset().top-50
	}, 450);

	$(".ProductListLoader").css("display", "flex");

	$.ajax({
		type: "GET",
		dataType: "json",
		data: (IncludeFilters ? $(".ProductFilters").serialize() : {}),
		url: Url,
		success: function(response) {

			window.history.pushState('','', Url+(IncludeFilters ? "&"+$(".ProductFilters").serialize() : ''));

			setTimeout(function () {
				$(".ProductListLoader").css("display", "none");
			}, 300);

			if(typeof response["ProductList"] !== 'undefined') {

				$("#Products_Lister").html(response['ProductList']);

				if($(window).outerWidth() < 992) {
					$("#Filter_Category").removeClass("show");
					$(".FilterGroupBtn[data-target='#Filter_Category']").attr("aria-expanded", "false");
					$(".FilterGroupBtn[data-target='#Filter_Category']").addClass("collapsed");
				}
				
				if($("#Filter_Price input[name='filters[min_price]']").val() == '' && $("#Filter_Price input[name='filters[max_price]']").val() == '') {
					$("#Filter_Price").removeClass("show");
					$(".FilterGroupBtn[data-target='#Filter_Price']").attr("aria-expanded", "false");
					$(".FilterGroupBtn[data-target='#Filter_Price']").addClass("collapsed");
				}
				
				if($("#Filter_Color input[type='checkbox']:checked").length < 1) {
					$("#Filter_Color").removeClass("show");
					$(".FilterGroupBtn[data-target='#Filter_Color']").attr("aria-expanded", "false");
					$(".FilterGroupBtn[data-target='#Filter_Color']").addClass("collapsed");
				}
	
				if($("#Filter_Size input[type='checkbox']:checked").length < 1) {
					$("#Filter_Size").removeClass("show");
					$(".FilterGroupBtn[data-target='#Filter_Size']").attr("aria-expanded", "false");
					$(".FilterGroupBtn[data-target='#Filter_Size']").addClass("collapsed");
				}

			} else if(typeof response["response"] !== "undefined" && response['responseMethod'] == "Error") {

				bootbox.alert('<div class="alert alert-danger" style="margin: 25px 0;">'+response["response"]+'</div>');

			} else {

				bootbox.alert('<div class="alert alert-danger" style="margin: 25px 0;">Ismeretlen hiba történt a termékek listázásakor.</div>');

			}

		},
		error: function(xhr, ajaxOptions, thrownError) {
			
			$(".ProductListLoader").css("display", "none");

			bootbox.alert('<div class="alert alert-danger" style="margin: 25px 0;">'+xhr.responseText+'</div>');

		}
	});

}

$(document).on("click", ".products-list .pagination .page-link", function () {
	AjaxShopNav($(this).attr("href"), true);
	return false;
});

$(document).on("click", ".products-list .filter-category-tree li a", function () {
	AjaxShopNav($(this).attr("href"));
	return false;
});

$(document).on("change", ".ProductFilters input", function () {

	var qs = URI(window.location.pathname+window.location.search).query(true);

	if(qs.s)
		var CurrentUrl = "/shop/list?s="+qs.s;
	else
		var CurrentUrl = "/shop/list?c="+qs.c;

	if(typeof qs.o !== 'undefined')
		CurrentUrl += "&o="+qs.o;

	if(typeof qs.l !== 'undefined')
		CurrentUrl += "&l="+qs.l;
	
	AjaxShopNav(CurrentUrl, true);
	return false;
});

$(window).on("load", function () {

	if($(window).outerWidth() < 992) {
		$("#Filter_Category").removeClass("show");
		$(".FilterGroupBtn[data-target='#Filter_Category']").attr("aria-expanded", "false");
		$(".FilterGroupBtn[data-target='#Filter_Category']").addClass("collapsed");
	}
	
	if($("#Filter_Price input[name='filters[min_price]']").val() == '' && $("#Filter_Price input[name='filters[max_price]']").val() == '') {
		$("#Filter_Price").removeClass("show");
		$(".FilterGroupBtn[data-target='#Filter_Price']").attr("aria-expanded", "false");
		$(".FilterGroupBtn[data-target='#Filter_Price']").addClass("collapsed");
	}
	
	if($("#Filter_Color input[type='checkbox']:checked").length < 1) {
		$("#Filter_Color").removeClass("show");
		$(".FilterGroupBtn[data-target='#Filter_Color']").attr("aria-expanded", "false");
		$(".FilterGroupBtn[data-target='#Filter_Color']").addClass("collapsed");
	}
	
	if($("#Filter_Size input[type='checkbox']:checked").length < 1) {
		$("#Filter_Size").removeClass("show");
		$(".FilterGroupBtn[data-target='#Filter_Size']").attr("aria-expanded", "false");
		$(".FilterGroupBtn[data-target='#Filter_Size']").addClass("collapsed");
	}

});