   img.onload = -> callback? img
   img.src = src
 
-mod = (a, b) ->
-  ((a % b) + b) % b
-
 class SubjectViewer extends Controller
   className: 'subject-viewer'
   template: require './templates/subject-viewer'
 
   currentFrame: 0
+  advanceTimeout: NaN
 
   step: ''
   toolOptions: null

      callback? image

  addToggle: (index) ->
    @togglesList.append "<li><button name='toggle-frame', value='#{index}'>#{index}</button></li>"

  goTo: (@currentFrame) ->
    @currentFrame = mod @currentFrame, @frames.length

    @frames[@currentFrame].toFront()


  playFrames: ->
    @playButton.prop 'disabled', true
    @pauseButton.prop 'disabled', false
    @advanceFrames()

  advanceFrames: ->
    @goTo @currentFrame + 1
    @advanceTimeout = setTimeout @advanceFrames.bind(@), 250

  pauseFrames: ->
    clearTimeout @advanceTimeout
    @playButton.prop 'disabled', false
    @pauseButton.prop 'disabled', true

  setStep: (step) ->
    @step = step
