#= require_self
#= require ./helpers
#= require_tree ./active_support
class Dunlop
  constructor: ->
    @setup_callbacks = {}
    @action_in_progress = 0

  setup: (target, callback)->
    @action_in_progress = 1
    target ||= $(document)
    for setup_label, callbacks_of_label of @setup_callbacks
      callbacks_of_label.forEach (callback)=>
        callback.call(@, target)



    # Set all optial boolean labels to the maximum label. NOTE: this is a hack
    delay 300, ->
      labels = $('.optional-boolean-label, .optional-input-activator-label')
      label_minimum_length = if labels.filter( -> $(@).parents('.optional-attribute-container').data('one') ).length then 356 else 320
      if labels.length
        labels.each -> label_minimum_length = $(@).width() if $(@).width() > label_minimum_length
        #max_optional_label_width = Math.max.apply(null, labels.map((i, el) -> $(el).width()).toArray())
        #max_optional_label_width = Math.max(max_optional_label_width, 356) # form display width match for big screens = 366 - 10
        labels.css 'width', "#{label_minimum_length + 10}px" if label_minimum_length

    if callback
      callback.call(@)
      @action_in_progress = 0
    else
      @action_in_progress = 0

  selected_ids: -> MultiSelect.selected_ids()

  register_setup: (label_or_callback, labelled_callback)->
    unless labelled_callback?
      labelled_callback = label_or_callback
      label_or_callback = 'anonymous'
    (@setup_callbacks[label_or_callback] ||= []).push labelled_callback


@Dunlop = new Dunlop()
