$ ->
$("[data-study-id-picker]").studyIDPicker()
$.fn.studyIDPicker = ->
$(this).each ->
new StudyIDPicker $(this), JSON.parse($(this).attr("data-study-id-picker"))
class StudyIDPicker
constructor: (@$el, @studyIDMap) ->
@$studies = @$el.find("select").eq(0)
@$sites = @$el.find("select").eq(1)
@$ids = @$el.find("select").eq(2)
@$studies.change => @change()
@$sites.change => @change()
change: ->
key = "" + @$studies.val() + @$sites.val()
ids = @idsForKey(key)
@$ids.html @optionsFromIds(ids)
idsForKey: (key) ->
@studyIDMap[key] || []
optionsFromIds: (ids) ->
options = ids.map (id) -> ""
"" + options.join("\n")