function getUrlVars($cst_u)	{
    var vars = [], hash;
    var hashes = $cst_u.slice($cst_u.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
var mobileCustomSearch = function() {
    var a = [];//ajax-loader.gif
   jQuery(".custom-search-input").length && jQuery(".custom-search-input").each(function() {
        function b(a) {
            var b = "";
            i.html(""), i.removeClass("suggestions-empty"), i.addClass("suggestions-added"); if(jQuery.isEmptyObject(a.data)) { (i.addClass("suggestions-empty"), b += '<div class="search-result">' + CustomSearchVars.no_search_results + "</div>", i.append(b)), total.html(''), p.html('') } else { jQuery.each(a.data, function(a, c) {
							/*b = "", b += '<div class="search-content">',b += '<a class="search-result-card" href="' + c.post_url + '" title="' + c.title + '">', b += '<h4 class="search-post-title">' + c.title + "</h4>", c.excerpt && (b += '<div class="search-excerpt">' + c.excerpt + "</div>"), b += "</a>", b += "</div>",*/ i.append(c)
            });p.html(a.pagination);total.html(a.total + ' Results');}
        }

        function c() {
					var c, searchRequest = null;
					if (jQuery(window).width() < 786) {
            if (f = e.closest(".live-search"), c = e.val(), CustomSearchVars.min_char_count <= c.length) {
                if (void 0 !== a[c]) return void b(a[c]);
                r.find(".slider-loading").html('<div class="loading-wrap"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div>'), f.find(".search-submit").css("color", "transparent"), h.css("color", "transparent");
								if (searchRequest != null)	searchRequest.abort();
									searchRequest = jQuery.ajax({
                    url: CustomSearchVars.ajaxurl,
                    type: "post",
                    data: {
                        action: "search_all_posts",
                        search: e.val(),
												paged: 1,
                        perPage: CustomSearchVars.per_page,
                        show_feat_img: CustomSearchVars.show_feat_img,
                        display_post_type: CustomSearchVars.display_post_type
                    },
                    success: function(d) {
                        a[c] = d, b(d), initSearhPagination(), r.find(".slider-loading").html(''), h.css("color", h.attr("data-color"))
                    }
                })
            } else r.find(".slider-loading").html(''), h.css("color", h.attr("data-color")), i.removeClass("suggestions-added")
					}
        }
				function rp(ob){
					var rp, prRequest=null;
					r.find(".slider-loading").html('<div class="loading-wrap"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div>');
					//console.log(ob);
					var all_per = getUrlVars(ob.attr('href'));
					if (prRequest != null)	prRequest.abort();
					 prRequest = jQuery.ajax({
						 url: CustomSearchVars.ajaxurl,
						 type: "post",
						 data: {
								 action: "search_all_posts",
								 search: e.val(),
								 perPage: CustomSearchVars.per_page,
								 paged: all_per['paged'],
								 display_post_type: CustomSearchVars.display_post_type
						 },
						 success: function(d) {
								 b(d),initSearhPagination(), r.find(".slider-loading").html(''), h.css("color", h.attr("data-color"))
						 }
					})					
				}
				function initSearhPagination()	{
					p.find('li a').on('click', function(ev) {
						ev.preventDefault();
						setTimeout(rp(jQuery(this)), 100);return false; 
					});
				}
        var d, e = jQuery(this),
            f = e.closest(".live-search"),
						r = jQuery(".search-results-wrapper .container"),
            g = f.find(".search-button"),
            h = f.find(".search-submit"),
						p = jQuery(".pagination-wrap"),
						total = jQuery(".total-results"),
            i = jQuery(".search-results-wrap");
        h.attr("data-color", h.css("color")), e.on("focusin", function() {
            4 < jQuery(this).val().length && i.children(".search-result").length && i.addClass("suggestions-added")
        }), e.on("focusout", function() {
            i.is(":hover") || g.is(":hover") || i.removeClass("suggestions-added")
        }), jQuery(g, i).on("mouseleave", function() {
            e.is(":focus") || i.removeClass("suggestions-added")
        }), e.on("keyup", function(event) {
            clearTimeout(d), d = setTimeout(c, 500);
        }), e.on("keydown", function() {
            clearTimeout(d)
        })
    })
};
function x13(x) { if(window.innerWidth < 786 && x.keyCode==13) return false; }
jQuery(document).ready(function() {
	jQuery(window).on('load resize', function() {
    mobileCustomSearch();	
	});
		jQuery('.header-search .header-icon-search').on('click', function(e) {
			e.preventDefault();
			if (jQuery(window).width() > 786 && jQuery('.header-search').hasClass('active')) {
				jQuery('form#searchform').submit();return false;
			}
		});
});