         process_tree SERVICE_TREE, null, true
         localStorage.setItem 'servicemap_lun_index', JSON.stringify index.toJSON()
 
-    $("#search input").typeahead
-        sections: [
+    sections = [
+        {
             name: 'services'
             source: (query, process) ->
                 res_list = index.search query

                    output.push id: service.id, value: service.name_fi, service: service
                process output
            highlight: true
        ]

    markers = []
    $("#search input").on 'typeahead:selected', (ev, item) ->
        center = map.getCenter()
        lat = center.lat.toFixed 5
        lon = center.lng.toFixed 5
        ne = map.getBounds().getNorthEast()
        distance = Math.round(ne.distanceTo center)

        url = "http://www.hel.fi/palvelukarttaws/rest/v2/unit/?service=#{item.id}&lat=#{lat}&lon=#{lon}&distance=#{distance}&callback=?"
        $.getJSON url, (data) ->
            for m in markers
                map.removeLayer m
            for unit in data
                marker = L.marker([unit.latitude, unit.longitude]).addTo map
                popup_html = "<strong>#{unit.name_fi}</strong><div>#{unit.street_address_fi}</div>"
                marker.bindPopup popup_html
                markers.push marker
    window.map = map

    paths:
        app: '../js'

SMBACKEND_BASE_URL = "http://localhost:8000/v1/"

requirejs ['app/map', 'lunr', 'servicetree', 'typeahead.bundle'], (map) ->
    SearchControl = L.Control.extend
        options: {
            position: 'topleft'
        },

        onAdd: (map) ->
            $container = $("<div id='search' />")
            $el = $("<input type='text' name='query' class='form-control'>")
            $el.css
                width: '250px'
