(function ($) {
	"use strict";
	var WidgetPagePiling = function ($scope, $) {
		$( document ).ready(
			function () {
				if ( $scope.find( '.eca-page-piling-wrapper' ).length ) {
					$( "#pp-nav" ).remove();
					var ppoptions = $scope.find( '.eca-page-piling-wrapper' ).data( "ppoptions" );
					if ($( window ).innerWidth() >= ppoptions['disable']) {
						$scope.find( '.eca-page-piling-wrapper' ).closest( 'body,html' ).addClass( 'ecppused' );
						var anchorsreturn            = ppoptions['anchors'].split( ',' ).map(
							function (item) {
								return item.trim();
							}
						);
						var navigationtooltipsreturn = ppoptions['navigationtooltips'].split( ',' ).map(
							function (item) {
								return item.trim();
							}
						);
						$( '#pagepiling' ).pagepiling(
							{
								direction: ppoptions['direction'],
								verticalCentered: ppoptions['verticalCentered'],
								sectionsColor: [],
								anchors: anchorsreturn,
								menu: '#ec-pp-menu',
								scrollingSpeed: ppoptions['scrollingSpeed'],
								easing: ppoptions['easing'],
								loopBottom: ppoptions['loopBottom'],
								loopTop: ppoptions['loopTop'],
								css3: true,
								navigation: {
									'position': ppoptions['navigationposition'],
									'tooltips': navigationtooltipsreturn
								},
								normalScrollElements: null,
								normalScrollElementTouchThreshold: 5,
								touchSensitivity: 5,
								keyboardScrolling: ppoptions['keyboardScrolling'],
								sectionSelector: '.section',
								animateAnchor: false,

								onLeave: function (index, nextIndex, direction) {
									var ecppanimation = $scope.find( '.pp-section:not(.active)' );
									if ($scope.find( ".pp-section .elementor-widget[data-settings]" ).length > 0) {
										$( ecppanimation ).find( ".elementor-widget[data-settings].animated" ).each(
											function () {
												if ($( this ).data( "settings" )._animation) {
													$( this ).addClass( "elementor-invisible" ).removeClass( $( this ).data( "settings" )._animation + " animated" );
												}
											}
										);
									}
								},
								afterLoad: function (anchorLink, index) {
									if ($scope.find( ".pp-section .elementor-widget[data-settings]" ).length > 0) {
										var ecppanimationactive = $scope.find( '.pp-section.active' );
										$( ecppanimationactive ).find( ".elementor-widget[data-settings]:not(.animated)" ).each(
											function () {
												var element = $( this );
												if (element.data( "settings" )._animation && element.data( "settings" )._animation_delay) {
													setTimeout(
														function () {
															element.removeClass( "elementor-invisible" ).addClass( element.data( "settings" )._animation + ' animated' );
														},
														element.data( "settings" )._animation_delay
													);
												} else {
													element.removeClass( "elementor-invisible" ).addClass( element.data( "settings" )._animation + ' animated' );
												}
											}
										);
									}
								},
							}
						);

						// Trigger animation on first load
						setTimeout(
							function () {
								var firstSection = $scope.find( '.pp-section.active' );
								firstSection.find( ".elementor-widget[data-settings]" ).each(
									function () {
										var element = $( this );
										if (element.data( "settings" )._animation && element.data( "settings" )._animation_delay) {
											setTimeout(
												function () {
													element.removeClass( "elementor-invisible" ).addClass( element.data( "settings" )._animation + ' animated' );
												},
												element.data( "settings" )._animation_delay
											);
										} else {
											element.removeClass( "elementor-invisible" ).addClass( element.data( "settings" )._animation + ' animated' );
										}
									}
								);
							},
							500
						); // Delay to ensure pagepiling has initialized
					} else {
						$scope.find( '.eca-page-piling-wrapper' ).closest( 'body,html' ).addClass( 'ecppunused' );
					}
				}
			}
		);
	};
	$( window ).on(
		'elementor/frontend/init',
		function () {
			elementorFrontend.hooks.addAction( 'frontend/element_ready/ecafe-page-piling.default', WidgetPagePiling );
		}
	);
})( jQuery );