 (ns ^{:doc "Code for Timetable Builder page"}
   cljs-nusmods.timetable
-  (:use [jayq.core :only [$ attr children hide is prevent show text width]])
+  (:use [jayq.core :only [$ attr children hide insert-after is parent prevent
+                          remove-attr show text width]])
   (:require [clojure.set]
             [clojure.string]
             [cljs-nusmods.select2 :as select2]

    ; Update url hash
    (set-document-location-hash-from-module-info-seq moduleInfoFinal)))

(defn remove-module
  "Removes a module from the timetable.

   This deletes every lesson of the module from the timetable."
  [moduleCode]
  (if (contains? ModulesSelected moduleCode)
    ;!!!
    nil
  ))

(defn remove-all-modules
  "Removes all modules from the timetable"

                                      (range startTime endTime))
            }))))

(defn- timetable-mark-lesson-slots-free
  "Marks the timeslots for a lesson as free. The `lessonInfo` parameter must be
   a map with the following keys:

     :day       -> 0-indexed day
     :row       -> 0-indexed row in the day
     :startTime -> 0-indexed start time of the lesson
     :endTime   -> 0-indexed end time of the lesson"
  [lessonInfo]
  (let [day         (:day lessonInfo)
        row         (:rowNum lessonInfo)
        startTime   (:startTime lessonInfo)
        endTime     (:endTime lessonInfo)
        ttRow       (get-in Timetable [day row])
        occupiedVec (reduce (fn [occupiedVec timeIdx]
                              (assoc occupiedVec timeIdx nil))
                            (:occupied ttRow)
                            (range startTime endTime))]
    (set! Timetable
          (update-in
            Timetable [day row]
            (fn [ttRow]
              {:nrLessonGroups (dec (:nrLessonGroups ttRow))
               :occupied       occupiedVec})))))

(defn- find-free-row-for-lesson
  "Returns the 0-indexed row which can accomodate the given lesson (timeslots
   for the lesson are not occupied). If no existing row can accomodate the
