/* eslint-disable */

// Behavior for the observations filter widget ... thing.
$(document).ready(function() {
  $('.iconic_taxon_filter input').change(function() {
    $(this).siblings('label').toggleClass('selected');
  });
  $('.iconic_taxon_filter input:checked').each(function() {
    $(this).siblings('label').addClass('selected');
  });
});

function selectAllIconicTaxa() {
  $('.iconic_taxon_filter input').each(function() {
    this.checked = true;
    $(this).siblings('label').addClass('selected');
  });
}

function deSelectAllIconicTaxa() {
  $('.iconic_taxon_filter input').each(function() {
    this.checked = false;
    $(this).siblings('label').removeClass('selected');
  });
}

function toggleFilters(link, options) {
  var options = $.extend({}, options);
  if ($('#filters:visible').length == 0) {
    showFilters(link, options)
  } else {
    hideFilters(link, options)
  }
}

function showFilters(link, options) {
  var options = $.extend({}, options)
  $('#filters').show()
  if (!options.skipClass) {
    $(link).addClass('open')
  }
  $('#filters input[name=filters_open]').val(true)
  if ($('#filters .simpleTaxonSelector').length == 0) {
    $('#filters input[name=taxon_name]').taxonAutocomplete()
  }
  if ( $( "#place_filter .ui-widget" ).length === 0 ) {
    var chosenPlaceJson = $( "#filters input[name=place_id]" ).attr( "data-json" );
    $( "#filters input[name=place_id]" ).chooser( {
      collectionUrl: "/places/autocomplete.json",
      resourceUrl: "/places/{{id}}.json?partial=autocomplete_item",
      chosen: chosenPlaceJson ? JSON.parse( chosenPlaceJson ) : null
    } );
  }
  if ( $( "#not_in_place_filter .ui-widget" ).length === 0 ) {
    var chosenNotInPlaceJson = $( "#filters input[name=not_in_place]" ).attr( "data-json" );
    $( "#filters input[name=not_in_place]" ).chooser( {
      collectionUrl: "/places/autocomplete.json",
      resourceUrl: "/places/{{id}}.json?partial=autocomplete_item",
      chosen: chosenNotInPlaceJson ? JSON.parse( chosenNotInPlaceJson ) : null
    } );
  }
}

function hideFilters(link, options) {
  var options = $.extend({}, options)
  $('#filters').hide()
  if (!options.skipClass) {
    $(link).removeClass('open')
  }
  $('#filters input[name=filters_open]').val(false)
}

function deselectAll() {
  $('#filters :text, #fitlers :input[type=hidden], #fitlers select').val(null)
  $('#filters :input:checkbox').prop('checked', false)
  deSelectAllIconicTaxa()
  $('#filters input[name=place_id]').chooser('clear', {bubble:false})
  $.fn.simpleTaxonSelector.unSelectTaxon('#filters .simpleTaxonSelector')
}

function setFiltersFromQuery(query) {
  deselectAll()
  var params = $.deparam(query);
  params.has = params.has || [ ];
  if( params.photos === "true" ) { params.has.push("photos"); }
  if( params.sounds === "true" ) { params.has.push("sounds"); }
  $.each(params, function(k,v) {
    if (k != 'iconic_taxa' && k != 'has') {
      $('#filters :input:radio[name="'+k+'"][value="'+v+'"]').prop('checked', true)
    }
    if ($.isArray(v)) {
      v2 = v.join(',')
      $('#filters :input[name="'+k+'[]"]').not(':checkbox, :radio').val(v2)
    } else {
      $('#filters :input[name="'+k+'"]').not(':checkbox, :radio').val(v)
    }
    if (k == 'place_id') {
      $('#filters input[name=place_id]').chooser('selectId', v)
    } else if (k == 'taxon_id') {
      $.fn.simpleTaxonSelector.selectTaxonFromId('#filters .simpleTaxonSelector', v)
    } else if (k == 'iconic_taxa' || k == 'has' || k == 'month') {
      $.each(v, function(i,av) {
        $selection = $('#filters :input:checkbox[name="'+k+'[]"][value='+av+']')
        $selection.prop('checked', true)
        if (k == 'iconic_taxa') {
          $selection.siblings('label').addClass('selected');
        }
      })
    } 
    else if (k == 'projects') {
      $('#filters :input[name="projects[]"]').val(v)
    }
  })
}

@@ -3234,22 +3234,96 @@
 if (
+ 
 k ==
- '
+= %22place_id%22 ) %7B%0A      if ( v === %22any%22 ) %7B%0A        $('#filters input%5Bname=
 place_id
 ') %7B
@@ -3318,21 +3318,57 @@
 place_id
+%5D
 ')
+.chooser( %22clear%22 );%0A      %7D else
  %7B%0A
+  
       $(
@@ -3412,17 +3412,18 @@
 ser(
-'
+ %22
 selectId
 ', v
@@ -3418,21 +3418,262 @@
 selectId
-', v)
+%22, v );%0A      %7D%0A    %7D else if ( k === %22not_in_place%22 ) %7B%0A      if ( v === %22any%22 ) %7B%0A        $('#filters input%5Bname=not_in_place%5D').chooser( %22clear%22 );%0A      %7D else %7B%0A        $('#filters input%5Bname=not_in_place%5D').chooser( %22selectId%22, v );%0A      %7D
 %0A    %7D e

Clear place chooser on obs/export when place_id=any (closes #3414)