$( document ).ready(function() { clearInputValue(); var locationHash = window.location.hash; if (locationHash) { $('html, body').animate({ scrollTop: $(locationHash).offset().top - 100 }, 50); $(locationHash).click(); } }); function delay(callback, ms) { var timer = 0; return function () { var context = this, args = arguments; clearTimeout(timer); timer = setTimeout(function () { callback.apply(context, args); }, ms || 0); }; } function clearInputValue() { if($("#services-search-input").val()) { $("#services-search-input").keyup(); } } function filterAccordion() { $(".first-category").removeClass("first-category"); var inputValue = $("#services-search-input").val(); var urlsList = []; $(".accordion").addClass("filtered"); $(".category-section").each(function () { var convertedInputValue = convertToStringIdentifier(inputValue); var categoryCorrect = convertToStringIdentifier($(this).children("h3").text()).indexOf(convertedInputValue) !== -1; $(this).children(".accordion-element").each(function () { $(this).addClass("hide"); var serviceName = $(this).children(".accordion-link").text(); var serviceUrl = $(this).children(".accordion-link").attr('href'); var tags = $(this).children(".tag").text(); var convertedTags = convertToStringIdentifier(tags); var convertedServiceName = convertToStringIdentifier(serviceName); if ((convertedTags.indexOf(convertedInputValue) !== -1 || convertedServiceName.indexOf(convertedInputValue) !== -1 /*|| categoryCorrect*/) && urlsList.indexOf(serviceUrl) === -1) { urlsList.push(serviceUrl); $(this).removeClass("hide"); var $element = $(this).children(".accordion-link"); $element.html($element.html().replace(/<\/?b>/g, '')); var convertedText = convertToStringIdentifier($element.text()); var firstCut = convertedText.indexOf(convertedInputValue); if (firstCut > -1) { var secondCut = firstCut + inputValue.length; var name = $element.text(); var firstPart = name.substring(0, firstCut); var secondPart = name.substring(firstCut, secondCut); var thirdPart = name.substring(secondCut); $element.html(firstPart + secondPart.bold() + thirdPart); } } }); }); $(".accordion > li").each(function () { if ($(this).children("div").children("div").children("div.accordion-element:not(.hide)").length === 0) { $(this).addClass("hide"); } else { $header = $(this).children("h2"); $(this).removeClass("hide"); $header.children("a").addClass("active"); if (!$header.next().is(":visible")) { $header.next().slideToggle(100); $header.children("a").attr('aria-label', 'Naciśnij enter aby zwinąć poniżej usługi należące do kategorii ' + $header.children("a").text()); } } }); $(".category-section").each(function () { if ($(this).children("div.accordion-element:not(.hide)").length === 0) { $(this).addClass("hide"); } else { $(this).removeClass("hide"); } }); $(".accordion > li > div").each(function () { $(this).children('div:not(.hide):first').addClass("first-category"); }); } $(".accordion > li > h2 > a").click(function (event) { var $service = $(this); $service.toggleClass("active"); $service.parent().next().slideToggle(100); var toggleText = $service.hasClass("active") ? 'zwinąć' : 'rozwinąć'; $service.attr('aria-label', 'Naciśnij enter aby ' + toggleText + ' poniżej usługi należące do kategorii ' + $service.text()); }); $("#services-remove-search-icon").click(function () { clearInputValue(); }); $("#services-remove-search-icon").keypress(function (e) { if(e.which == 13) { clearInputValue(); } }); function addCorrectRow(correctElement, priority, list, ele, counts) { correctElement.priority = priority; list.push(correctElement); counts.allCount++; if (!ele) { counts.electronicCount++; } } $("#services-search-input").keyup(delay(function (event) { var code = (event.keyCode ? event.keyCode : event.which); if (code == 9 || code == 16) { return; } if ($(window).width() < 600 && event.keyCode == 13) { event.preventDefault(); event.target.blur() } $(".first-category").removeClass("first-category"); var inputValue = this.value; var list = []; var urlsList = []; var counts = {allCount: 0, electronicCount: 0}; if (inputValue.length >= 3) { $(".category-section").each(function () { $category = $(this); var categoryCorrect = (convertToStringIdentifier($category.children("h3").text()).indexOf(convertToStringIdentifier(inputValue)) !== -1); $category.children(".accordion-element").each(function () { $service = $(this); var ele = ($service.children(".electronic").text() === "true") ? " " : ""; var serviceName = $service.children(".accordion-link").text(); var serviceUrl = $service.children(".accordion-link").attr('href'); var correctElement = { name: serviceName + ele, href: $service.children(".accordion-link").attr("href") }; if (urlsList.indexOf(serviceUrl) === -1) { if (convertToStringIdentifier(serviceName).indexOf(convertToStringIdentifier(inputValue)) !== -1) { addCorrectRow(correctElement, 3, list, ele, counts); urlsList.push(serviceUrl); } else if (convertToStringIdentifier($(this).children(".tag").text()).indexOf(convertToStringIdentifier(inputValue)) !== -1) { addCorrectRow(correctElement, 1, list, ele, counts); urlsList.push(serviceUrl); } } }); }); list = list.sort(function(a,b) { return b.priority - a.priority}); $(".list-container > ul > li").each(function (index) { if (list.length > index) { $(this).removeClass("hide"); var $element = $(this).children("a"); var firstCut = convertToStringIdentifier(list[index].name).indexOf(convertToStringIdentifier(inputValue)); $element.attr("href", list[index].href); if (firstCut > -1) { var secondCut = firstCut + inputValue.length; var name = String(list[index].name); var firstPart = name.substring(0, firstCut); var secondPart = name.substring(firstCut, secondCut); var thirdPart = name.substring(secondCut); $element.html(firstPart + secondPart.bold() + thirdPart); } else { $element.html(list[index].name); } } else { $(this).addClass("hide"); } }); filterAccordion(); } else { $(".accordion-link").each(function () { $(this).html($(this).html().replace(/<\/?b>/g, '')); $(this).parent().removeClass("hide"); }); $(".accordion > li").each(function () { $(this).removeClass("hide"); var $header = $(this).children("h2"); $header.children("a").removeClass("active"); if ($header.next().is(":visible")) { $header.next().slideToggle(100); $header.children("a").attr('aria-label', 'Naciśnij enter aby rozwinąć poniżej usługi należące do kategorii ' + $header.children("a").text()); } }); $(".category-section").removeClass("hide"); $(".accordion").removeClass("filtered"); } }, 200));