(function() {
  'use strict';
  var offerListSorters = angular.module('offerListSorters', []);

  offerListSorters.value('offerOrderState', {});

  offerListSorters.directive('offersSorter', ['offerOrderState',
    function(offerOrderState) {
      return {
        scope: {
          orderBy: '@',
          isNumeric: '@'
        },
        controller: function($scope, $element, $attrs, $transclude) {
          $scope.isAscending = true;
          $scope.clicked = function() {
            $scope.isAscending = !$scope.isAscending;
            offerOrderState.orderBy = $scope.orderBy;
            offerOrderState.isAscending = $scope.isAscending;
          };
          $scope.isActive = function() {
            return $scope.orderBy === offerOrderState.orderBy;
          };
        },
        restrict: 'E',
        templateUrl: 'src/mainView/offerList/offersSorterDirective.html',
        replace: true,
        transclude: true
      };
    }
  ]);

  offerListSorters.filter('order', ['orderByFilter', 'offerOrderState',
    function(orderByFilter, offerOrderState) {
      return function(offers) {
        var asc = offerOrderState.isAscending;
        var reverse = typeof asc === 'undefined' ? false : !asc;
        var reversePrefix = reverse ? '-' : '';
        return orderByFilter(offers, reversePrefix+offerOrderState.orderBy);
      };
    }
  ]);
})();

(function() {
  'use strict';
  var offerListSorters = angular.module('offerListSorters', []);

  offerListSorters.value('offerOrderState', {});

  offerListSorters.directive('offersSorter', ['offerOrderState',
    function(offerOrderState) {
      return {
        scope: {
          orderBy: '@',
          isNumeric: '@'
        },
        controller: function($scope, $element, $attrs, $transclude) {
          $scope.isAscending = true;
          $scope.clicked = function() {
            $scope.isAscending = !$scope.isAscending;
            offerOrderState.orderBy = $scope.orderBy;
            offerOrderState.isAscending = $scope.isAscending;
          };
          $scope.isActive = function() {
            return $scope.orderBy === offerOrderState.orderBy;
          };
        },
        restrict: 'E',
        templateUrl: 'src/mainView/offerList/offersSorterDirective.html',
        replace: true,
        transclude: true
      };
    }
  ]);

  offerListSorters.filter('order', ['orderByFilter', 'offerOrderState',
    function(orderByFilter, offerOrderState) {
      return function(offers) {
        if (!offerOrderState.orderBy) {
          return offers;
        }
        var asc = offerOrderState.isAscending;
        var reverse = typeof asc === 'undefined' ? false : !asc;
        var reversePrefix = reverse ? '-' : '';
        return orderByFilter(offers, reversePrefix+offerOrderState.orderBy);
      };
    }
  ]);
})();

fix offer sorting in chrome