(function() {
  var TransitDBUI;
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  TransitDBUI = (function() {
    TransitDBUI.prototype.selectedAutocomplete = null;
    TransitDBUI.prototype.autocompleteCache = {};
    function TransitDBUI() {
      this.autocompleteCb = __bind(this.autocompleteCb, this);;      $.extend($.ui.autocomplete.prototype, {
        _renderItem: function(ul, item) {
          return $("<li></li>").data("item.autocomplete", item).append($("<a></a>").html(item.label)).appendTo(ul);
        }
      });
      this.initializeFeedbackForm();
      this.initializeAutocomplete();
      $('input[placeholder], textarea[placeholder]').placeholder();
    }
    TransitDBUI.prototype.initializeFeedbackForm = function() {
      var active_icon, feedback_form, feedback_form_container, feedback_form_toggle, feedback_submit, feedback_type, inactive_icon;
      inactive_icon = 'ui-icon-carat-1-ne';
      active_icon = 'ui-icon-carat-1-sw';
      feedback_form = $('#feedback-form');
      $('input[name="context"]').val(document.location);
      $('input[name="referer"]').val(document.referrer);
      feedback_form_container = $('#feedback-form-container');
      feedback_form_toggle = $('.feedback-form-toggle, .feedback-form-toggle2');
      feedback_type = feedback_form.find('select[name="type"]').first();
      feedback_submit = feedback_form.find('input[type="submit"]').first();
      feedback_form_toggle.children('span').addClass(inactive_icon);
      feedback_form_toggle.click(__bind(function() {
        feedback_form_container.toggle('slide', {
          direction: 'down'
        }, 'fast');
        feedback_form_toggle.toggleClass('active').children().toggleClass(inactive_icon).toggleClass(active_icon);
        feedback_form.find('.status').hide();
        feedback_form.find('textarea').val('');
        return feedback_submit.removeAttr('disabled');
      }, this));
      feedback_type.change(__bind(function() {
        feedback_form.find('.caption_options > div').hide();
        return feedback_form.find('.' + feedback_type.val()).toggle('slide', {
          direction: 'down'
        }, 'fast');
      }, this));
      return feedback_submit.click(__bind(function() {
        var xhr;
        feedback_submit.attr('disabled', 'disabled');
        feedback_form.find('.status').hide();
        feedback_form.find('.status.submitting').toggle('slide', {
          direction: 'left'
        }, 'fast');
        xhr = $.post(feedback_form.attr('action') + '?mode=js', feedback_form.serialize());
        xhr.success(__bind(function() {
          return _.delay(__bind(function() {
            feedback_form.find('.status').hide();
            return feedback_form.find('.status.sent').toggle('slide', {
              direction: 'left'
            }, 'fast');
          }, this), 1000);
        }, this));
        return xhr.error(__bind(function() {
          return _.delay(__bind(function() {
            feedback_submit.removeAttr('disabled');
            feedback_form.find('.status').hide();
            return feedback_form.find('.status.fail').toggle('slide', {
              direction: 'left'
            }, 'fast');
          }, this), 1000);
        }, this));
      }, this));
    };
    TransitDBUI.prototype.autocompleteCb = function(request, response) {
      if (this.autocompleteCache[request.term]) {
        return response(this.autocompleteCache[request.term]);
      }
      return $.getJSON('/ac', request, __bind(function(data, status, xhr) {
        var autocompletions;
        autocompletions = _.map(data, function(result) {
          var parts;
          parts = result[0].split('|');
          return {
            label: parts[1],
            value: result[1],
            kind: parts[0]
          };
        });
        this.autocompleteCache[request.term] = autocompletions;
        return response(autocompletions);
      }, this));
    };
    TransitDBUI.prototype.zfill = function(n, w) {
      w -= n.toString().length;
      n = n.toString();
      while (w > 0) {
        n = '0' + n;
        w--;
      }
      return n;
    };
    TransitDBUI.prototype.initializeAutocomplete = function() {
      var searchbox;
      searchbox = $('#searchbox');
      searchbox.autocomplete({
        source: this.autocompleteCb,
        autoFocus: true,
        select: __bind(function(evt, ui) {
          this.selectedAutocomplete = ui.item;
          return $('#searchform').submit();
        }, this)
      });
      return $('#searchform').submit(__bind(function() {
        var sa, val;
        sa = this.selectedAutocomplete;
        if (sa) {
          if (sa.kind === 'R') {
            window.location = "/route/" + sa.value + "/";
          }
          if (sa.kind === 'S') {
            window.location = "/stop/" + sa.value + "/";
          }
          return false;
        } else {
          val = $('#searchbox').val();
          if (val.length === 5 && parseInt(val)) {
            window.location = "/stop/" + val + "/";
          }
          if (val.length <= 3) {
            if (parseInt(val)) {
              val = this.zfill(val, 3);
            }
            return window.location = "/route/" + val + "/";
          }
        }
      }, this));
    };
    return TransitDBUI;
  })();
  window.TransitDBUI = TransitDBUI;
  $(function() {
    return window.ui = new TransitDBUI;
  });
}).call(this);

