 $(window).onbeforeunload = -> return "You haven't saved your changes." if window.$isDirty
 $(document).ready(initialLoadReady)
 $(document)
-  .on('turbolinks:load', turbolinksReady)
-  .on('turbolinks:before-visit', (event) ->
+  .on("turbolinks:load", turbolinksReady)
+  .on("turbolinks:before-visit", (event) ->
     event.preventDefault() if window.$isDirty and !confirm("You haven't saved your changes.")
   )
-  .on('click', '[data-object~="suppress-click"]', -> false)
-  .on('click', '[data-object~="remove-self"]', ->
-    $(this).remove()
+  .on("click", "[data-object~=suppress-click]", -> false)
+  .on("click", "[data-object~=remove-closest]", ->
+    $(this).closest($(this).data("target")).remove()
     false
   )
-  .on('click', '[data-object~="remove-closest"]', ->
-    $(this).closest($(this).data('target')).remove()
+  .on("click", "[data-object~=remove]", ->
+    $($(this).data("target")).remove()
     false
   )
-  .on('click', '[data-object~="remove"]', ->
-    $($(this).data('target')).remove()
+  .on("click", "[data-object~=modal-show]", ->
+    $($(this).data("target")).modal({ dynamic: true })
     false
   )
-  .on('click', '[data-object~="modal-show"]', ->
-    $($(this).data('target')).modal({ dynamic: true })
+  .on("click", "[data-object~=modal-hide]", ->
+    $($(this).data("target")).modal("hide")
     false
   )
-  .on('click', '[data-object~="modal-hide"]', ->
-    $($(this).data('target')).modal('hide')
-    false
-  )
-  .on('click', '[data-object~="submit"]', ->
-    $($(this).data('target')).submit()
+  .on("click", "[data-object~=submit]", ->
+    $($(this).data("target")).submit()
     false
   )
   .on("click", "[data-object~=submit-and-disable]", ->

    $($(this).data("target")).submit()
    false
  )
  .on('mouseenter', '[data-object~="hover-show"]', ->
    return false unless document.documentElement.ontouchstart == undefined
    $('[data-object~="hover-show"]').each( (index, element) ->
      $($(element).data('target')).hide()
    )
    $($(this).data('target')).show()
  )
  .on('mouseleave', '[data-object~="hover-show"]', ->
    $($(this).data('target')).hide()
  )
  .keydown( (e) ->
    if $("#interactive_design_modal").is(':visible')
      hideInteractiveDesignModal()     if e.which == 27
    if e.which == 77 and not $("input, textarea, select, a").is(":focus")
      if $('#preview-mode').length == 1 and $('#edit-mode').length == 1 and $('#edit-mode').parent().hasClass('active')
        $('#preview-mode').click()
      else
        $('#edit-mode').click()
      e.preventDefault()
      return
  )
  .on('click', '[data-object~="settings-save"]', ->
    window.$isDirty = false
    $($(this).data('target')).submit()
    false
  )
  .on('click', '[data-object~="toggle-visibility"]', ->
    $($(this).data('target')).toggle()
    false
  )
  .on('change', ':input', ->
    if $("#isdirty").val() == '1'
      window.$isDirty = true
  )
  .on('click', '[data-object~="toggle-delete-buttons"]', ->
    $($(this).data('target-show')).show()
    $($(this).data('target-hide')).hide()
    false
  )
  .on("wheel", "input[type=number]", ->

@fadeAndRemove = (element) ->
  $(element).fadeOut(500, -> $(element).remove())

@setFocusToField = (element_id) ->
  val = $(element_id).val()
  $(element_id).focus().val("").val(val)

@componentsReady = ->
  themesReady()
