(function($) {
  $.widget("custom.combobox", {
    _create: function() {
      this.wrapper = $("<span>")
        .addClass("custom-combobox")
        .insertAfter(this.element);

      this.element.hide();
      this._createAutocomplete();
      this._createShowAllButton();
    },

    _createAutocomplete: function() {
      var selected = this.element.children(":selected"),
        first = this.element.children(':first'),
        value = selected.val() ? selected.text() : first.text();

      this.input = $("<input>")
        .appendTo(this.wrapper)
        .val(value)
        .attr("title", "")
        .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
        .autocomplete({
          delay: 0,
          minLength: 0,
          source: $.proxy(this, "_source")
        })
        .tooltip({
          tooltipClass: "ui-state-highlight"
        });

      this._on(this.input, {
        autocompleteselect: function(event, ui) {
          ui.item.option.selected = true;

          this._trigger("select", event, {
            item: ui.item.option
          });

          this.element.trigger('change');
        },

        autocompletechange: "_removeIfInvalid",

        blur: function(event) {
          this.element.trigger('change');
        },

        keypress: function(event) {
          if (event.keyCode == 13) {
            this.element.trigger('change');
          }
        }
      });
    },

    _createShowAllButton: function() {
      var input = this.input,
        wasOpen = false;

      $("<a>")
        .attr("tabIndex", -1)
        .attr("title", "Show All Items")
        .tooltip()
        .appendTo(this.wrapper)
        .button({
          icons: {
            primary: "ui-icon-triangle-1-s"
          },
          text: false
        })
        .removeClass("ui-corner-all")
        .addClass("custom-combobox-toggle ui-corner-right")
        .mousedown(function() {
          wasOpen = input.autocomplete("widget").is(":visible");
        })
        .click(function() {
          input.focus();

          // Close if already visible
          if (wasOpen) {
            return;
          }

          // Pass empty string as value to search for, displaying all results
          input.autocomplete("search", "");
        });
    },

    _source: function(request, response) {
      var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
      response(this.element.children("option").map(function() {
        var text = $(this).text();
        if (this.value && (!request.term || matcher.test(text)))
          return {
            label: text,
            value: text,
            option: this
          };
      }));
    },

    _removeIfInvalid: function(event, ui) {
      $('option[data-custom]', this.element).each(function(index, option) {
        $(option).remove();
      });

      // Selected an item, nothing to do
      if (ui.item) {
        return;
      }

      // Search for a match (case-insensitive)
      var value = this.input.val(),
        valueLowerCase = value.toLowerCase(),
        valid = false;
      this.element.children("option").each(function() {
        if ($(this).text().toLowerCase() === valueLowerCase) {
          this.selected = valid = true;
          return false;
        }
      });

      // Sanitize value if needed.
      var sanitizedValue;
      var handler = this.element.data('handler');

      switch (handler) {
        case 'numeric':
          sanitizedValue = value.replace(/[^0-9.,]/g, '');
          break;

        default:
          sanitizedValue = value;
          break;
      }

      // Found a match, nothing to do
      if (valid) {
        return;
      }

      // Remove invalid value if handler is attached.
      if (!value.length || !handler) {
        var defaultValue = this.element.children(':first').text();
        this.input.val(defaultValue);
        this.element.val("");
        this.input.data("ui-autocomplete").term = "";
      }
      else {
        $(this.element).append('<option value="' + sanitizedValue + '" selected="selected" data-custom="true">' + value + '</option>');
        return;
      }
    },

    _destroy: function() {
      this.wrapper.remove();
      this.element.show();
    }
  });
})(jQuery);
