var ifqpf_filter_products_new,
ifqpf_load_more_new,
ifqpf_search_new,
ifqpf_before_ajax_send_new,
ifqpf_display_response_new,
ifqpf_clear_filter_data_new,
ifqpf_get_filter_form_data_new,
ifqpf_update_filter_url_new,
ifqpf_update_pag_filter_url_new,
ifqpf_javascript_after_ajax;

function ifqpf_load_more_new() {};
function ifqpf_clear_filter_data_new() {};
function ifqpf_get_filter_form_data_new() {};
function ifqpf_filter_products_new() {};

jQuery(document).ready(function() {
  
  jQuery(".ifqpf-type-sorting select, #text_ifqpf_search, .ifqpf-from :input").on('keyup change', function (){

    /* Main ajax call for filter products */
    ifqpf_filter_products_new = function(serialize_data = null, is_return = false){

      var page = jQuery("#ifqpf_page").val();
      var offset = jQuery("#ifqpf_offset").val();
      var nonce_val = ifqpf_public_object['nonce'];

        if ( !ifqpf_is_empty(serialize_data) ) {
              window.ifqpf_filter_data = serialize_data;
              serialize_data = serialize_data+'&page='+page;
        }
            
        serialize_data = serialize_data+'&action=ifqpf_filter_products';

        if ( !ifqpf_is_empty(offset) ) {
              serialize_data = serialize_data+"&offset="+offset;
        }

        if(!ifqpf_is_empty(nonce_val)){
              serialize_data = serialize_data+"&nonce="+nonce_val;
        }

        var ajax_filter = jQuery.ajax({
              type: "POST",
              url: ifqpf_public_object.ajaxurl,
              nonce: ifqpf_public_object.nonce,
              data: serialize_data,
              dataType: "json",
              beforeSend: function(){
                    ifqpf_before_ajax_send_new();
                    ifqpf_update_filter_url_new();
              },
              success: function (response) {
                    jQuery('html, body').animate({ scrollTop: 0 }, 0);
                    ifqpf_display_response_new(response);
                    if (typeof ifqpf_javascript_after_ajax === 'function') {
                        ifqpf_javascript_after_ajax();
                    } else if (typeof ifqpf_javascript_after_ajax === 'string' && ifqpf_javascript_after_ajax.trim()) {
                        eval(ifqpf_javascript_after_ajax);
                    }
              },
              error: function(){

              }
        });

        if ( is_return ) {
              return ajax_filter;
        }

      }

       /* Load more products with infinite scrool */
      ifqpf_load_more_new = function(){

        var serialize = window.ifqpf_filter_data;
        var max_page = parseInt(jQuery("#ifqpf_max_page").val());
        var page = jQuery("#ifqpf_page").val();
        var offset = jQuery("#ifqpf_offset").val();
        var nonce_val = ifqpf_public_object['nonce'];

        if ( page > max_page ) {
              return false;
        }

        var additional_param = 'action=ifqpf_filter_products&page='+page+'&ifqpf_load_more=true'+"&nonce="+nonce_val;

        if( !ifqpf_is_empty(serialize) ){
              serialize = serialize+'&'+additional_param;
        }else{
              serialize = additional_param;
        }

        if ( !ifqpf_is_empty(offset) ) {
              serialize = serialize+"&offset="+offset;
        }

        jQuery.ajax({
              type: "POST",
              url: ifqpf_public_object.ajaxurl,
              data: serialize,
              dataType: "json",
              beforeSend: function(){
                    load_more = false;
                    ifqpf_before_ajax_send_new();
              },
              success: function (response) {
                    ifqpf_display_response_new(response, true);
                    load_more = true;
                    if (typeof ifqpf_javascript_after_ajax === 'function') {
                        ifqpf_javascript_after_ajax();
                    } else if (typeof ifqpf_javascript_after_ajax === 'string' && ifqpf_javascript_after_ajax.trim()) {
                        eval(ifqpf_javascript_after_ajax);
                    }
              },
              complete: function(){
                    jQuery('body').removeClass('no-scroll');
              },
              error: function(){
                    
              }
        });

      }

      /* Search product */
      ifqpf_search_new = function(value){

        var serialize_data = ifqpf_get_filter_form_data_new();
        ifqpf_clear_filter_data_new();
        return ifqpf_filter_products_new(serialize_data, true);

      }

       /* Before filter ajax call */
      ifqpf_before_ajax_send_new = function(){

        if ( ifqpf_is_empty(is_enabled_button) ){
              jQuery(".ifqpf-from").addClass("loading");
        }
        jQuery('.ifqpf-main-wrap > ul').addClass("ifqpf-loading");
        jQuery(".ifqpf-main-wrap > ul").append(loader);
        jQuery('.ifqpf-mobile-overlay').removeClass('ifqpf-mobile-active');
        jQuery('.ifqpf-form-wrap').removeClass('ifqpf-mobile-active');
        jQuery('body').removeClass('ifqpf-mobile-active');
        jQuery(".woocommerce-pagination").remove();

      }

      /* Display response after filterd data */
      ifqpf_display_response_new = function(response, is_load_more=false){

        if ( ifqpf_is_empty(is_enabled_button) ){
              jQuery(".ifqpf-from").removeClass("loading");
        }
        jQuery('.ifqpf-main-wrap > ul').removeClass("ifqpf-loading");
        jQuery(".ifqpf-main-wrap > ul .ifqpf-loader").remove();

        if ( response.hasOwnProperty('products_html') && response.products_html !== '' ) {
              if ( is_load_more ) {
                    jQuery(".ifqpf-main-wrap > ul").append(response.products_html);  
              }else{
                    jQuery(".ifqpf-main-wrap > ul").html(response.products_html);
              }
              
        }

        var count_product = jQuery('li.product.status-publish').length;        

        if(response.max_page == 0){
                jQuery(".quick-result-count-display").html('Showing '+count_product+' products out of '+count_product+' products'); 
        }  else {
            jQuery(".quick-result-count-display").html('Showing '+count_product+' products out of '+response.total_found_posts+' products');
        }

        if(response.max_page == 0 && count_product == 0){
            jQuery(".quick-result-count-display").html('');
        }

        if(response.max_page == 0){
              jQuery(".msgerror").html('No more products found!');
              jQuery('#loadmorebtn').hide();
        }
        if(response.max_page == 0 && count_product == 0){
              jQuery(".msgerror").html('');
        } 
        if(response.max_page > 0){
              jQuery(".msgerror").html('');
              jQuery('#loadmorebtn').show();
        }

        if(response.total_found_posts == count_product){
          jQuery('#loadmorebtn').hide();
          jQuery(".msgerror").html('No more products found!');
        }

        if(response.max_page == 1){
          jQuery(".msgerror").html('');
        }

        if(response.total_found_posts == 0){
            jQuery(".msgerror").html('');
        }
        
        if ( response.hasOwnProperty('pagination_html') && response.pagination_html !== '' ) {
            jQuery("nav.woocommerce-pagination").html(response.pagination_html);
        }

        if ( response.hasOwnProperty('max_page') ) {
            jQuery("#ifqpf_max_page").val(response.max_page);
        }

        if ( response.hasOwnProperty('result_count') ) {
            jQuery("p.woocommerce-result-count").replaceWith(response.result_count);
        }

        if ( response.hasOwnProperty('page') ) {
            jQuery("#ifqpf_page").val(response.page);
        }

        if ( response.hasOwnProperty('offset') ) {
            jQuery("#ifqpf_offset").val(response.offset);
        }

        ifqpf_update_pag_filter_url_new();
            
      }

      /* Clear hidden input */
      ifqpf_clear_filter_data_new = function(){
        jQuery("#ifqpf_max_page").val("");
        jQuery("#ifqpf_page").val("1");
        jQuery("#ifqpf_offset").val("");
      }

      /* Get filter form data in serialize format */
      ifqpf_get_filter_form_data_new = function(is_serializeArray=false){
        var serialize;
        if( is_serializeArray ){
             serialize = jQuery(".ifqpf-from").serializeArray();
        }else{
              serialize = jQuery(".ifqpf-from").serialize();
        }
        return serialize;
      }

      /* Update url for filter */
      ifqpf_update_filter_url_new = function() {
            
        if( store_filter_in_url === 'yes' ){

              var data = ifqpf_get_filter_form_data_new(true);
              var url_data = ifqpf_get_url_form_data(data);

              ifqpf_remove_param_current_url(url_data);

              if( Object.keys(url_data).length > 0 ){

                    jQuery.each(url_data, function(param_name, param_value) {
                          
                          window.history.replaceState('', '', ifqpf_upd_url_param(window.location.href, param_name, param_value));

                    });

              }    

              window.history.pushState("data","Title",window.location.href);

        }

      }

      /* Add pagination parameters to url */
      ifqpf_update_pag_filter_url_new = function() {

        if( store_filter_in_url === 'yes' ){

              var ifqpf_max_page = jQuery("#ifqpf_max_page").val();
              var ifqpf_page = jQuery("#ifqpf_page").val();
              var ifqpf_offset = jQuery("#ifqpf_offset").val();

              window.history.replaceState('', '', ifqpf_upd_url_param(window.location.href, 'ifqpf_max_page', ifqpf_max_page));
              window.history.replaceState('', '', ifqpf_upd_url_param(window.location.href, 'ifqpf_page', ifqpf_page));
              window.history.replaceState('', '', ifqpf_upd_url_param(window.location.href, 'ifqpf_offset', ifqpf_offset));
              window.history.replaceState('', '', ifqpf_upd_url_param(window.location.href, 'ifqpf_page_limit', posts_per_page_load_more)); 

              window.history.pushState("data","Title",window.location.href);
              
        }

      }
  });
});


