   
 setupScheduledEmails = ->
   testForLockManager()
-  mailTaskMap = 
-    "test_mail_task": employerNewCandidatesAvailableTask
+  mailTaskMap = #TODO: Edit this to include additional emails
+    "test_mail_task": candidateUpdateProfileTask
     
-  MailTask.find({}).lean().exec (err, mailTasks) ->
+  MailTask.find({}).lean().exec (err, mailTasks) -> #TODO: Ask Nick whether or not to remove this
     if err? then throw "Failed to schedule mailTasks! #{err}"
     for mailTask in mailTasks
-      setInterval mailTaskMap[mailTask.url], mailTask.frequency*2
+      setInterval mailTaskMap[mailTask.url], mailTask.frequency*2 #TODO: Have some random offset to prevent lock contention
       
 testForLockManager = -> unless lockManager then throw "The system isn't configured to do distributed locking!"
   
 ### Candidate Update Reminder Task ###
-emailTimeRange = (timeRange, finalCallback) ->
+
+candidateUpdateProfileTask = ->
+  mailTaskName = "candidateUpdateProfileTask"
+  lockDurationMs = 20000 #TODO: Change these to something appropriate for the mail frequency (ideally longer than the task but shorter than frequency)
+  currentDate = new Date()
+  timeRanges = []
+  for weekPair in [[4, 2,'two weeks'], [8, 4, 'four weeks'], [8, 52, 'eight weeks']]
+    timeRanges.push
+      start: generateWeekOffset currentDate, weekPair[0]
+      end: generateWeekOffset currentDate, weekPair[1]
+      name: weekPair[2]
+  lockManager.setLock mailTaskName, lockDurationMs, (err) ->
+    if err? then return log.error "Error getting a distributed lock for task #{mailTaskName}!"
+    async.each timeRanges, emailTimeRange.bind({mailTaskName: mailTaskName}), (err) ->
+      if err
+        log.error "There was an error sending the candidate profile update reminder emails: #{err}"
+      else
+        log.info "Completed mail task #{mailTaskName}"
+      lockManager.releaseLock mailTaskName, (err) ->
+        if err? then return log.error "There was an error releasing the distributed lock for task #{mailTaskName}: #{err}"
+
+generateWeekOffset = (originalDate, numberOfWeeks) ->
+  return (new Date(originalDate.getTime() - numberOfWeeks * 7 * 24 * 60 * 60 * 1000)).toISOString()
+
+emailTimeRange = (timeRange, emailTimeRangeCallback) ->
   waterfallContext =
     "timeRange": timeRange
     "mailTaskName": @mailTaskName
   async.waterfall [
     findAllCandidatesWithinTimeRange.bind(waterfallContext)
-    (unfilteredCandidates, cb) -> #now filter the candidates to see if they are eligible
-      async.reject unfilteredCandidates, candidateFilter.bind(waterfallContext), (filtered) -> cb null, filtered
-    (filteredCandidates, cb) -> #Now send emails to the eligible candidates and record.
+    (unfilteredCandidates, cb) -> 
+      async.reject unfilteredCandidates, candidateFilter.bind(waterfallContext), cb.bind(null, null)
+    (filteredCandidates, cb) ->
       async.each filteredCandidates, sendReminderEmailToCandidate.bind(waterfallContext), cb
-  ], finalCallback
+  ], emailTimeRangeCallback
   
 findAllCandidatesWithinTimeRange = (cb) ->
   findParameters =

Level = require '../levels/Level'
log = require 'winston'
sendwithus = require '../sendwithus'
if config.isProduction or config.redis.host isnt "localhost"
  lockManager = require '../commons/LockManager'
  

module.exports.setup = (app) ->
  app.all config.mail.mailchimpWebhook, handleMailchimpWebHook

    sendwithus.api.send context, (err, result) ->
      log.error "Error sending interal candidate update email: #{err} with result #{result}" if err
      cb null

### End Internal Candidate Update Reminder Email ###
### Employer New Candidates Available Email ###
employerNewCandidatesAvailableTask = ->
  mailTaskName = "employerNewCandidatesAvailableTask"
  lockDurationMs = 6000 #TODO: Update this lock duration
  lockManager.setLock mailTaskName, lockDurationMs, (err) ->
    if err? then return log.error "There was an error getting a task lock!"
    emailEmployerNewCandidatesAvailable.apply {"mailTaskName":mailTaskName}, (err) ->
      if err
        log.error "There was an error completing the new candidates available task: #{err}"
      else
        log.info "Completed the employer new candidates available task!"
      lockManager.releaseLock mailTaskName, (err) ->
        if err? then return log.error "There was an error releasing the distributed lock for task #{mailTaskName}: #{err}"

emailEmployerNewCandidatesAvailable = (emailEmployerNewCandidatesAvailableCallback) ->
  currentTime = new Date()
  asyncContext = 
    "currentTime": currentTime
