/*mlink*/
( function ( $ ) {
	"use strict";
	var WidgetSCProHandler = function ($scope, $) {
		var container = $scope.find( '.ec-scroll-progress' ),
			Offset    = container.data( "offset" ),
			Duration  = container.data( "duration" );

		if (container.hasClass( 'layout_1' )) {
			function ecScrollProgress() {
				let windowscroll = document.body.scrollTop || document.documentElement.scrollTop,
					height       = document.documentElement.scrollHeight - document.documentElement.clientHeight,
					cur          = (windowscroll / height) * 100;
					document.getElementById( "ecscrollprogressbar" ).style.width = cur + "%";
			}

			window.onscroll = function () {
				ecScrollProgress();
			};
		}
		if (container.hasClass( 'layout_2' )) {
			var path = document.querySelector( '.ec-scroll-progress.layout_2 path' );;
			var length                      = path.getTotalLength();
				path.style.transition       = path.style.WebkitTransition = 'none';
				path.style.strokeDasharray  = length + ' ' + length;
				path.style.strokeDashoffset = length;
				path.getBoundingClientRect();
				path.style.transition = path.style.WebkitTransition = 'stroke-dashoffset 10ms linear';
				var statechange       = function () {
					var scroll                  = $( window ).scrollTop();
					var height                  = $( document ).height() - $( window ).height();
					var state                   = length - (scroll * length / height);
					path.style.strokeDashoffset = state;
				}
				statechange();
				jQuery( window ).scroll( statechange );
				jQuery( window ).on(
					'scroll',
					function () {
						if (jQuery( this ).scrollTop() > Offset) {
							jQuery( '.ec-scroll-progress.layout_2' ).addClass( 'active-progress' );
						} else {
							jQuery( '.ec-scroll-progress.layout_2' ).removeClass( 'active-progress' );
						}
					}
				);
				jQuery( '.ec-scroll-progress.layout_2' ).on(
					'click',
					function (event) {
						event.preventDefault();
						jQuery( 'html, body' ).animate( {scrollTop: 0}, Duration );
						return false;
					}
				)
		}

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