# -----------------------------------------------------------------------------
# Author: Alexander Kravets <alex@slatestudio.com>,
#         Slate Studio (http://www.slatestudio.com)
# -----------------------------------------------------------------------------
# REDACTOR CUSTOM VERSION
#= require ./redactor_images
# -----------------------------------------------------------------------------

# change default fast speed from 200 to 10 as it's used by redactor modals
# while closing
console.log ':: [redactor-character] change $.fx.speeds.fast from 200 to 10 ::'
$.fx.speeds.fast = 10

@redactorCharacter =
  # PRIVATE ===================================================================

  # TODO: fixed toolbar disabled on mobile
  _redactor_options: ->
    @_trigger_change = true

    config = @_get_default_config()
    @config.redactorOptions ?= {}

    $.extend(config, @config.redactorOptions)

    if (! chr.isMobile()) && config.plugins.indexOf('fixedtoolbar') == -1
      config.plugins.push('fixedtoolbar')

    if Loft? && config.plugins.indexOf('loft') == -1
      config.plugins.push('loft')

    if chr.isMobile()
      config.toolbarFixed = false
      # config.toolbarFixedTopOffset = 40

    return config

  _get_default_config: () ->
    focus:             false
    imageFloatMargin:  '20px'
    buttonSource:      true
    pastePlainText:    true
    replaceDivs:       false
    scrollTarget:      chr.module.view.$content
    plugins:           [ 'table' ]
    buttons:           [ 'html'
                         'formatting'
                         'bold'
                         'italic'
                         'deleted'
                         'unorderedlist'
                         'orderedlist'
                         'table'
                         'link' ]

    # to have caching working we need to trigger 'change' event for textarea
    # when content got changed in redactor, but skip this when updating value
    # via `updateValue` method
    changeCallback: =>
      if @_trigger_change
        @$input.trigger('change')
      @_trigger_change = true

    initCallback: ->
      new RedactorImages(this)