// source --> https://ledoc-info.com/wp-content/plugins/ele-custom-skin/assets/js/ecs_ajax_pagination.js?ver=3.1.9 
jQuery( document ).ready(function() {
  jQuery( ".ecs-load-more-button" ).each(function() {
    widget=jQuery(this);
    settings = widget.attr("data-settings");
    args = JSON.parse(settings);
    widget.children( ".elementor-button" ).attr("href", "javascript:ECS_load_next_page('"+ args.widget_id +"');");
  });
});
var canBeLoaded = true; // this param allows to initiate the AJAX call only if necessary


function ECS_load_next_page(id){

  widget = jQuery(".elementor-element[data-id='" + id + "'] .ecs-posts");
  settings = widget.attr("data-settings");
  args = JSON.parse(settings);
  
  posts = jQuery(".elementor-element[data-id='" + args.widget_id + "'] .ecs-posts");
  
  if(args.load_method == 'loadmore'){
    button_text = jQuery(".elementor-element[data-id='" + args.widget_id + "'] .ecs-load-more-button .elementor-button");// add this .elementor-element[data-id='" + args.widget_id + "']
    button = jQuery(".elementor-element[data-id='" + args.widget_id + "'] .ecs-load-more-button");
    attb = JSON.parse(button.attr("data-settings"));
  }
  if(args.load_method == 'lazyload'){
    animation = jQuery(".elementor-element[data-id='" + args.widget_id + "'] .ecs-lazyload");
  }
  data = {
    'action': 'ecsload',
    'query': ecs_ajax_params.posts,
    'ecs_ajax_settings': settings,
  };

  jQuery.ajax({
    url : ecs_ajax_params.ajaxurl, // AJAX handler
    data : data,
    type : 'POST',
    beforeSend : function ( xhr ) {
      if(args.load_method == 'loadmore') button_text.html(attb.loading_text); // change the button text, you can also add a preloader image
      canBeLoaded = false;
    },
    success : function( data ){
      if( data ) { 
        posts.append(data); // insert new posts
        args.current_page++;
        if(args.load_method == 'loadmore'){
          button_text.html( attb.text );
          button_text.blur();
        }
        newsettings = JSON.stringify(args);
        widget.attr("data-settings",newsettings);
        
        
        if(args.load_method == 'lazyload') {jQuery(animation).addClass("animation-hidden");}
        //here you need to take care of linkable items and masonry !!!!!!!!!!!!!!!!!
        ECS_do_action('ajax',args);

        if ( args.current_page == args.max_num_pages ) {
          if(args.load_method == 'loadmore') button.remove(); // if last page, remove the button
          if(args.load_method == 'lazyload') animation.remove();
        }
        canBeLoaded = true;

        if (typeof ECScheckInView !== 'undefined') ECScheckInView();
        // you can also fire the "post-load" event here if you use a plugin that requires it
        // $( document.body ).trigger( 'post-load' );
      } else {
        if(args.load_method == 'loadmore') {button.remove();} // if no data, remove the button as well
        if(args.load_method == 'lazyload') {animation.remove();}
      }
    }
  });
  
}


jQuery(function($){
  $('.ecs-lazyload').addClass("animation-hidden");
  $('.ecs-lazyload a').css("display", "none"); 
});

function EleCustomSkinChangeUrlPage(args){
  if(!args.change_url) return;
  regex = /\/page\/[0-9]+\//gm;
  currenturl = window.location.pathname;
  newurl = currenturl.replace(regex, '/');
  newurl = newurl + 'page/' + args.current_page +'/';
  console.log(newurl);
  history.pushState({urlPath:newurl},"",newurl);
}

function EleCustomSkinReInitJs(args){
  if(!args.reinit_js) return;
  jQuery('.elementor-element-' + args.widget_id + ' .elementor-element').each(function() { elementorFrontend.elementsHandler.runReadyTrigger( jQuery( this ) ); });console.log(args.reinit_js);
}

jQuery( document ).ready(function() {

  ECS_add_action("ajax", function(args){EleCustomSkinChangeUrlPage(args)});
  ECS_add_action("ajax", function(args){EleCustomSkinReInitJs(args)});

});
// source --> https://ledoc-info.com/wp-content/plugins/ele-custom-skin/assets/js/ecs.js?ver=3.1.9 

var ECS_hooks = {};

var ECS_Columns_Count=0;

function ECS_add_action(name, func) {
  if(!ECS_hooks[name]) ECS_hooks[name] = [];
  ECS_hooks[name].push(func);
}

