#= require jquery-3.4.1.slim.js

$(document).data( "tempo", 80)
$(document).data( "sides", 3)

$(document).ready ->
  current_col = 0

  # append audio elements to buttons:
  $(".sequencer-button").data( "side", 1 )
  for row in [1..4]
    for side in [1..$(document).data("sides")]
      $("audio#row" + row ).clone().attr("id","").appendTo(".sequencer-button[row=" + row + "]")

  setInterval =>
    playCol(current_col)
    if (current_col <= 6)
      current_col++
    else
      current_col = 0
  , 15000 / $(document).data("tempo")

playCol = (col) ->
  $('.sequencer-button').removeClass('activated')
  for row in [1..4]
    current_button = $('.sequencer-button').eq(col + ( row - 1) * 8)
    current_button.addClass('activated')
    if ( current_button.prop('checked') )
      side = current_button.data("side")
      try
        current_button.children(":eq(" + (side - 1) + ")")[0].play()
      catch e
        console.log "Error playing sound."
      if (side >= $(document).data("sides"))
        current_button.data("side", 1)
        side = 1
      else
        current_button.data("side", side + 1)
