                ])
      (fn [director]
        (binding [*director* director]
-        (log/info logger " Images loaded. Creating scene.")
-        (doto director
-          (.addAudio "chime" "sounds/22267__zeuss__the-chime.ogg" ;; "sounds/22267__zeuss__the-chime.mp3"
-                     ) 
-          (.addAudio "doppp" "sounds/8001__cfork__cf-fx-doppp-01.ogg" ;; "sounds/8001__cfork__cf-fx-doppp-01.wav"
-                     )
-          (.addAudio "flopp" "sounds/8006__cfork__cf-fx-flopp-03-dry.ogg" ;; "sounds/8006__cfork__cf-fx-flopp-03-dry.wav"
-                     )
-          (.addAudio "munching" "sounds/27877__inequation__munching.ogg" ;; "sounds/27877__inequation__munching.wav"
-                     )
-          (.addAudio "song" "sounds/448614_Happiness_Alone.ogg"))
-        (let [scene (. director (createScene))
-              background (actor :image "room")
-              basket (basket scene :x 440 :y 240)
-              bookshelf (bookshelf scene)
-              counter (count-actor 6 :on-total-reached (fn [e] (add! scene (winning-screen scene))))
-              basket-objects (map
-                              #(let [actor (apply animated-actor (apply concat %))]
-                                 (make-draggable-into
-                                  actor
-                                  basket scene 
-                                  (fn [] (throw-into-basket director scene basket actor counter))
-                                  :valid-fn (fn [] (and (not (.isOpened basket)) (not (.eating basket))))))
-                              [{:image "football" :animation-indices [0] :draggable? true :thumbnail [60 60]}
-                               {:image "car" :animation-indices [0] :draggable? true :thumbnail [60 60]}
-                               {:image "blocks" :animation-indices [0] :draggable? true}
-                               {:image "teddy" :animation-indices [0 1 0 2] :draggable? true}])
-              bookshelf-objects (map
-                                 #(let [actor (apply animated-actor (apply concat %))]
-                                    (make-draggable-into
-                                     actor bookshelf scene 
-                                     (fn []
-                                       (.setExpired actor true)
-                                       (. bookshelf (inc-book-count))
-                                       (. counter (inc)))))
-                                 [{:image "book" :animation-indices [2] :default-index 2 :key-frames 3 :draggable? true}
-                                  {:image "book" :animation-indices [0] :default-index 0 :key-frames 3 :draggable? true}])
-              objects (concat basket-objects bookshelf-objects)] 
-          (log/info logger " Displaying scene")
-          (add! scene background)
-          (add! scene basket)
-          (add! scene counter) 
-          (add! scene bookshelf)
+         (log/info logger " Images loaded. Creating scene.")
+         (doto director
+           (.addAudio "chime" "sounds/22267__zeuss__the-chime.ogg" ;; "sounds/22267__zeuss__the-chime.mp3"
+                      ) 
+           (.addAudio "doppp" "sounds/8001__cfork__cf-fx-doppp-01.ogg" ;; "sounds/8001__cfork__cf-fx-doppp-01.wav"
+                      )
+           (.addAudio "flopp" "sounds/8006__cfork__cf-fx-flopp-03-dry.ogg" ;; "sounds/8006__cfork__cf-fx-flopp-03-dry.wav"
+                      )
+           (.addAudio "munching" "sounds/27877__inequation__munching.ogg" ;; "sounds/27877__inequation__munching.wav"
+                      )
+           (.addAudio "song" "sounds/448614_Happiness_Alone.ogg"))
+         (let [scene (. director (createScene))
+               background (actor :image "room")
+               basket (basket scene :x 440 :y 240)
+               bookshelf (bookshelf scene)
+               counter (count-actor 6 :on-total-reached (fn [e] (add! scene (winning-screen scene))))
+               basket-objects (map
+                               #(let [actor (apply animated-actor (apply concat %))]
+                                  (make-draggable-into
+                                   actor
+                                   basket scene 
+                                   (fn [] (throw-into-basket director scene basket actor counter))
+                                   :valid-fn (fn [] (and (not (.isOpened basket)) (not (.eating basket))))))
+                               [{:image "football" :animation-indices [0] :draggable? true :thumbnail [60 60]}
+                                {:image "car" :animation-indices [0] :draggable? true :thumbnail [60 60]}
+                                {:image "blocks" :animation-indices [0] :draggable? true}
+                                {:image "teddy" :animation-indices [0 1 0 2] :draggable? true}])
+               bookshelf-objects (map
+                                  #(let [actor (apply animated-actor (apply concat %))]
+                                     (make-draggable-into
+                                      actor bookshelf scene 
+                                      (fn []
+                                        (.setExpired actor true)
+                                        (. bookshelf (inc-book-count))
+                                        (. counter (inc)))))
+                                  [{:image "book" :animation-indices [2] :default-index 2 :key-frames 3 :draggable? true}
+                                   {:image "book" :animation-indices [0] :default-index 0 :key-frames 3 :draggable? true}])
+               objects (concat basket-objects bookshelf-objects)] 
+           (log/info logger " Displaying scene")
+           (add! scene background)
+           (add! scene basket)
+           (add! scene counter) 
+           (add! scene bookshelf)
+           ;; (let [actor (actor :fill-style "black" :size [50 50])]
+           ;;   (add! scene actor)
+           ;;   ;; BUG: (create-animation [:move :to [[400 300] [500 200]] :dur 6000])
+           ;;   ;; NO BUG: (create-animation [:move :to [[400 300] [500 250]] :dur 6000])  ;; what is the difference?
+           ;;   ((create-animation [:move :to [[400 300] [500 250]] :dur 2000]
+           ;;                      (fn [target] (log/info logger "first stage finished!"))
+           ;;                      [:move :up 100 :dur 3000]
+           ;;                      (fn [target] (log/info logger "second stage finished!")))
+           ;;    :on-finish (fn [actor] (log/info logger "Finished!"))))
          
