(function() {
  function gridFix() {
    var selectors = ['.grid-on-slider', '.products-view'];
    for (var i = 0; i < selectors.length; i++) {
      var container = document.querySelectorAll(selectors[i]);

      if (container) {
        for (var j = 0; j < container.length; j++) {
          var id = 'grid-fix-' + i + '-' + j;
          container[j].classList.add(id);

          var style,            columns = container[j].querySelectorAll('.item-column'),
            containerWidth = container[j].clientWidth,
            quantityPerRow = Math.floor(
              containerWidth /
                parseInt(
                  window
                    .getComputedStyle(columns[0])
                    .getPropertyValue('min-width')
                    .replace(/[\s]*[p][x]/, '')
                )
            ),
            quantityLastRow = columns.length > quantityPerRow ? columns.length % quantityPerRow : columns.length,
            emptyQuantity = quantityPerRow - quantityLastRow,
            css =
              '.' + id +
              selectors[i] +
              ':after{content:"";display:block;width:calc(100% / ' +
              quantityPerRow +
              ' * ' +
              emptyQuantity +
              ');}';

          style = document.getElementById(id);

          if (!style) {
            style = document.createElement('style');
            style.id = id;
          }

          style.textContent = css;
          document.body.appendChild(style);
        }
      }
    }
  }
  window.gridFix = gridFix;
})();
