 
   $switch.on
     click: ->
-      $(this).toggleClass("open")
-      if $(this).hasClass("open")
+      $(@).toggleClass("open")
+      if $(@).hasClass("open")
         $main_el.show()
         $opt_el.hide()
       else


fixHelper = (e, ui) ->
  ui.children().each ->
    $(this).width $(this).width()
    $(this).children().each ->
      $(this).width $(this).width()
  $(ui).closest("table").find("th").each ->
    $(this).width $(this).width()
  ui

$.fn.bind_sortable = ->
  $(this).sortable
    helper: fixHelper
    update: (event, ui) ->
      $.ajax
        type: "POST"
        data: $(this).sortable("serialize", { key: $(this).data("sortable-key")})
        url: $(this).data("sortable-url")
      $(this).parent().effect "highlight", {}, 2000

$.fn.convert_remote_links = ->
  $("[remote-links]").find("a").each ->
    $(this).attr("data-remote", "true") unless $(this).attr("href") is "#"

$.fn.bind_modal_data_url = ->
  $("[data-url]").each ->
    $(this).load_modal_data_url()

$.fn.load_modal_data_url = ->
  $(this).bind "click", ->
    el_id = "#" + $(this).data("controls-modal") + " .in_body"
    $(el_id).html ""
    $.ajax
      url: $(this).data("url")
      complete: (jqXHR) ->
        $(el_id).spin false
        $(el_id).html jqXHR.responseText

$.fn.concat_checkbox_text = ->
  $str = ""
  $(this).find("input[type='checkbox']:checked").each ->
    $str +=  "<span class='btn btn-mini'>" + $(this).parent().text() + "</span> "
  $str

$.fn.bind_collapsible_label = ->
  $switch = $($(this).data("collapsible-label"))
  $main_el = $($(this).data("collapsible-main"))
  $opt_el = $($(this).data("collapsible-opt"))

  $switch.addClass("switch")
  $main_el.hide()


$.fn.bind_switches = ->
  $(".switch").each ->
    $(@).bind "click", ->
      el = $("#" + $(@).data("switchable"))
      if $(@).hasClass("open")
        $(@).removeClass "open"
        $(@).addClass "closed"
        el.hide()
      else
        $(@).removeClass "closed"
        $(@).addClass "open"
        el.show()

bind_clearable_fields = ->
  $("[data-clearable-field]").each ->
    $input = $(@)
    # $form = (if $input.length > 0 then $($input[0].form) else $())
    $form = $input.parent("form")
    $link = $("<a href=\"#\" class=\"active_field_clear\"><i class='fa fa-times-circle'></i></a>")
    $(@).after $link

    $input.on
      change: ->
        console.log "change", $input
        $form.submit()
      # changeDate: ->
      #   console.log "changeDate", $input
      #   $form.submit()

    $link.on
      click: ->
        $input.val ""
        $form.submit()

bind_filter_html_elements = ->
  $("[data-filter-html]").on "keyUp", ->
    q = $(@).val()
    src_tag = $(@).data("src-tag")
    dest_tag = $(@).data("dest-tag")
    src_class = $(@).data("src-class")

    reg = new RegExp(".*" + q, "i")
    els = $("#{src_tag}.#{src_class}")
    els.each ->
      str = $(@).html()
      id = "#" + $(@).attr("id").replace(src_tag + "_", dest_tag + "_")
      unless q is ""
        if str.match(reg)
          $(id).show()
