function applyFilterRedirect(value, arrParamUrl, tag) {
  var arrayGets = getGetToJson();
  for (var i = 0; i < arrayGets.length; i++) {
    if (arrayGets[i][0] == 'route'){
      arrayGets[i][0] = 'route2';
    }
    var strArrayGets = String(arrayGets[i][0]);
    if ( (arrParamUrl.indexOf(strArrayGets) == -1) && (arrayGets[i][0] != 'route') && (arrayGets[i][0] != 'product_id') && (arrayGets[i][0] != '') ) {
      if (value != '') {
        value += '&' + arrayGets[i][0] + '=' + arrayGets[i][1];
      } else {
        value += arrayGets[i][0] + '=' + arrayGets[i][1];
      }
    }
  }
  /* Ajax que retorna e retireciona para a url completa do filtro aplicado */
  $.ajax({
    url: 'index.php?route=product/product/getProductGridLink',
    type: 'GET',
    data: value,
    dataType: 'text',
    contentType: 'text/plain',
    success: function(url) {
      var currentUrl = window.location.href;
      if (url != '' && url != currentUrl) {
        window.location.href = url;
      }
    }
  });
} // Final applyFilterRedirect

function applyFilter(classInput, paramUrl, tag) {
  /* Concatena os ids, separando-os por vircula, das opções que estão marcadas como checked */
  var idChecked = '';
  $('.' + classInput).each(function() {
    if ($(this).is(':checked')) {
      idChecked += ',' + $(this).val();
    }
  });
  idChecked = idChecked.substring(1); //Remove a primeira virgula / primeiro caracter
  if (idChecked.length > 0) {
    idChecked = paramUrl + '=' + idChecked;
  }
  applyFilterRedirect(idChecked, [paramUrl], tag);
}

function applyFilterTagText(classInput, classTag) {
  if ($('.' + classInput).is(':checked')) {
    $('.ftr-js-filters').find('.ftr-js-swipe-content').each(function () {
      var qtyChecked = $(this).find('.' + classInput + ':checked').length;
      if (qtyChecked) {
        var element = $(this).prev('.' + classTag);
        var textChecked = element.text() + ' • ' + qtyChecked;

        element.html(textChecked);
        element.addClass('ftr-background--primary');
        }
    });
  }
}

function applyFilterPrice(initial, laste, tag) {
  var idInitial = $('#' + initial);
  var idLaste = $('#' + laste);
  if ( (idInitial.val() != '') && (idLaste.val() != '') && (parseInt(idInitial.val(), 10) > parseInt(idLaste.val(), 10)) ) {
    $('.ftr-js-price-error').removeClass('ftr-none');
    $('#price_initial, #price_laste').one('keydown',function() {
      $('.ftr-js-price-error').addClass('ftr-none');
    });
  } else {
    var value = '';
    if (idInitial.val() != '') {
      value += '&' + initial + '=' + idInitial.val();
    }
    if (idLaste.val() != '') {
      value += '&' + laste + '=' + idLaste.val();
    }
    contentSwipeDisplayNone();
    applyFilterRedirect(value, [initial, laste], tag);
  }
}

$(document).ready(function() {
  /* Filtro de range do pontos de fidelidade */
  if ($('.ftr-js-fidelitypoints').length > 0) {
    $('.ftr-js-fidelitypoints__button').on('click', function() {
      applyFilter('ftr-js-fidelitypoints__input','filrange', 'ftr-js-fidelitypoints');
    });
    applyFilterTagText('ftr-js-fidelitypoints__input', 'ftr-js-fidelitypoints');
  }

  /* Filtro de cor */
  if ($('.ftr-js-color').length > 0) {
    $('.ftr-js-color__button').on('click', function() {
      applyFilter('ftr-js-color__input','filcolor', 'ftr-js-color');
    });
    applyFilterTagText('ftr-js-color__input', 'ftr-js-color');
  }

  /* Filtro de tamanho */
  if ($('.ftr-js-size').length > 0) {
    $('.ftr-js-size__button').on('click', function() {
      applyFilter('ftr-js-size__input','filsize', 'ftr-js-size');
    });
    applyFilterTagText('ftr-js-size__input', 'ftr-js-size');
  }

  /* Filtro de Especificação */
  if ($('.ftr-js-specification').length > 0) {
    $('.ftr-js-specification__button').on('click', function() {
      applyFilter('ftr-js-specification__input','filspecification', 'ftr-js-specification');
    });
    applyFilterTagText('ftr-js-specification__input', 'ftr-js-specification');
  }

  /* Filtro de Marca */
  if ($('.ftr-js-brand').length > 0) {
    $('.ftr-js-brand__button').on('click', function() {
      applyFilter('ftr-js-brand__input','filbrand', 'ftr-js-brand');
    });
    applyFilterTagText('ftr-js-brand__input', 'ftr-js-brand');
  }

  /* Filtro de Faixa de preço */
  if ($('.ftr-js-price').length > 0) {
    $('.ftr-js-price__button').on('click', function() {
      applyFilterPrice('price_initial', 'price_laste', 'ftr-js-price');
    });

    if ( ($('#price_initial').val() != '') || ($('#price_laste').val() != '') ) {
      var textPriceChecked;
      var initial = $('#price_initial').val();
      var laste = $('#price_laste').val();
      if ( (initial != '') && (laste == '') ) {
        textPriceChecked = 'A partir de R$' + initial;
      } else if ( (initial == '') && (laste != '') ) {
        textPriceChecked = 'Até R$' + laste;
      } else {
        textPriceChecked = 'R$' + initial + ' - R$' + laste;
      }
      $('.ftr-js-price').html(textPriceChecked);
      $('.ftr-js-price').addClass('ftr-background--primary');
    }
  } // Final ftr-js-price.length

}); // Final document.ready
