@App.modals.BaseController = Ember.Controller.extend
  alert_message: ""
  #modal_options: Ember.Object.create(closeOnOverlay: true, closeOnModalClick: false)
  title: (->
    # return title if directly set by options
    return @get('modal_options.title') if @get('modal_options.title')
    # return translated title_path if directly set by controller
    translation_params = {}
    if model = @get('model')
      translation_params = model.serialize() if model.serialize
    return tspan(@title_path, translation_params).htmlSafe() if @title_path
    # return translated title_path if directly set by options
    return tspan(@get('modal_options.title_path'), translation_params).htmlSafe() if @get('modal_options.title_path')
    # infer title path based on controller name App.modals.AddSectionController => add_section
    underscored = `this.constructor.toString().substr(11).replace(/Controller$/, '').underscore()`
    # find translated title or humanize the controller name
    if convention_translation = ttry("modal.#{underscored}.title", translation_params)
      tspan(@get("modal.#{underscored}.title"), translation_params).htmlSafe()
    else
      underscored.capitalize().replace(/_/, ' ')
  ).property('model.id')
  actions:
    close: ->
      if close = @get('modal_options.close')
        close.apply(@)
      @send 'closeModal' unless @preventClose
    closeOnOverlay: ->
      @send('close') if @get('modal_options.closeOnOverlay')
      false
    modalClick: ->
      @send('close') if @get('modal_options.closeOnModalClick')
      false
    ok: ->
      if ok = @get('modal_options.ok')
        ok.apply(@)
      @send 'closeModal' unless @preventClose
    confirm: -> @send('ok')
    save: ->
      @get('model').save()
      @send 'closeModal' unless @preventClose
    destroy: ->
      @modal 'confirm',
        title_path: @get('modal_options.destroy_text_path') || 'general.destroy.text'
        model: @get('model')
        ok: ->
          @get('model').destroyRecord()
          @send 'closeModal' unless @preventClose
