jQuery(function($){
	$('body').on('click', '.loadmore button', function() {
 
		var button = $(this);
		
		// Increment page BEFORE sending the request
		chic_lifestyle_loadmore_params.current_page++;
		
		var data = {
			'action': 'chic_lifestyle_loadmore',
			'page' : chic_lifestyle_loadmore_params.current_page,
			'cat' : chic_lifestyle_loadmore_params.cat,
			'view' : button.attr('view'),
			'context' : chic_lifestyle_loadmore_params.context
		};
 
		$.ajax({
			url : chic_lifestyle_loadmore_params.ajaxurl,
			data : data,
			type : 'POST',
			beforeSend : function ( xhr ) {
				button.text('Loading...');
			},
			success : function( data ) {
				if( data ) {
					$( 'div.blog-list-block' ).append(data);
					button.text( 'More Posts' );
 
					if ( chic_lifestyle_loadmore_params.current_page == chic_lifestyle_loadmore_params.max_page ) { 
						button.remove();
					}
				} else {
					// No data returned, decrement back
					chic_lifestyle_loadmore_params.current_page--;
					button.remove();
				}
			},
			error: function() {
				// On error, decrement back
				chic_lifestyle_loadmore_params.current_page--;
				button.text( 'More Posts' );
			}
		});
	});
});