// source --> https://jaroka.nl/wp-content/themes/jarokaV2/js/ajax.js?ver=6.9.4 
jQuery(function($){
 
	/*
	 * Load More
	 */
	$('#loadmore').click(function(){
 
		$.ajax({
			url : loadmore_params.ajaxurl, 
			data : {
				'action': 'loadmorebutton',
				'query': loadmore_params.posts, 
				'page' : loadmore_params.current_page
			},
			type : 'POST',
			beforeSend : function ( xhr ) {
				$('.cards').addClass('loading');
			},
			success : function( posts ){
				if( posts ) {
 
					$('.cards').removeClass('loading');
					$('#posts_wrap').append( posts );
					loadmore_params.current_page++;
 
					if ( loadmore_params.current_page == loadmore_params.max_page ) 
						$('#loadmore').hide();
 
				} else {
					$('#loadmore').hide();
				}
			},
			timeout: 15000
		});
		return false;
	});
 
	/*
	 * Filter
	 */
	$('#filters').change(function(){
 
		$.ajax({
			url : loadmore_params.ajaxurl,
			data : $('#filters').serialize(),
			dataType : 'json',
			type : 'POST',
			beforeSend : function(xhr){
				//$('#filters').find('button').text('Laden...');
				$('.cards').addClass('loading');
			},
			success : function( data ){
 
				loadmore_params.current_page = 1;
 
				loadmore_params.posts = data.posts;
 
				loadmore_params.max_page = data.max_page;
 
				//$('#filters').find('button').text('Filteren');
 				
 				$('.cards').removeClass('loading');
				$('#posts_wrap').html(data.content);
 
				if ( data.max_page < 2 ) {
					$('#loadmore').hide();
				} else {
					$('#loadmore').show();
				}
			},
			timeout: 15000
		});
 
		return false;
 
	});

	$('#reset').click(function(){
 
 		//$('.product-filter input').val('');
 		$('.product-filter select').prop('selectedIndex',0);

		$.ajax({
			url : loadmore_params.ajaxurl,
			data : $('#filters').serialize(),
			dataType : 'json',
			type : 'POST',
			beforeSend : function(xhr){
				//$('#filters').find('button').text('Laden...');
				$( "#loading-animation").show();
			},
			success : function( data ){
 
				loadmore_params.current_page = 1;
 
				loadmore_params.posts = data.posts;
 
				loadmore_params.max_page = data.max_page;
 
				//$('#filters').find('button').text('Filteren');
 
 				$("#loading-animation").hide();
				$('#posts_wrap').html(data.content);
 
				if ( data.max_page < 2 ) {
					$('#loadmore').hide();
				} else {
					$('#loadmore').show();
				}
			},
			timeout: 15000
		});
	 
		return false;
	});
 
});


(function($) {

    $.fn.displayPost = function() {

        event.preventDefault();

        var post_id = $(this).data("id");
        var id = "#" + post_id;

        // Check if the reveal modal for the specific post id doesn't already exist by checking for it's length
        if($(id).length == 0 ) {
            // We'll add an ID to the new reveal modal; we'll use that same ID to check if it exists in the future.
            var modal = $('<div>').attr('id', post_id ).addClass('reveal-modal').appendTo('.wrapper');
            var ajaxURL = loadmore_params.ajaxurl
             $.ajax({
                type: 'POST',
                url: ajaxURL,
                data: {"action": "load-content", post_id: post_id },
                success: function(response) {
                    modal.empty().html(response).append('<a class="close-reveal-modal">&#215;</a>').reveal('reveal', 'open');
                    modal.bind('opened', function() {
                        // Reset visibility to hidden and set display: none on closed reveal-modal divs, for some reason not working by default when reveal close is triggered on .secondary links  
                        $(".reveal-modal:not('.reveal-modal.open')").css({'visibility': 'hidden', 'display' : 'none'})
                        // Trigger resize 
                        $(window).trigger('resize');
                    return false;
                    });
                },
				 timeout: 15000
            });
        }
         //If the div with the ID already exists just open it.
         else {
             $(id).reveal('reveal', 'open');
         }

         // Recalculate left margin on window resize to allow for absolute centering of variable width elements
         // $(window).resize(function(){
         //     var left;
         //        left = Math.max($(window).width() - $(id).outerWidth(), 0) / 2;
         //        $(id).css({
         //            left:left + $(window).scrollLeft()
         //        });
         // });
    }

})(jQuery);

// Apply the function when we click on the .reveal link
jQuery(document).on("click", ".reveal,.secondary", function() {
    jQuery(this).displayPost();

});

// Close open modals via secondary paging links in open modal window
jQuery(document).on("click", ".secondary", function() {
    var id = jQuery(this).closest("div").attr("id");
        jQuery(id).reveal('reveal', 'close');
});