context = window
$ = jQuery

panHelper = class PanHelper

  ###
  Convert the pan value that comes from a backend mixer
  to a 0-100 % usable by a draggable panner element
  ###
  convertPanToPercent: (mixerPan) ->
    value = (((mixerPan + 90) / 90) * 100) / 2

    if value < 0
      0
    else if value > 100
      100
    else
      value

  ###
  Convert the % value of a draggable panner element
  to a mixer-ready pan value
  ###
  convertPercentToPan: (percent) ->
    value = 2 * percent / 100 * 90 - 90

    if value < -90
      -90
    else if value > 90
      90
    else
      Math.round(value)

  convertPercentToPanForDisplay: (percent) ->
    Math.abs(context.JK.PanHelpers.convertPercentToPan(percent))

context.JK.PanHelpers = new panHelper()