(function ($) {
	"use strict";
	var WidgetImageAccordion = function ($scope, $) {
		let container = $scope.find( '.eca-image-accordion-wrapper' );
		if ( container.length ) {
			var loopItem = $scope.find( '.eca-image-accordion-loop-wrapper' ),
				flexGrow = $scope.find( '.eca-image-accordion-inner' ).data( 'flexgrow' ),
				duration = container.data( 'duration' );
			if (container.hasClass( 'ialoop' )) {
				setInterval(
					function () {
						var active = $scope.find( '.eca-image-accordion-wrapper .active-image-accrodian' );
						active.removeClass( 'active-image-accrodian' );
						active.css( "flexGrow","1" );
						if (active.next().length) {
							active.next().addClass( 'active-image-accrodian' );
							active.next().css( "flexGrow",flexGrow );
						} else {
							var firstItem = $scope.find( '.eca-image-accordion-loop-wrapper:nth-child(1)' );
							if (firstItem) {
								firstItem.addClass( 'active-image-accrodian' );
								firstItem.css( "flexGrow",flexGrow );
							}
						}
					},
					duration * 1000
				);
			}

			loopItem.on(
				"mouseenter",
				function () {
					$( this ).closest( '.eca-image-accordion-wrapper' ).find( '.active-image-accrodian' ).css( 'flex-grow','1' ).removeClass( 'active-image-accrodian' );
					$( this ).addClass( "active-image-accrodian" ).css( 'flex-grow',flexGrow );
				}
			);

			if (container.hasClass( 'eca-ia-mouseleave' )) {
				loopItem.on(
					"mouseleave",
					function () {
						$( this ).closest( '.eca-image-accordion-wrapper' ).find( '.active-image-accrodian' ).css( 'flex-grow','1' ).removeClass( 'active-image-accrodian' );
					}
				);
			}
		}
	};

	$( window ).on(
		'elementor/frontend/init',
		function () {
			elementorFrontend.hooks.addAction( 'frontend/element_ready/ecafe-image-accordion.default', WidgetImageAccordion );
		}
	);
})( jQuery );