-          (dotimes [index (count objects)]
-            (let [object (nth objects index)]
-              (add!
-               scene
-               object
-               :position [(+ (rand-int 30) (* (/ 600 (count objects)) index) 100)
-                          (+ (rand-int 50) 360)])))
-          (add! scene (winning-screen scene))
-          ))))))
+           (dotimes [index (count objects)]
+             (let [object (nth objects index)]
+               (add!
+                scene
+                object
+                :position [(+ (rand-int 30) (* (/ 600 (count objects)) index) 100)
+                           (+ (rand-int 50) 360)])))
+           ;; (add! scene (winning-screen scene))
+           ))))))

  CAAT.Behavior
  (behavior [s] s)
  cljs.core.Vector
  (behavior [v]
                (let [[kind & args] v] 
                  (apply (kind kw->behavior-fn) args))))

(defn animation-specs->behavior
  "Given a TARGET and any number of animation SPECS, create & return a

                      last-caat-behavior)))))
       container)))

(def kw->behavior-fn2
     {:move move-behavior
      :rotate rotate-behavior
      :scale scale-behavior
      :alpha alpha-behavior})

(defn- behavior-spec->behavior
  "Given a behavior spec like [:move ...], transform that into an actual CAAT.Behavior."
  [spec target]
  (let [[kind & args] spec]
    (apply (kw->behavior-fn2 kind) :target target args)))


(defn create-animation
  "Create an animation. This will return a function which can be
applied to a target to animate it."
  ([] (fn [target & {:keys [on-finish]}]
        (when (fn? on-finish)
         (on-finish target))))
  ([spec]
     (if (fn? spec)
       (fn [target & {:keys [on-finish] :or {on-finish nil}}]
         (spec target)
         (when (fn? on-finish)
           (on-finish target)))
       (let [director *director*]
         (fn [target & {:keys [on-finish] :or {on-finish nil}}]
           (log/info logger (str "Running animation spec: " spec "; Current (x,y):" (.x target) "," (.y target)))
           (let [bh (behavior-spec->behavior spec target)
                 dur (. bh (getDuration))]
             (when (fn? on-finish)
               (.addListener
                bh
                (clj->js
                 {:behaviorExpired
                  (fn [bh time actor]
                    (binding [*director* director]
                      (on-finish actor)))})))
             (.addBehavior target bh))))))
  ([spec1 spec2 & more]
     (fn [target & {:keys [on-finish]}]
       ((create-animation spec1)
        target
        :on-finish
        (fn [target]
          ((create-animation spec2)
           target
           :on-finish
           (fn [target] 
             ((apply create-animation more)
              target
              :on-finish
              on-finish))))))))

(defn animate!
  ([target & specs]
     ((apply create-animation specs) target)))


(defn animation
  [target & animations]
  {:pre [(actor? target)]}
