// Generated by CoffeeScript 1.9.1
(function() {
  var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    hasProp = {}.hasOwnProperty;

  define(["studiogdo/skeleton"], function(Skeleton) {
    var Filter;
    return Filter = (function(superClass) {
      extend(Filter, superClass);

      function Filter() {
        this.showFilterIfNotEmpty = bind(this.showFilterIfNotEmpty, this);
        this.resetFilter = bind(this.resetFilter, this);
        this.filterGo = bind(this.filterGo, this);
        this.showFilter = bind(this.showFilter, this);
        this.toggleFilter = bind(this.toggleFilter, this);
        this.filter = $(".filter");
        this.filter_content = this.filter.find(".filter-content");
        this.filter.find('.toggle-filter').on("click", (function(_this) {
          return function(evt) {
            if (evt != null) {
              evt.preventDefault();
            }
            return _this.toggleFilter();
          };
        })(this));
        this.filter.find('.reset-filter').on("click", (function(_this) {
          return function(evt) {
            return _this.resetFilter(evt);
          };
        })(this));
        this.filter.find('.search-filter').on("click", (function(_this) {
          return function(evt) {
            return _this.filterGo(evt);
          };
        })(this));
        $(this.filter).on('keypress', (function(_this) {
          return function(evt) {
            if (evt.which === 13) {
              return _this.filterGo(evt);
            }
          };
        })(this));
        $(this.filter).on('submit', (function(_this) {
          return function(evt) {
            return _this.filterGo(evt);
          };
        })(this));
        this.showFilterIfNotEmpty();
      }

      Filter.prototype.toggleFilter = function() {
        return this.filter_content.toggleClass("hidden");
      };

      Filter.prototype.showFilter = function() {
        return this.filter_content.removeClass("hidden");
      };

      Filter.prototype.filterGo = function(evt) {
        var callback, params;
        if (evt != null) {
          evt.preventDefault();
        }
        params = this.filter.serializeForm();
        callback = (function(_this) {
          return function() {
            return window.location.reload();
          };
        })(this);
        return this.postEmpty(callback, params);
      };

      Filter.prototype.resetFilter = function(evt) {
        this.filter_content.find("input, select").val("");
        return this.filterGo(evt);
      };

      Filter.prototype.showFilterIfNotEmpty = function() {
        var input, not_empty_inputs, not_empty_selects, select;
        not_empty_inputs = (function() {
          var i, len, ref, results;
          ref = this.filter_content.find("input");
          results = [];
          for (i = 0, len = ref.length; i < len; i++) {
            input = ref[i];
            if (input.value.length) {
              results.push(input);
            }
          }
          return results;
        }).call(this);
        not_empty_selects = (function() {
          var i, len, ref, results;
          ref = this.filter_content.find("select");
          results = [];
          for (i = 0, len = ref.length; i < len; i++) {
            select = ref[i];
            if (select.value.length) {
              results.push(select);
            }
          }
          return results;
        }).call(this);
        if (not_empty_inputs.length || not_empty_selects.length) {
          return this.showFilter();
        }
      };

      return Filter;

    })(Skeleton);
  });

}).call(this);
