 
 $.fn.bind_switches = ->
   $(".switch").each ->
-    $(this).bind "click", ->
-      el = $("#" + $(this).data("switchable"))
-      if $(this).hasClass("open")
-        $(this).removeClass "open"
-        $(this).addClass "closed"
+    $(@).bind "click", ->
+      el = $("#" + $(@).data("switchable"))
+      if $(@).hasClass("open")
+        $(@).removeClass "open"
+        $(@).addClass "closed"
         el.hide()
       else
-        $(this).removeClass "closed"
-        $(this).addClass "open"
+        $(@).removeClass "closed"
+        $(@).addClass "open"
         el.show()
 
 bind_clearable_fields = ->
   $("[data-clearable-field]").each ->
-    input = $(this)
-    form = (if input.length > 0 then $(input[0].form) else $())
-    link = $("<a href=\"#\" class=\"active_field_clear\"><i class='fa fa-times-circle'></i></a>")
-    $(this).after link
-    input.on
+    $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: ->
-        form.submit()
-      changeDate: ->
-        form.submit()
-
-    link.on
+        console.log "change", $input
+        $form.submit()
+      # changeDate: ->
+      #   console.log "changeDate", $input
+      #   $form.submit()
+
+    $link.on
       click: ->
-        input.val ""
-        form.submit()
+        $input.val ""
+        $form.submit()
 
 bind_filter_html_elements = ->
   $("[data-filter-html]").on "keyUp", ->
-    q = $(this).val()
-    src_tag = $(this).data("src-tag")
-    dest_tag = $(this).data("dest-tag")
-    src_class = $(this).data("src-class")
+    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 = $(this).html()
-      id = "#" + $(this).attr("id").replace(src_tag + "_", dest_tag + "_")
+      str = $(@).html()
+      id = "#" + $(@).attr("id").replace(src_tag + "_", dest_tag + "_")
       unless q is ""
         if str.match(reg)
           $(id).show()

  $(".form-vertical").removeClass("form-horizontal")

  $("[data-nicedit]").each ->
    new nicEditor().panelInstance($(this).attr("id"))

  $("[data-checkboxes-clone]").each ->
    $(this).bind_checkboxes_clone()
  $("[data-collapsible-label]").each ->
    $(this).bind_collapsible_label()

  $("[data-boolean-value]").each ->
    $(this).bind_boolean_value()

  $("[data-active-select]").each ->
    $url = $(this).data("active-select")
    $(this).on
      change: ->
        $to_url = $url.replace /__template__/, $(this).val()
        window.location = $to_url

  $("[data-token-input]").each ->
    $(this).tokenInput $(this).data("token-input"),
      theme: "facebook"
      prePopulate: $(this).data("token-pre")
      preventDuplicates: true
      crossDomain: false
      hintText: $(this).data("token-hint-text")
      noResultsText: $(this).data("token-no-results-text")
      searchingText: $(this).data("token-searching-text")

  $("#flash_panel").delay(3000).fadeOut 3000



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

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

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

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

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

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

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

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