 
 # 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 #
 

                $("#new-assignment").parent().addClass("has-error")
                return

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

    # Determine which assignments to display in main pane,
    # selected based on class_id and tag_filter.
    class_id = Session.get "class_id"

    sel = class_id: class_id
    if class_id is ""
        sel = {}

    return _(Offline.smart.assignments().find(sel).fetch()).chain()
           .sortBy((obj) -> new Date(obj.timestamp).getTime())
           .sortBy((obj) -> new Date(obj.due).getTime())
           .sortBy("done")
           .groupBy("done")
           .pairs()
           .map(([truthiness, list]) -> _.sortBy(list, (obj) ->
                if truthiness is "true"
                    Math.abs(DateOMatic.msDifferential(new Date(obj.due)))
                else
                    new Date(obj.due).getTime()
            ))
           .reduce(((memo, list) -> memo.concat(list)), [])
           .value()

Template.assignment_item.precise_due_date = -> DateOMatic.stringify(@due, yes, no, yes)

Template.assignment_item.editable_due_date = -> DateOMatic.stringify(@due, no, yes, yes)

div = (a, b) -> (a - a % b) / b

Template.assignment_item.fuzzy_due_date = -> if DateOMatic.isFuture(@due) then "in #{DateOMatic.fuzzyDifferential(@due)}" else "#{DateOMatic.fuzzyDifferential(@due)} ago"

Template.assignment_item.done_class = -> if this.done then "muted" else ""

Template.assignment_item.done_checkbox = -> if this.done then "check-" else ""

Template.assignment_item.editing = -> Session.equals("editing_itemname", this._id)

Template.assignment_item.class_color = ->
    Offline.smart.classes().findOne(@class_id)?.color

Template.assignment_item.color_class = ->
    if @done
        return "list-group-item-success"
    msLeft = new Date(@due).getTime() - (new Date()).getTime()
    if msLeft < 0
        return "list-group-item-danger"
    if div(msLeft, 1000 * 60 * 60) < 24
        return "list-group-item-warning"
    if div(msLeft, 1000 * 60 * 60 * 24) < 2
        return "list-group-item-info"
    return ""

Template.assignment_item.events
    "click .check": ->

    input.focus()
    input.select()

Template.contents.helpers
    showEverything: ->
        return true if Meteor.userId()?
        return true if !online
        return false

# Classes #

