(function($){"use strict";$(document).ready(function(){$('.header-search--ajaxed .header-search__form').each(function(){var $form=$(this),$input=$form.find('.header-search__input'),$filter=$form.find('.header-search__filter'),$langInput=$form.find('.header-search__lang-input'),searchQuery='',postType,postTypeArr=[],lang;$filter.find('input:checked').each(function(){postTypeArr.push($(this).val())});postType=postTypeArr.length<1?'any':postTypeArr.join();postTypeArr=[];lang=$langInput.length?$langInput.val():false;$input.on('change keyup paste',function(e){var newSearchQuery=$(this).val();if((newSearchQuery!==searchQuery)&&!(38===e.which||40===e.which||13===e.which)){searchQuery=newSearchQuery.trim();lsvrPressvilleAjaxSearchGetResults($form,postType,searchQuery,lang)}});$input.on('focus',function(e){var newSearchQuery=$(this).val();if((newSearchQuery===searchQuery)&&$form.find('.header-search__results').length>0){$form.find('.header-search__results').slideDown(300)}else{searchQuery=newSearchQuery.trim();lsvrPressvilleAjaxSearchGetResults($form,postType,searchQuery,lang)}});$filter.find('input[type="checkbox"]').on('change',function(){$filter.find('input:checked').each(function(){postTypeArr.push($(this).val())});postType=postTypeArr.length<1?'any':postTypeArr.join();postTypeArr=[];lsvrPressvilleAjaxSearchGetResults($form,postType,searchQuery,lang)});$input.on('keydown',function(e){var $searchResults=$form.find('.header-search__results'),$active=$searchResults.find('.header-search__results-item--active');if(40===e.which){if($active.length<1||$active.filter(':last-child').length){$active.removeClass('header-search__results-item--active');$searchResults.find('.header-search__results-item:first-child').addClass('header-search__results-item--active')}else{$active.removeClass('header-search__results-item--active');$active.next().addClass('header-search__results-item--active')}e.preventDefault();e.stopPropagation()}if(38===e.which){if($active.length<1||$active.filter(':first-child').length){$active.removeClass('header-search__results-item--active');$searchResults.find('.header-search__results-item:last-child').addClass('header-search__results-item--active')}else{$active.removeClass('header-search__results-item--active');$active.prev().addClass('header-search__results-item--active')}e.preventDefault();e.stopPropagation()}if(13===e.which){if($active.length){window.location.href=$active.find('a').attr('href');e.preventDefault();e.stopPropagation()}}})});var lsvrPressvilleAjaxRequest=null;function lsvrPressvilleAjaxSearchGetResults($form,postType,searchQuery,lang){var searchQuery=searchQuery.trim();var lang=lang!==false?'&lang='+lang:'';if(searchQuery.length>1){clearTimeout($form.data('ajax-search-timer'));$form.data('ajax-search-timer',setTimeout(function(){$form.addClass('header-search__form--loading');$form.find('.header-search__spinner').fadeIn(150);if(null!==lsvrPressvilleAjaxRequest){lsvrPressvilleAjaxRequest.abort()}lsvrPressvilleAjaxRequest=jQuery.ajax({type:'post',url:lsvr_pressville_ajax_search_var.url,data:'action=lsvr-pressville-ajax-search&nonce='+lsvr_pressville_ajax_search_var.nonce+'&post_type='+postType+'&search_query='+searchQuery+lang,success:function(response){if(''!==response){var responseJson=false;try{responseJson=JSON.parse(response)}catch(e){console.log('Ajax Search Response: INVALID JSON')}if(responseJson){lsvrPressvilleAjaxSearchShowResults($form,responseJson)}}else{console.log('Ajax Search Response: BLANK')}$form.removeClass('header-search__form--loading');$form.find('.header-search__spinner').fadeOut(150)},error:function(){$form.removeClass('header-search__form--loading');$form.find('.header-search__spinner').fadeOut(150);console.log('Ajax Search Response: ERROR')}})},500))}}function lsvrPressvilleAjaxSearchShowResults($form,json){if(json.hasOwnProperty('status')){var status=json.status,listTitle=json.hasOwnProperty('list_title')?json.list_title:'',$input=$form.find('.header-search__input'),output='';if('ok'===status&&json.hasOwnProperty('results')){$.each(json.results,function(){var rating='';if(this.hasOwnProperty('post_title')&&this.hasOwnProperty('permalink')&&this.hasOwnProperty('post_type')&&this.hasOwnProperty('icon_class')){output+='
  • ';output+='';output+='';output+=this.post_title;output+='
  • '}});output='';if(json.hasOwnProperty('more_label')&&json.hasOwnProperty('more_link')){output+='

    ';output+=''+json.more_label+'

    '}}else if('noresults'===status){var message=json.hasOwnProperty('message')?json.message:'';if(''!==message){output='

    '+message+'

    '}}if(''!==output){if($form.find('.header-search__results').length>0){$form.find('.header-search__results').first().html(output)}else{$form.append('
    '+output+'
    ')}if($form.find('.header-search__results').is(':hidden')){$form.find('.header-search__results').slideDown(300)}}}}})})(jQuery);