$(document).ready ->

  #show and hide
  updateShow = (currentID) ->
    if currentID == 1
      $("tr").slice(0, currentID).show().addClass('active').attr('id', currentID)
    else
      $("tr").slice((currentID - 1), currentID).show().addClass('active').attr('id', currentID)

  updateHide = ->
    $("tr").hide().removeClass('active')

  updateCount = (currentID) ->  
    $("span.count").text(currentID)

  #set Checkbox based on selected value
  setCheckBox = (curr) ->
    val = $("tr##{curr}").find("input:checked").attr("value")
    $('.checkBox').removeClass('active')
    $(".checkBox[value='#{val}']").addClass('active') if val

  #check to see if input is checked in current row and set class
  checkSet = (curr) ->
    if ($("tr##{curr} input:checked").length != 0)
      $("tr##{curr}").addClass('set')
    $('.navButtons #right').attr('disabled', false).removeClass('disabled')

  #navigate right
  $('.navButtons #right').click ->
    if $(this).hasClass('disabled')
      return false
    updateHide()
    curr++
    updateShow(curr)
    checkSet(curr)
    setCheckBox(curr)
    if curr == 2
      $('.navButtons #left').show()
    if curr == 10
      $('.navButtons #right').hide()
      $('input[type="submit"]').removeClass("hidden")
      val = $("tr##{curr}").find("input:checked").attr("value")
      if val
        $('input[type="submit"]').attr("value", "Next").prop("disabled", false)
    updateCount(curr)

  #navigate left
  $('.navButtons #left').click ->
    updateHide()
    curr--
    updateShow(curr)
    checkSet(curr)
    setCheckBox(curr)
    if curr == 1
      $('.navButtons #left').hide()
    if curr == 9
      $('.navButtons #right').show()
    updateCount(curr)


  #Set current ID
  curr = 1
  

  #Hide table questions and update/show first row
  $('tr').hide()
  updateShow(curr)
  setCheckBox(curr)
  checkSet(curr)