 ;; State
 
 (def contestants (atom [
-                        {:id 1 :name "Carolyn" :out 18 :tribe "masaya" :locked true}
+                        {:id 1 :name "Carolyn" :out 0 :tribe "masaya"}
                         {:id 2 :name "Dan" :out 0 :tribe "escameca"}
                         {:id 3 :name "Hali" :out 0 :tribe "nagarote"}
                         {:id 4 :name "Jenn" :out 0 :tribe "nagarote"}

          (.sortable (js/$ "#placed") (clj->js {
                                                :items "li:not(.ui-state-disabled)"
                                                :connectWith ".connectedSortable"
                                                :update (fn [e ui] (re-sort (-> e .-target .-childNodes)))
                                                }))
          (.disableSelection (js/$ "#placed")))))


        (dec max-points)
        (rest entry))))))

(defn calculate-points-for-entries [entries-to-modify voted-out-list]
  (map #(assoc
         %
         :points
         (calculate-points-for-entry (create-contestant-list entries-to-modify (:id %) voted-out-list)))
       entries-to-modify))

(defn sorted-entries [entries]
   (into [] (sort #(compare (:points %2) (:points %1)) entries)))

(defn leader-board [entries contestants]
  [:div.column
   [:h3 "Leaderboard"]
   [:ul
    (for [entry (sorted-entries entries)]
      ^{:key (:id entry)} [:li.ui-state-default 
                           [:div {:class "points"} (:name entry)
                            [:div (:points entry)]]])]])

(defn create-placed-list [placed]
  (into [] (sort #(compare (:out %2) (:out %1)) placed)))

(defn entries-display [entries selected-entry]
  [:div.column
   [:h3 "Entries"]
   [:select {:on-change #(swap! selected-entry (fn [] (int (-> % .-target .-value))))}
    (for [entry entries]
      ^{:key (:id entry)} [:option {:value (:id entry)}
                           (:name entry)])]
   [lister (create-contestant-list entries @selected-entry @contestants) "selected-entry"]])

(defn home []
  (let [entries (calculate-points-for-entries @entries @voted-out-list)]
    [:div [:h2 "Phil & Will's Survivor 30 Bootlist"]
     [leader-board entries @contestants]
     [entries-display entries selected-entry]
     [:div.column
      [:h3 "Voted out"]
      [lister (create-placed-list  (filter #(true? (:locked %)) @contestants)) "placed"]]
     [:div.column
      [:h3 "Active"]
      [lister (filter #(= 0 (:out %)) @contestants) "contestants"]]]))


(defn update-voted-out [new-placed-list]
  (let [new-sorted-list (map-indexed (fn [index item] (hash-map :id (int (.-id item)) :out (- 18 index))) new-placed-list)]
    (swap! voted-out-list (fn [] new-sorted-list))))

(defn home-did-mount []
  (js/$ (fn []
