$ ->
  if ($('#alerts-search-page').length > 0)

    $('#alert-description-filter').selectpicker()
    $('#gate-filter').selectpicker()

    getFilterButtonParams = ->
      
      vals = []
      i=0
      bookmark = 0
      
      $("#alert-filter-buttons .filter-buttons").each ->
        button = $(this)
        lnPar = ""
        if(button.hasClass('active'))
            vals[i++] = button.data('filter')
  
      return vals 

    refreshHiddenFieldForFilterButtons = ->
      values = getFilterButtonParams()
      
      $("#alarm-filter-hidden-field-container").empty()
      
      jQuery.each values, (i, val) ->
        if val == "bookmark"
          $("#alarm-filter-hidden-field-container").append "<input type=\"hidden\" name=\"AlertSearchForm[bookmark]\" value=\"1\" />"
        else  
          $("#alarm-filter-hidden-field-container").append "<input type=\"hidden\" name=\"AlertSearchForm[types][]\" value=\"" + val + "\" />"


    
    updateHiddenFieldForStatusFilterButtons = ->
    
      if $("#alert-takenincharge-filter-button").hasClass('active')
         $("#input-field-status-filter-takenincharge").attr("value", 1)
      else   
         $("#input-field-status-filter-takenincharge").attr("value", 0)

      if $("#alert-closed-filter-button").hasClass('active')
         $("#input-field-status-filter-closed").attr("value", 1)
      else   
         $("#input-field-status-filter-closed").attr("value", 0)

         
         
         
    $("#alert-filter-buttons").on 'click', '.filter-buttons', ->
      $(this).button "toggle"
      
      refreshHiddenFieldForFilterButtons()

    $("#alert-filter-status-buttons").on 'click', '.filter-buttons', ->
      $(this).button "toggle"
      
      inputId = "input-field-status-filter-" + $(this).data('filter')
      
      if $(this).hasClass('active')
         $("#" + inputId).attr("value", 1)
      else   
         $("#" + inputId).attr("value", 0)


    initDateTimePickers = ->
      $('#datetimepicker_start_date_time').datetimepicker
        language: 'it'
      $('#datetimepicker_end_date_time').datetimepicker
        language: 'it'
        



    refreshHiddenFieldForFilterButtons()
    updateHiddenFieldForStatusFilterButtons()
    initDateTimePickers()

    


