 class WidgetSwitcher
   constructor: (@elements) ->
     @$elements = $(elements)
-    @incrementTime = 1000 # refresh every 1000 milliseconds
 
   start: (interval=5000) ->
     self = @

    # Stop and reset timer
    @$timer.stop().once()

  updateTimer: () ->
    # Output timer position
    timeString = formatTime(currentTime)
    @$countdown.html(timeString)

    # If timer is complete, trigger alert

    @$timer = $.timer(@updateTimer, @incrementTime, true)

  addElements: () ->
    @$nextDashboardNameContainer = $("<span id='dc-switcher-next-name'></span>")
    @$countdown = $("<span id='dc-switcher-countdown'></span>")
    manualSwitcher = $("<span id='dc-switcher-manual-switcher'>&ensp;&#65515;</span>")
    @$elements.append(@$nextDashboardNameContainer).append(@$countdown).append(manualSwitcher).click( () =>
      location.href = "/#{@nextDashboardName}"
    )

  formatTime: (time) ->
    time = time / 10;
    min = parseInt(time / 6000, 10)
    sec = parseInt(time / 100, 10) - (min * 60)
    "#{(if min > 0 then @pad(min, 2) else "00")}:#{@pad(sec, 2)}"

  pad: (number, length) =>
    str = "#{number}"
    while str.length < length
      str = "0#{str}"
