<script type="text/javascript" id="digitcart-load-more-products-js-script">
$(document).ready(function(){
	var disableMobile = <?php echo $dc_disable_mobile; ?>;
	if(disableMobile && $(window).width() < 768){
		console.log('Load more products is disabled on mobiles.');
	} else {
		if($('<?php echo $dc_item_selector; ?>').length){
			var paginationActive = <?php echo $dc_pagination_status; ?>;
			var wrapNewProducts = <?php echo $dc_wrap_status; ?>;
			if(!paginationActive){
				$<?php echo $dc_pagination_row; ?>.remove();
			}
			var threshold = <?php echo $threshold; ?>;
			var wrapCount = <?php echo $wrap_count; ?>;
			var wrapperClass = '<?php echo $wrapper_class; ?>';
			var page = <?php echo $page; ?>;
			var limit = <?php echo $limit; ?>;
			var totalProducts = <?php echo $product_total; ?>;
			var remainingProductsFixed = totalProducts - (limit * page);
			var remainingProducts = remainingProductsFixed;
			var totalPages = <?php echo $product_total; ?> / <?php echo $limit; ?>;
			var totalPages = Math.ceil(totalPages);
			var delay = <?php echo $dc_delay; ?>;
			var processing = false;
			var LoadWithButton = <?php echo $dc_button_load; ?>;
			if(LoadWithButton){
				if((totalPages - page) > 0){
					var dcModifiedUrl = updateURLParameter(window.location.href, 'page', (page + 1));
					dcModifiedUrl = dcModifiedUrl + ' <?php echo $dc_item_selector; ?>';
					if(wrapNewProducts){
						$('<?php echo $dc_item_selector; ?>').last().parent().after('<div id="dcloading-button" class="clearfix" style="clear:both;text-align:center;width:100%;"><a data-href="' + dcModifiedUrl + '"><?php echo $dc_load_more_text; ?></a></div>');
					} else {
						$('<?php echo $dc_item_selector; ?>').last().after('<div id="dcloading-button" class="clearfix" style="clear:both;text-align:center;width:100%;"><a data-href="' + dcModifiedUrl + '"><?php echo $dc_load_more_text; ?></a></div>');
					}
				}
			} else {
				$(window).on('scroll', function(){
					var productDiv = $('<?php echo $dc_item_selector; ?>');
					var classes = productDiv.attr('class');
					var lastProduct = productDiv.last();
					var lastProductOffsetTop = lastProduct.offset().top + threshold;
					var windowScrollTop = $(window).scrollTop();
					var windowHeight = $(window).height();
					var documentHeight = $(document).height();
					if(page < totalPages){
						if(windowScrollTop > lastProductOffsetTop && remainingProducts > 0 || (documentHeight - windowHeight) == windowScrollTop && remainingProducts > 0){
							if (!processing){
								processing = true;
								var dcModifiedUrl = updateURLParameter(window.location.href, 'page', (page + 1));
								page += 1;
								lastProduct.after('<div id="dcloading-icon" class="clearfix" style="clear:both;width:100%;"><?php echo $dc_loading_icon; ?></div>');
								dcModifiedUrl = dcModifiedUrl + ' <?php echo $dc_item_selector; ?>';
								$.get(dcModifiedUrl, function(data) {
									var data = $(data);
									var newProds = data.find('<?php echo $dc_item_selector; ?>');
									newProds.each(function(){
										$(this)
										.removeAttr('class')
										.addClass(classes + ' dc-new-product dc-pre-wrapped <?php echo $dc_css_classes; ?>');
										if(delay){
											$(this).css({
												animationDelay: delay + 'ms'
											});
										}
									});
									$('#dcloading-icon').remove();
									if($('[data-toggle="tooltip"]').length){
										$('[data-toggle="tooltip"]').tooltip({container: 'body'});
									}
									if(wrapNewProducts){
										lastProduct.parent().after(newProds);
										var preWrapProducts = $('.dc-pre-wrapped');
										for(var i = 0; i < preWrapProducts.length; i+= wrapCount) {
											preWrapProducts.slice(i, i + wrapCount).wrapAll('<div class="' + wrapperClass + ' dc-new-wrapper"></div>');
										}
										$('.dc-pre-wrapped').removeClass('dc-pre-wrapped');
									} else {
										lastProduct.after(newProds);
									}
									if(paginationActive){
										$<?php echo $dc_pagination_row; ?>.replaceWith(data.find<?php echo $dc_pagination_row; ?>);
									}
									<?php if($custom_js){ echo $custom_js; } ?>
									processing = false;
								});
							}
						}
					} else {
						if(!processing){
							lastProduct.after('<div id="dcloading-icon" class="clearfix" style="clear:both;width:100%;"><?php echo $dc_finished_text; ?></div>');
							processing = true;
						}
					}
				});
			}
			$('#dcloading-button a').on('click', function(){
				var btn = $(this);
				var url = btn.attr('data-href');
				var productDiv = $('<?php echo $dc_item_selector; ?>');
				var lastProduct = productDiv.last();
				var classes = productDiv.attr('class');
				$.get(url, function(data) {
					var data = $(data);
					var newProds = data.find('<?php echo $dc_item_selector; ?>');
					newProds.each(function(){
						$(this)
						.removeAttr('class')
						.addClass(classes + ' dc-new-product dc-pre-wrapped <?php echo $dc_css_classes; ?>');
						if(delay){
							$(this).css({
								animationDelay: delay + 'ms'
							});
						}
					});
					$('#dcloading-icon').remove();
					if($('[data-toggle="tooltip"]').length){
						$('[data-toggle="tooltip"]').tooltip({container: 'body'});
					}
					if(wrapNewProducts){
						lastProduct.parent().after(newProds);
						var preWrapProducts = $('.dc-pre-wrapped');
						for(var i = 0; i < preWrapProducts.length; i+= wrapCount) {
							preWrapProducts.slice(i, i + wrapCount).wrapAll('<div class="' + wrapperClass + ' dc-new-wrapper"></div>');
						}
						$('.dc-pre-wrapped').removeClass('dc-pre-wrapped');
					} else {
						lastProduct.after(newProds);
					}
					if(paginationActive){
						$<?php echo $dc_pagination_row; ?>.replaceWith(data.find<?php echo $dc_pagination_row; ?>);
					}
					<?php if($custom_js){ echo $custom_js; } ?>
					page += 1;
					btn.attr('data-href', updateURLParameter(btn.attr('data-href'), 'page', (page + 1)));
					if(page == totalPages){
						$('#dcloading-button').replaceWith('<div id="dcloading-icon" class="clearfix" style="clear:both;width:100%;"><?php echo $dc_finished_text; ?></div>');
					}
				});
			});
		}
	}
});
function updateURLParameter(url, param, paramVal){
	var newAdditionalURL = "";
	var tempArray = url.split("?");
	var baseURL = tempArray[0];
	var additionalURL = tempArray[1];
	var temp = "";
	if (additionalURL) {
		tempArray = additionalURL.split("&");
		for (var i=0; i<tempArray.length; i++){
			if(tempArray[i].split('=')[0] != param){
				newAdditionalURL += temp + tempArray[i];
				temp = "&";
			}
		}
	}
	var rows_txt = temp + "" + param + "=" + paramVal;
	return baseURL + "?" + newAdditionalURL + rows_txt;
}
</script>