 
 # New Assignment Box #
 
-Template.new_assignment_box.sample = ->
-    rand = (min, max) -> Math.floor(Math.random() * (max - min + 1) + min)
-
-    types = [
-        -> "today"
-        -> "tomorrow"
-        -> "#{DateOMatic.getMonthName(rand(0, 11))} #{rand(1, 28)}"
-        -> "#{DateOMatic.getMonthName(rand(0, 11))} #{rand(1, 28)}, #{new Date().getFullYear()+1}"
-        -> "#{DateOMatic.getDowName(rand(0, 6))}"
-        -> "#{DateOMatic.getDowName(rand(0, 6)).slice(0, 3)}"
-        -> "#{rand(1, 10)} days from now"
-    ]
-    task = "read chapter #{rand(1, 15)} due #{_.sample(types)()}"
-    if Session.equals("class_id", "")
-        "#{_(Offline.smart.classes().find().fetch()).chain().pluck(_.shuffle(['name','abbr'])[0]).shuffle().value()[0]} #{task}"
-    else
-        "#{task}"
+Template.new_assignment_box.helpers
+    sample: ->
+        rand = (min, max) -> Math.floor(Math.random() * (max - min + 1) + min)
+
+        types = [
+            -> "today"
+            -> "tomorrow"
+            -> "#{DateOMatic.getMonthName(rand(0, 11))} #{rand(1, 28)}"
+            -> "#{DateOMatic.getMonthName(rand(0, 11))} #{rand(1, 28)}, #{new Date().getFullYear()+1}"
+            -> "#{DateOMatic.getDowName(rand(0, 6))}"
+            -> "#{DateOMatic.getDowName(rand(0, 6)).slice(0, 3)}"
+            -> "#{rand(1, 10)} days from now"
+        ]
+        task = "read chapter #{rand(1, 15)} due #{_.sample(types)()}"
+        if Session.equals("class_id", "")
+            "#{_(Offline.smart.classes().find().fetch()).chain().pluck(_.shuffle(['name','abbr'])[0]).shuffle().value()[0]} #{task}"
+        else
+            "#{task}"
 
 # Assignments #
 

    for dow in (DateOMatic.getDowName(n).toLowerCase() for n in [0..6])
        $(".#{dow} input[type='time']").prop("disabled", not $(".#{dow} input[type='checkbox']").prop("checked"))

Template.classes.classes = ->
    # Fake dependency on update_interval to update periodically.
    Meteor.extra_garbage_of_tomfoolery = Session.get "update_interval"

    classes = Offline.smart.classes().find({}).fetch()
    sortIterator = (oneClass) ->
        return oneClass.name unless oneClass.schedule?
        [sun, mon, tue, wed, thu, fri, sat] = oneClass.schedule
        dateStrings = _.compact [
            if sun.enabled
                "Sunday at #{sun.time}"
            if mon.enabled
                "Monday at #{mon.time}"
            if tue.enabled
                "Tuesday at #{tue.time}"
            if wed.enabled
                "Wednesday at #{wed.time}"
            if thu.enabled
                "Thursday at #{thu.time}"
            if fri.enabled
                "Friday at #{fri.time}"
            if sat.enabled
                "Saturday at #{sat.time}"
        ]
        dates = _.map dateStrings, DateOMatic.parseFuzzyFutureDateAndTime
        deltas = _.map dates, (date) ->
            delta = DateOMatic.msDifferential date
            if delta > 603900000
                delta = delta - 603900000
            delta
        smallestDelta = Math.min deltas...
        return smallestDelta
    sortedClasses = _.sortBy classes, sortIterator
    return sortedClasses

Template.classes.fake_all_class_list = -> [_id: ""]

Template.classes.edit_disabled = -> if online() then "" else "disabled"
Template.classes.show_edit = -> online()
Template.new_assignment_box.disabled = -> if online() then "" else "disabled"
Template.classes.class_color = -> @color

Template.classes.events
    "mousedown .class": (evt) -> Router.setList(this._id) if @_id?

                Offline.save()
        evt.target.value = ""

Template.classes.helpers
    active: ->
        if Session.equals("class_id", this._id)
            "active"
        else
            ""

# Class Editing #

Template.edit_class_modal.helpers
    name: -> Offline.smart.classes().findOne(Session.get("editing_class"))?.name
    abbr: -> Offline.smart.classes().findOne(Session.get("editing_class"))?.abbr
    class_color: -> Offline.smart.classes().findOne(Session.get("editing_class"))?.color
do ->
    ecmHelpers = {}
    for dow, n in (DateOMatic.getDowName(n).toLowerCase() for n in [0..6])
        do (dow, n) ->
            ecmHelpers["#{dow}Checked"] = ->
                if Offline.smart.classes().findOne(Session.get("editing_class"))?.schedule?[n]?.enabled then "checked" else ""
            ecmHelpers["#{dow}Time"] = ->
                Offline.smart.classes().findOne(Session.get("editing_class"))?.schedule?[n]?.time
    Template.edit_class_modal.helpers ecmHelpers

Template.edit_class_modal.events
    "click .delete": ->
