     e.stopPropagation()
     e.preventDefault()
     @select(true)
+    @$element.focus()
 
   mouseenter: (e) ->
+    @mousedover = true
     @$menu.find('.active').removeClass('active')
     $(e.currentTarget).addClass('active')
+
+  mouseleave: (e) ->
+    @mousedover = false
+    @hide() if !@focused and @shown
 
 
 $.fn.autocomplete_collection = (option) ->

      data[option]()

$.fn.autocomplete_collection.defaults =
  collection_ui: '<ul class="autocomplete-collection"></ul>'
  items: 8
  menu: '<ul class="autocomplete_collection dropdown-menu"></ul>'
  item: '<li><a href="#"></a></li>'

      .on('click', $.proxy(@click, @))
      .on('mouseenter', 'li', $.proxy(@mouseenter, @))


  move: (e) ->
    return false if !@shown

    switch e.keyCode
      when 9, 13, 27
        e.preventDefault()
        break
      when 38
        e.preventDefault()
        @prev()
        break
      when 40
        e.preventDefault()
        @next()
        break

    e.stopPropagation()

  keydown: (e) ->
    @suppressKeyPressRepeat = ~$.inArray(e.keyCode, [40,38,9,13,27])
    @move(e)

  keypress: (e) ->
    return if @suppressKeyPressRepeat
    @move(e)

  keyup: (e) ->

    switch e.keyCode
      when 40, 38 # down / up arrow
        break
      when 9 # tab
        return if !@shown
        @select()
      when 13 # enter
        if !@shown
          @onenter() if typeof @onenter == "function"
          @select() if @allowNew
        else
          @select()
        break
      when 27 # escape
        return if !@shown
        @hide()
        break
      else
        @lookup()

    e.stopPropagation()
    e.preventDefault()

  focus: (e) ->
    @focused = true

  blur: (e) ->
    @focused = false
    that = @
    e.stopPropagation()
    e.preventDefault()
