(function ($) {
	"use strict";
	var ecaLax = function ($scope, $) {
		var target = $scope,
		sectionId = target.data("id"),
		editMode = elementorFrontend.isEditMode(),
		settings = {};
		if (editMode) {
			var editorElements = null,
			sectionData = {};
			if (!window.elementor.hasOwnProperty("elements")) {
				return false;
			}
			editorElements = window.elementor.elements;
			if (!editorElements.models) {
				return false; 
			}
			$.each(editorElements.models, function (index, elem) {
				if (elem.id == target.closest('.elementor-top-section,.e-container,.e-con').data('id')) {
					$.each(elem.attributes.elements.models, function (index, col) {
						$.each(col.attributes.elements.models, function (index, subSec) {
							$.each(subSec.attributes.elements.models, function (index, subCol) {
								$.each(subCol.attributes.elements.models, function (ind, subWidget) {
									if (sectionId == subWidget.id) {
										sectionData = subWidget.attributes.settings.attributes;
										settings["laxScroll"] = sectionData["ecaExtraLaxScroll"];
										if(target.data('lax-md')){
											target.removeClass(target[0]['dataset']['laxMd']);
											target.removeClass(target[0]['dataset']['laxSm']);
											target.removeClass(target[0]['dataset']['laxXs']);
										}
										if (settings["laxScroll"] == "yes") {
											if(sectionData["laxScrollOpt"]['models']!=''){
												let repeater = sectionData["laxScrollOpt"]['models'];
												let laxClass = laxRender(repeater);
												
												target.attr('data-lax-md',laxClass.md);
												target.attr('data-lax-sm',laxClass.sm);
												target.attr('data-lax-xs',laxClass.xs);
												if($('body')[0]['dataset']['elementorDeviceMode']=='tablet'){
													target.addClass('lax '+laxClass.sm);
												}else if($('body')[0]['dataset']['elementorDeviceMode']=='mobile'){
													target.addClass('lax '+laxClass.xs);
												}else{
													target.addClass('lax '+laxClass.md);
												}
											}
											generateLax();
										}
										if (0 !== settings.length) {
											return settings;
										}
									}
									if (!editMode || !settings) {
										return false;
									}
								});
							});
						});
					});
				}
				$.each(elem.attributes.elements.models, function (inde, column) {
					$.each(column.attributes.elements.models, function (ind, widget) {
						if (sectionId == widget.id) {
							sectionData = widget.attributes.settings.attributes;
							settings["laxScroll"] = sectionData["ecaExtraLaxScroll"];
							if(target.data('lax-md')){
								target.removeClass(target[0]['dataset']['laxMd']);
								target.removeClass(target[0]['dataset']['laxSm']);
								target.removeClass(target[0]['dataset']['laxXs']);
							}
							if (settings["laxScroll"] == "yes") {
								if(sectionData["laxScrollOpt"]['models']!=''){
									let repeater = sectionData["laxScrollOpt"]['models'];
									let laxClass = laxRender(repeater);
									
									target.attr('data-lax-md',laxClass.md);
									target.attr('data-lax-sm',laxClass.sm);
									target.attr('data-lax-xs',laxClass.xs);
									if($('body')[0]['dataset']['elementorDeviceMode']=='tablet'){
										target.addClass('lax '+laxClass.sm);
									}else if($('body')[0]['dataset']['elementorDeviceMode']=='mobile'){
										target.addClass('lax '+laxClass.xs);
									}else{
										target.addClass('lax '+laxClass.md);
									}
								}
								generateLax();
							}
							if (0 !== settings.length) {
								return settings;
							}
						}
						if (!editMode || !settings) {
							return false;
						}
					});
				});				
			});
			function laxRender(repeater){
				let laxClassDk='',laxClassTb='',laxClassMb='';
				repeater.forEach( (element,index) => {
					let attr = element.attributes;
					let name = attr.laxEffects;
					let scrollDistanceDk = (attr.scrollDistance.size!='') ? attr.scrollDistance.size : 0,
						scrollDistanceTb = (attr.scrollDistance_tablet.size!='') ? attr.scrollDistance_tablet.size : 0,
						scrollDistanceMb = (attr.scrollDistance_mobile.size!='') ? attr.scrollDistance_mobile.size : 0;
					let effectDk = ' lax_preset_'+ attr.laxEffects +':'+ scrollDistanceDk+':',
						effectTb = ' lax_preset_'+ attr.laxEffects +':'+ scrollDistanceTb+':',
						effectMb = ' lax_preset_'+ attr.laxEffects +':'+ scrollDistanceMb+':';
					
					if(name=='fadeIn' || name=='fadeOut' || name=='fadeInOut'){
						let fadeOptDk = (attr.fadeOpt.size!='') ? attr.fadeOpt.size : 0,
							fadeOptTb = (attr.fadeOpt_tablet.size!='') ? attr.fadeOpt_tablet.size : fadeOptDk,
							fadeOptMb = (attr.fadeOpt_mobile.size!='') ? attr.fadeOpt_mobile.size : fadeOptTb;
						laxClassDk+= effectDk + fadeOptDk;
						laxClassTb+= effectTb + fadeOptTb;
						laxClassMb+= effectMb + fadeOptMb;
					}
					if(name=='scaleIn' || name=='scaleOut' || name=='scaleInOut'){
						let scaleOptDk = (attr.scaleOpt.size!='') ? attr.scaleOpt.size : 0,
							scaleOptTb = (attr.scaleOpt_tablet.size!='') ? attr.scaleOpt_tablet.size : scaleOptDk,
							scaleOptMb = (attr.scaleOpt_mobile.size!='') ? attr.scaleOpt_mobile.size : scaleOptTb;
						laxClassDk+= effectDk + scaleOptDk;
						laxClassTb+= effectTb + scaleOptTb;
						laxClassMb+= effectMb + scaleOptMb;
					}
					if(name=='slideX' || name=='slideY'){
						let slideOptDk = (attr.slideOpt.size!='') ? attr.slideOpt.size : 0,
							slideOptTb = (attr.slideOpt_tablet.size!='') ? attr.slideOpt_tablet.size : slideOptDk,
							slideOptMb = (attr.slideOpt_mobile.size!='') ? attr.slideOpt_mobile.size : slideOptTb;
						laxClassDk+= effectDk + slideOptDk;
						laxClassTb+= effectTb + slideOptTb;
						laxClassMb+= effectMb + slideOptMb;
					}
					if(name=='jiggle'){
						let jiggleOptDk = (attr.jiggleOpt.size!='') ? attr.jiggleOpt.size : 0,
							jiggleOptTb = (attr.jiggleOpt_tablet.size!='') ? attr.jiggleOpt_tablet.size : jiggleOptDk,
							jiggleOptMb = (attr.jiggleOpt_mobile.size!='') ? attr.jiggleOpt_mobile.size : jiggleOptTb;
						laxClassDk+= effectDk + jiggleOptDk;
						laxClassTb+= effectTb + jiggleOptTb;
						laxClassMb+= effectMb + jiggleOptMb;
					}
					if(name=='seesaw'){
						let seesawOptDk = (attr.seesawOpt.size!='') ? attr.seesawOpt.size : 0,
							seesawOptTb = (attr.seesawOpt_tablet.size!='') ? attr.seesawOpt_tablet.size : seesawOptDk,
							seesawOptMb = (attr.seesawOpt_mobile.size!='') ? attr.seesawOpt_mobile.size : seesawOptTb;
						laxClassDk+= effectDk + seesawOptDk;
						laxClassTb+= effectTb + seesawOptTb;
						laxClassMb+= effectMb + seesawOptMb;
					}
					if(name=='zigzag'){
						let zigzagOptDk = (attr.zigzagOpt.size!='') ? attr.zigzagOpt.size : 0,
							zigzagOptTb = (attr.zigzagOpt_tablet.size!='') ? attr.zigzagOpt_tablet.size : zigzagOptDk,
							zigzagOptMb = (attr.zigzagOpt_mobile.size!='') ? attr.zigzagOpt_mobile.size : zigzagOptTb;
						laxClassDk+= effectDk + zigzagOptDk;
						laxClassTb+= effectTb + zigzagOptTb;
						laxClassMb+= effectMb + zigzagOptMb;
					}
					if(name=='hueRotate' || name=='spin' || name=='flipX' || name=='flipY'){
						let hueRotateOptDk = (attr.hueRotateOpt.size!='') ? attr.hueRotateOpt.size : 0,
							hueRotateOptTb = (attr.hueRotateOpt_tablet.size!='') ? attr.hueRotateOpt_tablet.size : hueRotateOptDk,
							hueRotateOptMb = (attr.hueRotateOpt_mobile.size!='') ? attr.hueRotateOpt_mobile.size : hueRotateOptTb;
						laxClassDk+= effectDk + hueRotateOptDk;
						laxClassTb+= effectTb + hueRotateOptTb;
						laxClassMb+= effectMb + hueRotateOptMb;
					}
					if(name=='blurIn' || name=='blurOut' || name=='blurInOut'){
						let blurOptDk = (attr.blurOpt.size!='') ? attr.blurOpt.size : 0,
							blurOptTb = (attr.blurOpt_tablet.size!='') ? attr.blurOpt_tablet.size : blurOptDk,
							blurOptMb = (attr.blurOpt_mobile.size!='') ? attr.blurOpt_mobile.size : blurOptTb;
						laxClassDk+= effectDk + blurOptDk;
						laxClassTb+= effectTb + blurOptTb;
						laxClassMb+= effectMb + blurOptMb;
					}
				})
				let laxClass = {'md' : laxClassDk, 'sm' : laxClassTb, 'xs' : laxClassMb };
				return laxClass;
			}
			function generateLax() {
				// target.attr("id", "cccc-" + sectionId);
				// var $currentTooltip = "#" + target.attr("id");
				lax.init();
				lax.addDriver('scrollY', function () {
					return window.scrollY
				});
			}
		}
	};
	$(window).on('elementor/frontend/init', function () {
		elementorFrontend.hooks.addAction('frontend/element_ready/widget', ecaLax);
	});
})(jQuery);