;/*Load More Post*/( function ($) {
	'use strict';
	$( document ).ready(
		function () {
			if ($( ".eca-posts-load-more" ).length > 0) {
				$( document ).on(
					"click",
					".posts-load-more",
					function (e) {
						e.preventDefault();
						var $this       = $( this ),
						settings        = $this.data( 'settings' ),
						postsoffset     = settings.postsOffset,
						numberOfPosts   = settings.numberOfPosts,
						totalPage       = settings.totalPage,
						page            = $this.attr( "data-page" ),
						loadMorePost    = settings.loadMorePost,
						loadMoreBtnText = settings.loadMoreBtnText,
						loadingMoreText = settings.loadingMoreText,
						loadedPostsText = settings.loadedPostsText,
						ecrtl           = settings.ecrtl;

						if (postsoffset === undefined || postsoffset === "") {
							postsoffset = 0;
						}

						if (totalPage >= page) {
							var offset      = (parseInt( page - 1 ) * parseInt( loadMorePost )) + parseInt( numberOfPosts ) + parseInt( postsoffset );
							settings.offset = offset;
							$.ajax(
								{
									type: 'POST',
									data: 'settings=' + JSON.stringify( settings ) + '&action=eca_load_more_posts',
									url: ecafe_data.eca_ajax_url,
									beforeSend: function () {
										$this.text( loadingMoreText );
									}, success: function (data) {
										if (data === '') {
											$this.addClass( "hide" );
										} else {
											$( "." + settings.id + ' .eca-post-listing-inner' ).append( data );
											var $newItems = $( '' );
											$( "." + settings.id + ' .eca-post-listing-inner' ).isotope( 'insert', $newItems );
											$( "." + settings.id + ' .eca-post-listing-inner' ).isotope( 'layout' ).isotope( 'reloadItems' );
										}

										page++;
										if (page === totalPage) {
											$this.addClass( "hide" );
											$this.attr( 'data-page', page );
											$this.parent( ".eca-posts-load-more" ).append( '<div class="eca-all-posts-loaded">' + loadedPostsText + '</div>' );
										} else {
											$this.text( loadMoreBtnText );
											$this.attr( 'data-page', page );
										}
									}, complete: function () {
										if ($( "." + settings.id + ' .terms-filters-wrap' ).length) {
											$( "." + settings.id + ' .terms-filters-wrap .terms-filters-lists > li > a' ).each(
												function () {
													var filter = $( this ).data( "filter" );
													if (filter !== '' && filter !== undefined && filter === '*') {
														var totle_count = $( "." + settings.id + ' .eca-post-listing-inner .ec-grid-wrap' ).length;
													} else if (filter !== '' && filter !== undefined) {
														var totle_count = $( "." + settings.id + ' .eca-post-listing-inner .ec-grid-wrap' + filter ).length;
													}
													if (totle_count) {
														$( this ).find( ".term_count" ).text( totle_count );
													}
												}
											);
										}
										$( "." + settings.id + ' .eca-post-listing-inner' ).isotope(
											{
												itemSelector: '.ec-grid-wrap',
												originLeft: ecrtl,
											}
										);
									}
								}
							);
						} else {
							$this.addClass( "hide" );
						}
					}
				);
			}
		}
	);

	$( window ).on(
		'load',
		function () {
			if ($( 'body' ).find( '.posts-lazy-load' ).length >= 1) {
				var windowWidth, windowHeight, documentHeight, scrollTop, containerHeight, containerOffset, $window = $( window );

				var load_calc_Values = function () {
					windowWidth     = $window.width();
					windowHeight    = $window.height();
					documentHeight  = $( 'body' ).height();
					containerHeight = $( '.eca-isotope' ).height();
					containerOffset = $( '.eca-isotope' ).offset().top + 50;
					setTimeout(
						function () {
							containerHeight = $( '.eca-isotope' ).height();
							containerOffset = $( '.eca-isotope' ).offset().top + 50;
						},
						50
					);
				};

				load_calc_Values();
				$window.on( 'resize', load_calc_Values );  // Updated from .resize() to .on('resize')

				$window.on(
					'scroll',
					function (e) {
						// Updated from .bind() to .on()
						e.preventDefault();
						load_calc_Values();
						scrollTop = $window.scrollTop();
						$( '.eca-isotope' ).each(
							function () {
								var $this       = $( this );
								containerHeight = $this.height();
								containerOffset = $this.offset().top + 50;
								if ($this.find( ".posts-lazy-load" ).length && scrollTop < documentHeight && scrollTop > (containerHeight + containerOffset - windowHeight)) {
									var load        = $this.find( ".posts-lazy-load" ),
									settings        = load.data( 'settings' ),
									postsoffset     = settings.postsOffset,
									numberOfPosts   = settings.numberOfPosts,
									totalPage       = settings.totalPage,
									page            = load.attr( "data-page" ),
									loadMorePost    = settings.loadMorePost,
									loadingMoreText = settings.loadingMoreText,
									loadedPostsText = settings.loadedPostsText,
									ecrtl           = settings.ecrtl;

									if (load.data( 'requestRunning' )) {
										return;
									}

									if (postsoffset === undefined || postsoffset === "") {
										postsoffset = 0;
									}

									if (totalPage >= page) {
										load.data( 'requestRunning', true );
										var offset      = (parseInt( page ) - 1) * parseInt( loadMorePost ) + parseInt( numberOfPosts ) + parseInt( postsoffset );
										settings.offset = offset;
										$.ajax(
											{
												type: 'POST',
												data: 'settings=' + JSON.stringify( settings ) + '&action=eca_load_more_posts',
												url: ecafe_data.eca_ajax_url,
												beforeSend: function () {
													load.find( '.lazy-text' ).text( loadingMoreText );
													load.addClass( 'post-loading' );
												}, success: function (data) {
													if (data === '') {
														load.addClass( "hide" );
													} else {
														$( "." + settings.id + ' .eca-post-listing-inner' ).append( data );
														var $newItems = $( '' );
														$( "." + settings.id + ' .eca-post-listing-inner' ).isotope( 'insert', $newItems );
														$( "." + settings.id + ' .eca-post-listing-inner' ).isotope( 'layout' ).isotope( 'reloadItems' );
													}

													page++;
													if (page === totalPage) {
														load.addClass( "hide" );
														load.attr( 'data-page', page );
														load.parent( ".eca-posts-lazy-load" ).append( '<div class="eca-all-posts-loaded">' + loadedPostsText + '</div>' );
													} else {
														load.find( '.lazy-text' ).text( 'Lazy Load' );
														load.attr( 'data-page', page );
													}
													load.removeClass( 'post-loading' );
												}, complete: function () {
													if ($( "." + settings.id + ' .terms-filters-wrap' ).length) {
														$( "." + settings.id + ' .terms-filters-wrap .terms-filters-lists > li > a' ).each(
															function () {
																var filter = $( this ).data( "filter" );
																if (filter !== '' && filter !== undefined && filter === '*') {
																	var totle_count = $( "." + settings.id + ' .eca-post-listing-inner .ec-grid-wrap' ).length;
																} else if (filter !== '' && filter !== undefined) {
																	var totle_count = $( "." + settings.id + ' .eca-post-listing-inner .ec-grid-wrap' + filter ).length;
																}
																if (totle_count) {
																	$( this ).find( ".term_count" ).text( totle_count );
																}
															}
														);
													}
													load.data( 'requestRunning', false );
													$( "." + settings.id + ' .eca-post-listing-inner' ).isotope(
														{
															itemSelector: '.ec-grid-wrap',
															originLeft: ecrtl,
														}
													);
												}
											}
										);
									} else {
										load.addClass( "hide" );
									}
								}
							}
						);
					}
				);
			}
		}
	);
})( jQuery );
/*Load More Post*/