function ECS_do_action(name, ...params){
  if(ECS_hooks[name]) 
     ECS_hooks[name].forEach(func => func(...params));
};
// source --> https://ledoc-info.com/wp-content/plugins/luckywp-table-of-contents/front/assets/main.min.js?ver=2.1.14 
!function(){var o,l,n,e,g,m=function(t,e){var i,n={};for(i in t)n[i]=t[i];for(i in e)n[i]=e[i];return n},t=function(t){return t},c=(o={duration:300,action:"close",startTime:null,startHeight:null,endHeight:null,easing:t},l=function(e,i){cancelAnimationFrame(e.getAttribute("data-lwptoc-animation-request-id")),e.setAttribute("data-lwptoc-animation-request-id",window.requestAnimationFrame(function(t){n(e,i,t)}))},n=function(t,e,i){e.startTime||(e.startTime=i);var n,o=i-e.startTime;o<e.duration?(t.style.height=((e.endHeight-e.startingHeight)*e.easing(o/e.duration)+e.startingHeight).toFixed(2)+"px",l(t,e)):("close"===e.action&&(t.style.display="none"),"open"===e.action&&(t.style.display="block"),(n=t).style.height=null,n.style.overflow=null)},function(t,e){if(window.requestAnimationFrame){var i=m(o,{});i.action=e,t.style.height?i.startingHeight=parseFloat(t.style.height):i.startingHeight="close"===e?t.scrollHeight:0,(n=t).style.display="block",n.style.overflow="hidden",i.endHeight="close"===e?0:(t.style.height="0px",t.scrollHeight),l(t,i)}else t.style.display="close"===e?"none":"block";var n}),a=function(t){for(var e,i=document.querySelectorAll('[id="'+t+'"]'),n=0;n<i.length;n++)if((e=i[n]).offsetWidth||e.offsetHeight||e.getClientRects().length)return i[n];return null},i=(e={offset:0,duration:500,easing:t,onComplete:function(t,e){}},g=function(t,e){var i=t.getBoundingClientRect().top+window.pageYOffset-e;return i<0?0:i},function(o,t){var l,a=m(e,t);if(window.requestAnimationFrame&&"smooth"!==window.getComputedStyle(document.getElementsByTagName("HTML")[0]).scrollBehavior){var r,s,c=window.pageYOffset,d=null,u=function(t){l=g(o,a.offset),r=l-c;var e=window.pageYOffset;if(!s||!(0<r&&e<s||r<0&&s<e)){s=e,d||(d=t-1);var i=t-d,n=((l-c)*a.easing(i/a.duration)+c).toFixed();window.scroll(0,n),i<a.duration?window.requestAnimationFrame(u):(window.scroll(0,l),a.onComplete(0,l))}};window.requestAnimationFrame(u)}else l=g(o,a.offset),window.scroll(0,l),a.onComplete(0,l)}),u={scrollTo:function(t,e){i(t,e)},registerScrollTrigger:function(t,i){for(var e=0;e<t.length;e++)t[e].addEventListener("click",function(t){t.preventDefault();var e=this.getAttribute("href"),c=e.substring(1),d=a(c);d&&(e!==document.location.hash&&(i.onComplete=function(t,e){var i,n,o,l,a,r,s;d.setAttribute("id",""),i=c,n=t,o=e,(s=document.createElement("a")).setAttribute("id",i),s.setAttribute("style","position:absolute;visibility:hidden;left:"+n+"px;top:"+o+"px;"),l=document.body,a=s,l.prepend?l.prepend(a):l.insertBefore(a,l.firstChild),document.location.hash=i,(r=s).remove?r.remove():r.parentNode.removeChild(r),d.setAttribute("id",c)}),u.scrollTo(d,i))})},init:function(t){if("1"!==t.getAttribute("data-lwptoc-initialized")){t.setAttribute("data-lwptoc-initialized","1");var a,r=t.getElementsByClassName("lwptoc_toggle_label")[0],s=t.getElementsByClassName("lwptoc_items")[0];if(r)r.addEventListener("click",function(t){var e,i,n,o,l;t.preventDefault(),a=r.getAttribute("data-label"),r.setAttribute("data-label",r.innerHTML),r.innerHTML=a,l="lwptoc_items-visible",-1<(" "+s.className+" ").indexOf(" "+l+" ")?(o="lwptoc_items-visible",(n=s).className=(" "+n.className+" ").replace(" "+o+" ","").trim(),c(s,"close")):(i="lwptoc_items-visible",(e=s).className=e.className.trim()+" "+i,c(s,"open"))});"1"===t.getAttribute("data-smooth-scroll")&&u.registerScrollTrigger(s.getElementsByTagName("A"),{offset:t.getAttribute("data-smooth-scroll-offset")})}},globalInit:function(){for(var t=document.getElementsByClassName("lwptoc"),e=0;e<t.length;e++)u.init(t[e])}};window.lwptoc=u,"loading"===document.readyState?document.addEventListener("DOMContentLoaded",u.globalInit):u.globalInit()}();