       when 'r' then 'rank'
       when 'q' then 'query'
 
+  buildSearchQuery: (query) =>
+    $.extend {'q': query}, @getFilterState(), @getSortState()
+
   search: =>
     searchText = $('#searchbox').val()?.trim()
     @showLoader()


    searchText = $('#searchbox').val()?.trim()

    $.ajax(@state.paging.url,
      method: 'get'
      dataType: 'json'
      data: $.extend({'q': searchText}, @getFilterState(), @getSortState(),
        'page': @state.paging.page + 1)).done ((data) =>
          more = data['data'].length > 10
          @setState
            beatmaps: @state.beatmaps.concat(data['data'])
            paging:
              page: @state.paging.page + (if more then 1 else 0)
              url: @state.paging.url
              more: more
            loading: false
    ).bind(this)

  showLoader: =>
    @setState loading: true

      if !@state.just_restored
        history.pushState(@state, "¯\_(ツ)_/¯", "/beatmaps/?#{params.join('&')}")

      $.ajax @state.paging.url,
        method: 'get'
        dataType: 'json'
        data: @buildSearchQuery(searchText)
      .done (data) =>
        newState =
          beatmaps: data['data']
          query: searchText
          paging:
            page: 1
            url: @state.paging.url
            loading: false
            more: data['data'].length > 10
          just_restored: false
          loading: false

        @setState newState, ->
            $(document).trigger 'beatmap:search:done'

  loadMore: =>
    if @state.loading or @state.paging.loading or !@state.paging.more