jQuery(function(jQuery){

  var is_enabled_button = ifqpf_public_object.is_enabled_button;
  
  function ifqpf_filter(e_data = null){ 

    ifqpf_clear_filter_data_new();
    var serialize = ifqpf_get_filter_form_data_new();
    if ( !ifqpf_is_empty(e_data) ) {
      serialize = serialize+e_data;
    }
    ifqpf_filter_products_new(serialize);

  }

  if ( is_enabled_button !== 'yes' ){
    jQuery(".ifqpf-from :input").change(function(){
      ifqpf_filter();
    });
  }

  jQuery(".ifqpf-from").submit(function(e){
    e.preventDefault();
    ifqpf_filter();
  });

  jQuery('.ifqpf-type-sorting select').change(function(e){
    e.preventDefault();
    ifqpf_filter();
  });

  jQuery("#text_ifqpf_search").keyup(function(){
    var that = this,
    value = jQuery(this).val();
    var searchRequest = null;

    if (value.length >= search_minlength ) { 
      if (searchRequest != null){ 
        searchRequest.abort();
      }
      searchRequest = ifqpf_search_new(value);
    }else if ( value.length < 1 ){ 
      ifqpf_filter();
    }
  });

  jQuery('.ifqpf-toggle-enabeled.taxonomy-page .ifqpf-current-toggle .ifqpf-term-children').css('display','block');

  jQuery('.ifqpf-toggle-btn').click(function(e) {
    e.preventDefault();
    var btn = jQuery(this);
    var wraper = jQuery(this).closest('.ifqpf-type');
    var parent = jQuery(this).closest('.ifqpf-term-item');
    var last_parent = jQuery(this).parents('.ifqpf-term-parent').last();
    var length = jQuery('.ifqpf-term-children').length;
    wraper.find('.ifqpf-term-parent').removeClass('ifqpf-current-toggle');
    last_parent.addClass('ifqpf-current-toggle');

    wraper.find('.ifqpf-toggle-btn:not(.ifqpf-current-toggle .ifqpf-toggle-btn)').removeClass('ifqpf-toggled');
    wraper.find('.ifqpf-term-children:not(.ifqpf-current-toggle .ifqpf-term-children)').slideUp("fast");
    wraper.find('.ifqpf-term-item:not(.ifqpf-current-toggle, .ifqpf-current-toggle .ifqpf-term-item)').removeClass('ifqpf-toggled');

    if( !btn.hasClass('ifqpf-toggled') ){
      btn.addClass('ifqpf-toggled');
      parent.addClass('ifqpf-toggled');
      parent.find('.ifqpf-term-children:first').slideDown("fast");
    }else{
      last_parent.removeClass('ifqpf-current-toggle');
      btn.removeClass('ifqpf-toggled');
      parent.removeClass('ifqpf-toggled');
      parent.find('.ifqpf-toggle-btn').removeClass('ifqpf-toggled');
      parent.find('.ifqpf-term-children').removeClass('ifqpf-toggled').slideUp("fast");

    }
  });

  jQuery('.ifqpf-mobile-btn').click(function(){
    jQuery('.ifqpf-mobile-overlay').addClass('ifqpf-mobile-active');
    jQuery('.ifqpf-form-wrap').addClass('ifqpf-mobile-active');
    jQuery('body').addClass('ifqpf-mobile-active');
  });

  jQuery('.ifqpf-mobile-close').click(function(e){
    e.preventDefault();
    jQuery('.ifqpf-mobile-overlay').removeClass('ifqpf-mobile-active');
    jQuery('.ifqpf-form-wrap').removeClass('ifqpf-mobile-active');
    jQuery('body').removeClass('ifqpf-mobile-active');
  });

  jQuery('.ifqpf-toggle-enabeled .ifqpf-type-heading').click(function(e) {

    e.preventDefault();
    var field = jQuery(this).closest('.ifqpf-type');
    var btn = field.find('.ifqpf-toggle-field-btn');

    if( field.hasClass('ifqpf-type-toggled') ){
      field.find('.ifqpf-field-wrap').slideUp('fast');
    }else{
      field.find('.ifqpf-field-wrap').slideDown('fast');
    }
    field.toggleClass('ifqpf-type-toggled');
    btn.toggleClass('ifqpf-type-btn-toggled');

  });

  jQuery("#ifqpf_reset").click(function(){

    jQuery(".ifqpf-from")[0].reset();
    ifqpf_filter();
    jQuery(".msgerror").html('');

  });

  var get_offset = ifqpf_get_URL_param('ifqpf_offset');

  if( !ifqpf_is_empty(get_offset) ){
    get_offset = parseInt(get_offset)-1;
     var pT = jQuery('.ifqpf-main-wrap > ul').offset().top,
     pH = jQuery('.ifqpf-main-wrap > ul').outerHeight() + 150,
     wH = jQuery(window).height();
    var hh = (75/100) * (pT+pH-wH);
    jQuery([document.documentElement, document.body]).animate({
      scrollTop: hh
    }, 2000);

  }

  jQuery('.ifqpf-toggle-enabeled.ifqpf-type-toggled .ifqpf-field-wrap').css('display','inherit');

});