   (GET "/sp/latest/:page" {params :params cookies :cookies}
        (let [page (Integer/parseInt ^String (webutil/get-page params))
              user (auth/user-from-cookie cookies)]
-         (render-with (partial latest page)
+         (render-with (partial latest (partial url "/latest/") page)
                       {:exs (ex/get-latest-ex-list-default-order 
                              user
                              {} 
                              page)
                        :user user})))
   
+  (GET "/sp/list/:page" {params :params cookies :cookies}
+       (let [page (Integer/parseInt ^String (webutil/get-page params))
+             user (auth/user-from-cookie cookies)]
+         (render-with (partial latest (partial url (dissoc params :page) "/list/") page)
+                      {:exs  (ex/get-latest-ex-list user 
+                                                    (mongerfilter/make-query-from params) 
+                                                    (webutil/get-page params) 
+                                           (mongerfilter/sortby params))
+                       :user user})))
+
   (GET "/sp/ex/:id" {params :params cookies :cookies}
        (let [user (auth/user-from-cookie cookies)]
          (render-with single-exercise  

     [:div.shortEx (-> ex :body utils/strip-html truncate)]]]
   ])

(defn as-link [name page]
  [:a.naviLink {:href (url "/latest/" page)} (str name)])

(defn latest-paging [page current-count]
  (let [pages 1
        next-pages (if (> current-count 0) (range (inc page) (+ page (inc pages))) [])
        start-page (- page pages)
        prev-pages (if (> start-page 0) (range start-page page) (range 1 page))
        navigation  (vec (conj (concat (map (partial as-link "edellinen") prev-pages) 
                                       [[:span.currentPage (str " " page " ")]] 
                                       (map (partial as-link "seuraava") next-pages)) :div.pageNavi))]
    navigation))

(defn header [user]

   [ontrail.webutil :as webutil]
   [ontrail.auth :as auth]
   [ontrail.formats :as formats]
   [ontrail.mongerfilter :as mongerfilter]

   [clj-time.core :as time]
   [hiccup.form :as form]
   [hiccup.core :as hiccup]
   [stencil.core :as stencil]
   [clojure.string :as string])
  (:import [java.net URLEncoder]))

(def #^{:private true} logger (org.slf4j.LoggerFactory/getLogger (str *ns*)))

(defn urlp [params]
  (if (empty? params)
    ""
    (str "?"
         (apply str 
                (interpose "&"
                 (map (fn [param] (str (-> param first name) "=" (-> param second URLEncoder/encode)))
                   params))))))

;; (url {:a "b" :c "d"} "/list/" page) = /list/page?a=b&c=d
(defn url[maybe-params & parts] 
  (if (map? maybe-params)
    (let [params (urlp maybe-params)]
      (apply (partial str "/sp") (concat parts [params])))
    (apply (partial str "/sp" maybe-params) parts)))

(defmacro render-with [render-fn data & [status]]
  `(try 
