jQuery ->

# $(".tip").popover(placement: 'top auto')

$(document).on 'click', '.tip', (e) ->
  event.preventDefault()

$(document).on 'click', '.tab_box a', (e) ->
  e.preventDefault()
  $(this).tab('show')

$(document).on 'click', '.nested_form_remove_link', (e) ->
  e.preventDefault()
  $(this).closest(".nested_form").find(".nested_form_destroy").val("1")
  $(this).closest(".nested_form").collapse('hide')

$(document).on 'click', '.nested_form_add_link', (e) ->
  e.preventDefault()
  new_id = new Date().getTime()
  regexp = new RegExp("new_association", "g")
  html = $(this).data('content').replace(regexp, new_id)
  $(".nested_forms").append(html)

$(document).on 'click', '.nested_form_polymorphic_add_link', (e) ->
  e.preventDefault()
  object_class = $('#add_link_class').children('option:selected').val()
  if object_class != ''
    data = $(this).data('content')
    new_id = new Date().getTime()
    regexp = new RegExp("new_association", "g")
    html = data[object_class].replace(regexp, new_id)
    $(".nested_forms").append(html)

$(document).on 'click', '.polymorphic_add_link', (e) ->
  e.preventDefault()
  object_class = $('#add_link_class').children('option:selected').val()
  if object_class != ''
    location.href = "#{$(this).attr('href')}?filter%5Btype%5D=#{object_class}"

$('.jstree').each ->
  params =
    "core":
      "data": $(this).data('content')
      "themes":
        "dots": false
    "plugins": []
  if $(this).data('sortable')
    params["core"]["check_callback"] = true
    params["plugins"].push "dnd"
  tree = $(this).jstree params
  tree.on "select_node.jstree", (e, data) ->
    location.href = data.node.a_attr.href
  if $(this).data('sortable')
    tree.on "move_node.jstree", (e, data) ->
      $(tree).siblings('.alert').remove()
      $.ajax
        async: false,
        type: 'post',
        url: "#{data.node.a_attr.href}/move.json"
        data: 
          "parent": if data.parent != '#' then data.parent else ''
          "position": data.position
        error: (data) ->
          tree.jstree('refresh')
          $(tree).before("<div class=\"alert alert-danger\">#{data.responseText}</div>")

$(document).on 'click', '.collapse_link', (e) ->
  e.preventDefault()
  $($(this).data('target')).collapse('toggle')

$(document).on 'click', '.img-link', (e) ->
  e.preventDefault()
  links = $(this).closest('.image-gallery').find('.img-link')
  target = e.target || e.srcElement
  link = if target.src then target.parentNode else target
  options = { index: link, event: e }
  blueimp.Gallery(links, options)

$('.upload_image').fileupload
  add: (e, data) ->
    types = /(\.|\/)(gif|jpe?g|png|mov|mpeg|mpeg4|avi)$/i
    file = data.files[0]
    if types.test(file.type) || types.test(file.name)
      $(this).closest('.image_attribute_container').find('.progress').collapse('show')
      $("input[name='_method']").attr('name', '__method')
      data.submit()
      $("input[name='__method']").attr('name', '_method')
    else
      alert("#{file.name} is not a gif, jpg or png image file")
  progress: (e, data) ->
    progress = parseInt(data.loaded / data.total * 100, 10)
    $(this).closest('.image_attribute_container').find('.bar').css('width', progress + '%')
  done: (e, data) ->
    $(this).closest('.image_attribute_container').find('.upload_buffer_id').attr('value', data.result.id)
    $(this).closest('.image_attribute_container').find('.image').attr('src', data.result.thumb_url)

$('.nested_form_add_images').fileupload
  add: (e, data) ->
    types = /(\.|\/)(gif|jpe?g|png|mov|mpeg|mpeg4|avi)$/i
    file = data.files[0]
    if types.test(file.type) || types.test(file.name)
      tmpl_data = { index: new Date().getTime() }
      data.context = $(this).closest('.nested_form_container').find('.nested_form_script').tmpl(tmpl_data)
      $(this).closest('.nested_form_container').append(data.context)
      data.context.find('.progress').collapse('show')
      $("input[name='_method']").attr('name', '__method')
      data.submit()
      $("input[name='__method']").attr('name', '_method')
    else
      alert("#{file.name} is not a gif, jpg or png image file")
  progress: (e, data) ->
    if data.context
      progress = parseInt(data.loaded / data.total * 100, 10)
      data.context.find('.bar').css('width', progress + '%')
  done: (e, data) ->
    data.context.find('.upload_buffer_id').attr('value', data.result.id)
    data.context.find('.image').attr('src', data.result.thumb_url)

$('.nested_form').each ->
  nested_form = $(this)
  upload_buffer_id = $(this).find('.upload_buffer_id').attr('value')
  if upload_buffer_id
    $.getJSON "/upload_buffers/#{upload_buffer_id}.json", (data) ->
      nested_form.find('.image').attr('src', data.thumb_url)