                  ))
   (go (async-search @data ajax-chan)))
 
+(defn maybe-search [data state owner]
+  (when (not (empty? (:text state)))
+    (print @data)
+    (set! js/window.location.hash
+          ((-> @data :routes :search) {:query (:text state)}))
+    (om/set-state! owner :text "")))
+
 ; Key handling ---
 
 (def ENTER 13)

      om/IWillMount
      (will-mount [_]
        (let [ajax-chan (chan 1)]
          (om/set-state! owner :ajax-chan ajax-chan) 
          (async-response-loop data ajax-chan)
          (async-window-scroll data ajax-chan)
          (async-window-resize data)
          (setup-navigation data ajax-chan)))
      om/IRenderState 
      (render-state [this local-state]
        (dom/div nil
                 (case (:current-state data)
                   :search  (om/build render-search data
                                      {:state {:ajax-chan (om/get-state owner :ajax-chan)}})
                   :loading (om/build render-loading data)
                   :loaded  (om/build render-loaded data)
                   :loaded-final (om/build render-loaded data)


(defn async-window-scroll [data ajax-chan]
  (.addEventListener js/window "scroll" 
  (fn []
    (when
      (and
        (= :loaded (:current-state @data))
        (let [body (.. js/document -body)
              html  (.. js/document -documentElement)
              height (max (.. body -scrollHeight)
                          (.. body -offsetHeight)
                          (.. html -clientHeight)
                          (.. html -scrollHeight)
                          (.. html -offsetHeight)
                          )]
          (> 100
              (- height
                (+ (.. js/window -innerHeight)
                    (.. js/window -scrollY))))))
      (om/transact! data #(assoc % :current-state :loading))
      (go (async-search @data ajax-chan))))))

(defn async-window-resize [data]
  (.addEventListener 
    js/window "resize"
    (fn []
      (om/transact! data 
                    #(assoc % :window-width 
                            (.. js/window -innerWidth))))))


(defn setup-navigation [data ajax-chan]
  (secretary/set-config! :prefix "#")

  (om/transact! 
    data #(assoc % :routes
                 {
                  :index 
                  (defroute "/" []
                    (om/transact! data reset-state))

                  :search
                  (defroute "/search/:query" [query]
                    (new-search data query ajax-chan))
                  }))

  (let [h (History.)]
    (goog.events/listen h EventType/NAVIGATE #(secretary/dispatch! (.-token %)))
