             [secretary.core :as secretary]))
 
 (def nav-seq (concat (map str "#abcdefghijklmnopqrstuvwxyz") ["all"]))
-
+(def audio-node (js/Audio.))
 (def track-list (chan))
 (def player-ctrl (chan))
-
+(def stream-position (chan))
+(aset audio-node "onended" (fn [e] (put! player-ctrl :next)
+                             false))
+;; (aset audio-node "onabort" (fn [e] (put! player-ctrl :next)
+;;                              false))
+;; (aset audio-node "onerror" (fn [e] (put! player-ctrl :next)
+;;                              false))
+
+(aset audio-node "ontimeupdate" (fn[e] (put! stream-position :changed)))
 
 (defn nav-item [x]
   (let [nav-url (utils/format "#/nav/%s" (utils/encode x))]

              } "add"]]]])))))


(defn ctrl-audio-node [node action]
  (let [seek-offset 5]
    (case action
    :stop (do (.pause node)
              (aset node "currentTime" 0))
    :play (.play node)
    :pause (.pause node)
    :seek-backward (aset node "currentTime" (- (.-currentTime node) 1))
    :seek-forward (aset node "currentTime" (+ (.-currentTime node) 1)))))


(defn audio-elem [data owner]
  (reify
    om/IInitState
    (init-state [this]
      (go (loop []
            (let [track-src (<! track-list)
                  my-play-list (om/get-state owner :play-list)
                  node (om/get-state owner :audio)]
              (aset node "src" track-src)
              (ctrl-audio-node node :play)
              (om/set-state! owner :current-src track-src))
            (recur)))
      (go (loop []
            (let [ctrl (<! player-ctrl)
                  node (om/get-state owner :audio)]
              (om/set-state! owner :ctrl ctrl)
              (ctrl-audio-node node ctrl))
            (recur)))
      {:current-src "/api/artists/fantômas/albums/delìrium còrdia/tracks/228"
       :current-offset 0
       :ctrl :stop
       :audio (js/Audio.)})
    om/IRender
    (render [this]
      (let [my-play-list (om/get-state owner :play-list)
            ctrl-current (om/get-state owner :ctrl)
            node (om/get-state owner :audio)
            is-playing? (not (.-paused node))]
        (html [:div
               [:ul
                ;; [:li [:i.fa.fa-fast-backward]]
                [:li {:on-click (fn [e] (put! player-ctrl :seek-backward))}
                 [:i.fa.fa-step-backward]]
                [:li {:on-click (fn [e] (put! player-ctrl (if is-playing? :pause :play)))}

                 [:i.fa.fa-stop]]
                [:li {:on-click (fn [e] (put! player-ctrl :seek-forward))}
                 [:i.fa.fa-step-forward]]
                [:li {:on-click (fn [e] (put! player-ctrl :next))}
                 [:i.fa.fa-fast-forward]]
                ]])))))

(defn view-debug [data owner]
