 (def ^:dynamic *output-dir*)
 
 (def def-pause 1000)
-(def driver (wc/new-webdriver {:browser :phantomjs}))
+(def driver (delay (wc/new-webdriver {:browser :phantomjs})))
 (def built-in-formatter (f/formatters :basic-date-time))
+
+(defn int [s] (Integer. (re-find  #"\d+" s )))
 
 (defn date []
   (let [nlocal (t/to-time-zone (t/now) (t/time-zone-for-offset -0))]
     (f/unparse (f/formatter-local "yyyy-MM-dd_hh:mm")
                nlocal)))
 
+(defn render-pause [pause-ms]
+  (when-not (= pause-ms 0)
+    (println "pausing for : " pause-ms " ms")
+    (Thread/sleep pause-ms)))
+
 (defn screenshot
   ([name]
    (let [nm (if name (str "-" name) "")
          s-file (str *output-dir* "/" (date) "_" (.getName *crison-file*) "-screenshot" nm ".png")]
-      (wc/get-screenshot driver :file s-file)))
+      (wc/get-screenshot @driver :file s-file)))
   ([] (screenshot nil)))
 
 (defn source
   ([name]
    (let [nm (if name (str "-" name) "")
          s-file (str *output-dir* "/" (date) "_" (.getName *crison-file*) "-source" nm ".txt")]
-      (spit s-file (wc/page-source driver))))
+      (when name (spit s-file (wc/page-source @driver)))))
   ([] (source nil)))
 
-(defn title [] (wc/title driver))
+(defn title [] (wc/title @driver))
 
 (defn title? [x] (is (= x (title)) x))
 
-(defn go [x] (wc/to driver x))
+(defn go [x] (wc/to @driver x))
 
-(defn el [x] (-> driver (wc/find-element x)))
+(defn el [x] (-> @driver (wc/find-element x)))
 
 ;; currently a catchall from the multimethod
-(defn ? [x] (is (-> driver (wc/find-element x) wc/exists?) (str "Fail on : " x)))
+(defn ? [x] (is (-> @driver (wc/find-element x) wc/exists?) (str "Fail on : " x)))
 
 (defmulti decode (fn[x] (ffirst x)))
 
 (defmethod decode :url! [x]
   (go (:url! x))
-  (Thread/sleep (or (:pause x) def-pause))
+  (render-pause (or (:pause x) def-pause))
   (screenshot (:screenshot x))
   (source (:source x)))
 
 (defmethod decode :click! [x]
-  (let [e (:click! x)]
+  (println "x : " x)
+  (println ":click! x " (:click! x))
+  (let [e (:click! x)
+        orig-handles (count (wc/window-handles @driver))]
+    (println "e : " e)
     (if (string? e)
-      (-> driver (wc/find-element {:id e}) wc/click)
-      (-> driver (wc/find-element e) wc/click))
-    (Thread/sleep (or (:pause x) def-pause))
+      (-> @driver (wc/find-element {:id e}) wc/click)
+      (-> @driver (wc/find-element e) wc/click))
+    (render-pause (or (:pause x) def-pause))
+    (when (> (count (wc/window-handles @driver)) orig-handles)
+      (wc/switch-to-other-window @driver))
     (screenshot (:screenshot x))
     (source (:source x))))
 

    (doseq [e head]
      (if (:clear! e)
        (let [field (dissoc e :clear!)]
          (-> driver (wc/find-element field) (wc/clear)))
        (let [txt-val (:text! e)
              field (dissoc e :text!)]
          (-> driver (wc/find-element field) (wc/input-text txt-val)))))))

(defmethod decode :fill-submit! [{:keys [fill-submit! pause] :as x}]
  (if (seq (filter #(:text! %) (butlast fill-submit!)))
    (compute-fill x)
    (wf/quick-fill-submit driver (:fill! x)))
  (-> driver (wc/find-element (last fill-submit!)) wc/click)
  (Thread/sleep (or pause def-pause))
  (screenshot (:screenshot x)))

(defmethod decode :fill! [{:keys [fill! pause] :as x}]
  (if (seq (filter #(:text! %) (butlast fill!)))
    (compute-fill x)
    (wf/quick-fill-submit driver (:fill! x)))
  (Thread/sleep (or pause def-pause))
  (screenshot (:screenshot x)))

(defmethod decode :title [x] (title? (:title x)))

(defmethod decode :default [x] (? x))

(deftest tests
  (let [width (env :crison-width 1024)
        height (env :crison-height 800)
        fs (do/filter-exts (file-seq (file *input-dir*)) ["csn"])]
    (println "crison-width : " width)
    (println "crison-height : " height)
    (wc/resize @driver {:width (int width) :height (int height)})
    (doseq [f fs]
      (binding [*crison-file* f]
        (doseq [t (read-string (slurp f))] (decode t))
