
function updateVisibleProductsCount() {
  jQuery('.product_listing').each(function() {
    var id = jQuery(this).attr('id').replace('product_listing_', '');

    var product_count = jQuery('.product_row_' + id + ':visible').length;
    if (jQuery('#visible_products_count_' + id).length > 0) {
      jQuery('#visible_products_count_' + id).html(product_count.toString());
    }
  });
}

jQuery(document).ready(
  function() {
    jQuery('#filter_dropdown').change(
      function(e) {
        var _this = jQuery(this);
        var uri = window.location.href;
        if (_this.val() == -1) {
          uri = window.location.href.replace(/\??manufacturers_id=\d+&?/,
            '');
        } else {
          if (window.location.href.match(/manufacturers_id=/)) {
            uri = window.location.href.replace(/manufacturers_id=\d+(&?)/,
              'manufacturers_id=' + _this.val() + '$1');
          } else {
            if (!window.location.href.match(/\?/)) {
              uri = window.location.href + '?manufacturers_id='
              + _this.val();
            } else {
              uri = window.location.href + '&manufacturers_id='
              + _this.val();
            }
          }
        }

        window.location.href = uri;
      });

    if(jQuery('#sort_menu').length) {
      jQuery('#sort_menu').change(
        function(e) {
          var _this = jQuery(this);
          var uri = window.location.href;
          if (_this.val() == -1) {
            uri = window.location.href.replace(/\??sortuj=[^&]+&?/, '');
          } else {
            if (window.location.href.match(/sortuj=/)) {
              uri = window.location.href.replace(/sortuj=[^&]+(&?)/,
                'sortuj=' + _this.val() + '$1');
            } else {
              if (!window.location.href.match(/\?/)) {
                uri = window.location.href + '?sortuj=' + _this.val();
              } else {
                uri = window.location.href + '&sortuj=' + _this.val();
              }
            }
          }

          window.location.href = uri;
        });
    }
  });

