                (rest substitutions))
         slug))))
 
-(defn sync-slug-with-title []
+(defn sync-slug-with-title [feed]
   (when-not (.checked (dom/getElement "custom-slug"))
     (let [title (.value (dom/getElement "title"))
           slug-el (dom/getElement "slug")]
-      (ui/set-form-value slug-el (create-slug title))
+      (ui/set-form-value slug-el (create-slug feed title))
       (document/get-doc (.value slug-el) handle-duplicate-slug-callback))))
 
-(defn toggle-custom-slug []
+(defn toggle-custom-slug [feed]
   (let [slug-el (dom/getElement "slug")]
     (if (.checked (dom/getElement "custom-slug"))
       (ui/enable-element slug-el)

  (soy/renderElement (dom/getElement "main-page") tpl/document-not-found))

(defn render-editor
  ([tpl-map] (render-editor tpl-map nil))
  ([tpl-map content]
     (if (= "new" (:status tpl-map))
       (do
         (render-editor-template tpl-map)
         (events/listen (dom/getElement "title")
                        event-type/INPUT
                        sync-slug-with-title)
         (events/listen (dom/getElement "slug")
                        event-type/INPUT
                        validate-slug)
         (events/listen (dom/getElement "custom-slug")
                        event-type/CHANGE
                        toggle-custom-slug)
         (events/listen (dom/getElement "save-document")
                        "click"
                        save-new-document-click-callback))

         (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))))