 (defn set-minutes! [new-mins]
   (swap! selected-time doto (.setMinutes new-mins)))
 
-(defn use-index-time! [index]
-  (swap! selected-time #(-> (@snapshot-list index) :time Date.)))
+(defn sync-time-with-snapshot! []
+  (swap! selected-time #(js/Date. (:time @snapshot-index))))
+
+; TODO: This could be improved. Probably need to rethink my data model
+(defn show-closest-snapshot! []
+  (let [timestamp (.getTime @selected-time)]
+        earlier-index (keep-indexed #(if (>= timestamp (:time %2)) %1) @snapshot-list)
+        later-index (dec earlier-index)
+        later (:time (@snapshot-list later-index))
+        earlier (:time (@snapshot-list earlier-index))
+        display-index (if (< (- later timestamp) (- timestamp earlier))
+                        later-index
+                        earlier-index))
+    (swap! selected-index #(display-index)))
+
+(defn set-url! [timestamp]
+  (let [base-url (.split js/document.URL "?")
+        new-url (str base-url "?time=" timestamp)])
+    (.replaceState js/history nil nil new-url))
 
 ;;------------------------------------------------------------------------------
 ;; Watchers
 ;;------------------------------------------------------------------------------
 
-(defn )
-
-; TODO: Find closest index
 (defn on-selected-time-change [_ _ _ new-time]
   (let [hour (.getHours new-time)
         meridian (if (> 11 hour) "PM" "AM")

(defn click-toggle-meridian []
  (let [hours (+ (get-hours) 12)
        hours (if (>= 24 ) (- 24 hours) hours)]
    (set-hours! hours)))

(defn click-decrement-hour []
  (set-hours! (dec (get-hours))))

(defn click-increment-hour []
  (set-hours! (inc (get-hours))))

(defn click-decrement-Minute []
  (set-minutes! (dec (get-minutes))))

(defn click-increment-minute []
  (set-minutes! (inc (get-minutes)))))

(defn click-later []
  (if (pos? @snapshot-index)
    (use-index-time! (dec @snapshot-index))))

(defn click-earlier []
  (if (>= @snapshot-index (count @snapshot-list)
    (use-index-time! (inc @snapshot-index)))))

(defn add-events []
  (aset js/window "onpopstate" on-popstate)

;; Atoms
;;------------------------------------------------------------------------------

(def snapshot-list (atom nil))
(def snapshot-index (atom nil)) ; can this be derived from current time, quickly?
(def selected-time (atom nil))
(def url-update-timeout (atom nil)) ; This probably doesn't need to be atom, maybe be clojure feature that handles this

;;------------------------------------------------------------------------------
;; Timepicker
