   comparator: (first, second) ->
     firstYear = first.get('year')
     secondYear = second.get('year')
-    
+
     if firstYear == secondYear
       if first.get('cweek') < second.get('cweek') then -1 else 1
     else

      totals = aggregate.get "totals"
      biggestTotal = Math.max(biggestTotal, if (totals.actual > 0) then totals.actual else totals.estimated)
    , 0
      
  aggregateWeek: (week) ->
    aggregate = @findOrCreateRelevantAggregate week.pick(['cweek', 'year'])
    aggregate.update week.pick(['proposed', 'estimated_hours', 'actual_hours'])

    @
    
  findOrCreateRelevantAggregate: (week) ->
    aggregate = @detect (a) -> _.all ['cweek', 'year'],
      (attr) -> a.get(attr) is week[attr]

class window.StaffPlan.Collections.WeeklyAggregates extends Backbone.Collection

  model: window.StaffPlan.Models.WeeklyAggregate

  initialize: (models, options) ->
    @parent = options.parent

  populate: () ->
    begin = new XDate().addWeeks(-150)
    end = begin.clone().addWeeks(300)
    range = _.range(begin.getTime(), end.getTime(), 7 * 86400 * 1000)

    baseAggregate = new StaffPlan.Models.WeeklyAggregate
      cweek: 0
      year: 0
      timestamp: 0
      totals:
        estimated: 0
        actual: 0
        proposedEstimated: 0
        proposedActual: 0
    date = new XDate()
    dummies = range.map (timestamp) ->
      dateClone = date.clone().setTime(timestamp)
      baseAggregate.clone().set
        cweek: dateClone.getWeek()
        year: dateClone.getFullYear()
        timestamp: timestamp
    @add dummies

    @parent.getAssignments().each (assignment) =>
      assignment.work_weeks.each (week) =>
