$(document).ready(function () {
  'use strict';

  // to guess language, get first element in path.
  var language = location.pathname.substr(1).split('/')[0];
  var languages = ['it', 'en'];
  // if page does not have a language, take italian as default.
  if (languages.indexOf(language) == -1) {
    language = 'it';
  }
  var getParams = parseSearchParameters(paramKeys);
  var pageId = $('#searchPageId').attr('data');

  // listener for modal window search.
  $('#search-form').on('click', function(){
    var $inputText = $('#searchModal .autocomplete input');
    
    $inputText.on('input', executeAutoCompleteESQuery);
    $('#searchModal form a.btn-filter').on('click', function(event){
      event.preventDefault();
      $inputText.val(''); $('#suggestions').text('');
      if($(event.target).hasClass('active')) {
        return;
      }

      $('#searchModal form a.btn-filter.active').removeClass('active')
      $(event.target).addClass('active');
    });

  });

  // When close modal window search, clean input text.
  $('#searchModal .modal-header button').on('click', function(event){
    $('#searchModal .modal-body form .autocomplete input').val('');
    $('#suggestions').text('').addClass('d-none');
  });

  // listener on search form submit.
  $('#searchModal form').on('submit', function(event){
    // prevent form submit.
    event.preventDefault();

    // redirect to search page.
    var path = '/' + language + '/search';
    var queryString = "keyword=" + $('#searchModal .autocomplete input').val().trim().split(' ').join('+');
    window.location = path + '?' + queryString;
  });

  if(typeof pageId != 'undefined') {
    // ############# SEARCH ####################
    var params = getParams();
    var queryType = pageId;
    if (pageId == 'search') {
      queryType = (params['type'].length == 0) ? 'all' : params['type'].slice(0).pop();
    }

    var queryManager = new esDevelopersItaliaManager(queryType, {}, pagesQueryconfig, params);
  }

  /**
   * Parse location search to find parameters in keys array and returns a function closure.
   * 
   * @param array keys
   */
  function parseSearchParameters(keys) {
    var parameters = {}, params;
    params = location.search.substr(1).split('&').sort();

    $.each(keys, function(i,k){
      var values = [];
      for (var i = 0; i < params.length; i++) {
        if (k == params[i].substr(0, k.length)) {
          values.push(params[i].split('=').pop());
        }
      }
      parameters[k] = values;
    });

    /**
     * Get value for k location search parameter.
     * 
     * @param k 
     */
    function getParams(k) {

      // returns all parameters.
      if (typeof k == 'undefined') {
        return JSON.parse(JSON.stringify(parameters));
      }

      return typeof parameters[k] != 'undefined' ? parameters[k].slice(0) : [];
    }

    return getParams;
  }

  function executeAutoCompleteESQuery(event) {
    var queryType = $('#searchModal form a.btn-filter.active').attr('data');
    var esAutocompleteQuery;
    if (event.target.value.length > 0) {
      $('#suggestions').removeClass('d-none');
    }
    else {
      $('#suggestions').addClass('d-none');
    }

    switch(queryType) {
      case 'all':
        autocompleteQueryconfig['all']['language'] = language;
        esAutocompleteQuery = new esDevelopersItaliaAutocompleteAllQuery(JSON.parse(JSON.stringify(autocompleteQueryconfig['all'])));
        break;
      case 'platforms':
        autocompleteQueryconfig['platforms']['language'] = language;
        esAutocompleteQuery = new esDevelopersItaliaAutocompletePlatformsQuery(JSON.parse(JSON.stringify(autocompleteQueryconfig['platforms'])));
        break;
      case 'software_open':
        autocompleteQueryconfig['software_open']['language'] = language;
        esAutocompleteQuery = new esDevelopersItaliaAutocompleteSoftwareOpenSourceQuery(JSON.parse(JSON.stringify(autocompleteQueryconfig['software_open'])));
        break;
      case 'reuse_software':
        autocompleteQueryconfig['reuse_software']['language'] = language;
        esAutocompleteQuery = new esDevelopersItaliaAutocompleteSoftwareRiusoQuery(JSON.parse(JSON.stringify(autocompleteQueryconfig['reuse_software'])));
        break;
      case 'api':
        autocompleteQueryconfig['api']['language'] = language;
        esAutocompleteQuery = new esDevelopersItaliaAutocompleteAPIQuery(JSON.parse(JSON.stringify(autocompleteQueryconfig['api'])));
        break;
      case 'administrations':
        autocompleteQueryconfig['administrations']['language'] = language;
        esAutocompleteQuery = new esDevelopersItaliaAutocompletePAQuery(JSON.parse(JSON.stringify(autocompleteQueryconfig['administrations'])));
        break;
    }

    esAutocompleteQuery.executeESQuery();
  }

});

// encode(decode) html text into html entity
var decodeHtmlEntity = function(str) {
  return str.replace(/&#(\d+);/g, function(match, dec) {
    return String.fromCharCode(dec);
  });
};

@@ -565,13 +565,9 @@
 ');%0A
-    
 %0A
+
     
@@ -913,16 +913,17 @@
 active')
+;
 %0A      $
@@ -1448,16 +1448,103 @@
 earch';%0A
+    var queryType = %22type=%22 + $('#searchModal form a.btn-filter.active').attr('data');%0A
     var 
@@ -1689,16 +1689,34 @@
 ryString
+ + '&' + queryType
 ;%0A  %7D);%0A
@@ -2183,25 +2183,24 @@
 losure.%0A   *
- 
 %0A   * @param
@@ -2675,17 +2675,16 @@
 .%0A     *
- 
 %0A     * 
@@ -2691,17 +2691,16 @@
 @param k
- 
 %0A     */

Fix #402: Search parameters not correctly passed