      :else (when (= (. status-el textContent) slug-not-unique-err)
              (ui/remove-error status-el slug-el slug-label-el)))))
 
-(def *feed* (atom {:default-slug-format "/{feed-name}/{document-title}"}))
-
-(defn fetch-feed-details! [e]
-  (let [xhr (.target e)
-        status (. xhr (getStatus))]
-    (when (= status 200)
-      (let [json (js->clj (. xhr (getResponseJson)))]
-        (swap! *feed* assoc :name ("name" json)
-                            :default-slug-format ("default-slug-format" json))))))
-
 (defn create-document-title-token [title]
   (string/join "-" (filter #(not (string/blank? %)) (.split title #"[^a-zA-Z0-9]"))))
 
-(defn create-slug [title]
+(defn create-slug [feed title]
   (let [today (util/date-now)]
-    (loop [slug (:default-slug-format @*feed*)
+    (loop [slug (:default-slug-format feed)
            substitutions [["{document-title}" (create-document-title-token title)]
-                          ["{feed-name}" (:name @*feed*)]
+                          ["{feed-name}" (:name feed)]
                           ["{year}" (:year today)]
                           ["{month}" (:month today)]
                           ["{day}" (:day today)]

        (ui/remove-error (dom/getElement "status-message")
                           slug-el
                           (dom/getElement "slug-label"))
        (sync-slug-with-title)))))

(defn validate-slug []
  (if (.checked (dom/getElement "custom-slug"))

         (goog.ui.editor.ToolbarController. editor toolbar)
         (. editor (makeEditable))))))

(defn start-default-mode! [slug status feed]
  (if (= :new status)
    (do
      (util/set-page-title! "New document")
      (render-editor feed
                     {:status "new"
                      :feed (:name feed)
                      :title ""
                      :slug ""
                      :draft false}))
    (document/get-doc slug
                      (fn [e]
                        (let [xhr (.target e)]
                          (if (= (. xhr (getStatus)) 200)
                            (let [json (js->clj (. xhr (getResponseJson)))]
                              (util/set-page-title!
                               (str "Edit \"" ("title" json) "\""))
                              (render-editor feed
                                             {:status "edit"
                                              :feed ("feed" json)
                                              :title ("title" json)
                                              :slug ("slug" json)
                                              :draft ("draft" json)}
                                             ("content" json)))
                            (render-document-not-found-template)))))))

(defn start-image-mode! [slug status feed]
  (if (= :new status)
    (do
      (util/set-page-title! "New document")
      (render-editor feed
                     {:status "new"
                      :feed (:name feed)
                      :title ""
                      :slug ""
                      :draft false}))
    (document/get-doc slug
                      (fn [e]
                        (let [xhr (.target e)]
                          (if (= (. xhr (getStatus)) 200)
                            (let [json (js->clj (. xhr (getResponseJson)))]
                              (util/set-page-title!
                               (str "Edit \"" ("title" json) "\""))
                              (render-editor feed
                                             {:status "edit"
                                              :feed ("feed" json)
                                              :title ("title" json)
                                              :slug ("slug" json)
                                              :draft ("draft" json)}
                                             ("content" json)))
                            (render-document-not-found-template)))))))

(defn start-mode-callback! [slug status e]
  (let [xhr (.target e)]
    (when (= (. xhr (getStatus)) 200)
      (let [json (js->clj (. xhr (getResponseJson)))
            feed {:name ("name" json)
                  :default-slug-format ("default-slug-format" json)
                  :default-document-type ("default-document-type" json)}]
        (cond
         (= (:default-document-type feed) "image")
           (start-image-mode! slug status feed)
         :default
           (start-default-mode! slug status feed))))))

(defn start [status uri]
  (let [feed-name (get-feed-from-uri)
        slug (when (= status :edit)
               (str "/" (last (re-find #"^/admin/[^/]+/edit/(.*?)$" uri))))]
    (document/get-feed feed-name (partial start-mode-callback! slug status))))