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

    $('#gate-filter').selectpicker()
    
    refreshPage = ->
      paramsLink = getFilterButtonParams()
      postUrl = settings.baseUrl+"/alert/index?"
      postUrl += paramsLink
      window.location = postUrl
      
    getFilterButtonParams = ->
      
      linkParams = ""
      bookmark = 0
      
      $(".filter-buttons").each ->
        button = $(this)
        lnPar = ""
        if(button.hasClass('active'))
          if(button.data('filter') == 'bookmark')
            bookmark = 1     
          else  
            lnPar = "types[]=" + button.data('filter')
            
            if(linkParams != '')
              linkParams += "&"
              
            linkParams = linkParams.concat(lnPar)  
      
     
      if(bookmark > 0)
        if(linkParams != '')
          linkParams += "&"
        
        linkParams += "bookmarkedOnly=1"
      
      gateFilter = ""
      $("select option:selected").each ->
        if(gateFilter != '')
          gateFilter += "&"
        gateFilter += "gates[]=" +$(this).val()
     
      if(gateFilter != '')
        linkParams += "&"
        linkParams += gateFilter
              
      return linkParams 
    
    
    $("#alert-filter-buttons").on 'click', '.filter-buttons', ->
      $(this).button "toggle"
      refreshPage()

    $('#gate-filter').change(->
      refreshPage()
     
    )