function setupInfinityScroll(options) { // Uudet valinnaiset asetukset: // - lastIdSelector: selektori, josta luetaan viimeisen elementin id (jos eri kuin itemSelector) // - appendAfterSelector: jos annettu, lisätään response tämän selektorin viimeisen elementin jälkeen (.last().after(...)) let isLoading = false; let allResultsLoaded = false; const triggerElement = $(options.triggerSelector); if (!triggerElement.length) { return; } function loadMore(params = {}) { if (isLoading || (allResultsLoaded && !params.limit)) { return; } isLoading = true; const originalHtml = triggerElement.html(); if (!params.limit) { triggerElement.html('Ladataan...'); } // Mistä haetaan viimeisen kohteen id? const idSourceSelector = options.lastIdSelector || options.itemSelector; const lastItem = $(idSourceSelector).last(); const lastId = params.limit ? '' : (lastItem.length ? lastItem.attr('id') : ''); const stype = triggerElement.attr('stype'); const squery = triggerElement.attr('squery'); const url = triggerElement.attr('url'); let ajaxUrl = `/search-more.php?stype=${stype}&squery=${squery}&id=${lastId}`; if (url) ajaxUrl += `&url=${url}`; if (params.limit) ajaxUrl += `&limit=${params.limit}`; $.ajax({ url: ajaxUrl, type: 'GET', success: function(response) { if (response && $.trim(response) !== "") { if (params.limit) { // Tilapalautus: korvataan nykyinen sisältö if (options.appendAfterSelector) { // Tyhjennetään container, jos sellainen on annettu if (options.containerSelector) { $(options.containerSelector).empty(); } else { // Jos containeria ei ole, poistetaan listaelementit turvallisesti $(options.itemSelector).remove(); } } else if (options.containerSelector) { $(options.containerSelector).empty(); } } // Mihin lisätään uudet rivit? if (options.appendAfterSelector) { $(options.appendAfterSelector).last().after(response); } else if (options.containerSelector) { $(options.containerSelector).append(response); } triggerElement.html(originalHtml); } else { allResultsLoaded = true; triggerElement.html('Ei enempää tuloksia').show(); } }, error: function() { triggerElement.html('Latausvirhe.').show(); }, complete: function() { isLoading = false; if (typeof params.onComplete === 'function') { params.onComplete(); } } }); } $(document).on('click', options.triggerSelector, function(e) { e.preventDefault(); loadMore(); }); $(window).on('scroll', function() { if (window.isRestoring || isLoading || allResultsLoaded) return; if (triggerElement.is(':visible') && (triggerElement.offset().top < $(window).scrollTop() + $(window).height())) { loadMore(); } }); (function restoreState() { const lastUrl = sessionStorage.getItem('lastSearchUrl'); const itemsLoaded = sessionStorage.getItem('itemsLoaded'); const scrollPosition = sessionStorage.getItem('scrollPosition'); const normalizeUrl = (url) => { if (!url) return ''; try { return (new URL(url).host + new URL(url).pathname).replace(/\/$/, ''); } catch (e) { return url; } }; if (!lastUrl || normalizeUrl(lastUrl) !== normalizeUrl(window.location.href) || !itemsLoaded) { return; } const requiredItems = parseInt(itemsLoaded, 10); if (requiredItems <= $(options.itemSelector).length) { window.scrollTo(0, parseFloat(scrollPosition)); return; } window.isRestoring = true; $('#page-loader-overlay').fadeIn(150); triggerElement.hide(); loadMore({ limit: requiredItems, onComplete: function() { $('#page-loader-overlay').fadeOut(150, function() { window.scrollTo(0, parseFloat(scrollPosition)); window.isRestoring = false; if (!allResultsLoaded) { triggerElement.show(); } }); } }); })(); } $(document).ready( function() { preload_image_object = new Image(); image_url = new Array(); image_url[0] = "/images/waiting.gif"; image_url[1] = "/images/flag-FI.png"; image_url[2] = "/images/flag-SE.png"; var i = 0; for(i=0; i<=2; i++) { preload_image_object.src = image_url[i]; } if (window.location.pathname != '/register_info.php' && window.location.pathname.match(/^\/admin/) === null && window.location.pathname.match(/^\/tilha/) === null && window.location.pathname.match(/^\/manager/) === null && window.location.pathname.match(/^\/lainaa/) === null){ //if (!getPrivacy()){ //privacyPopup(); //} } $('.img_replace_source').live('click', function(){ let target_src = $(this).attr("data-img-replace-target-src"); let target_id = $(this).attr("data-img-replace-target-id"); replace_image_src_to_id(target_id,target_src); }); function replace_image_src_to_id(id,src) { element = document.getElementById(id); if(element) { element.src=src; } } $('.a_replace_href').live('click', function(){ let target_href = $(this).attr("data-a-replace-target-href"); let target_id = $(this).attr("data-a-replace-target-id"); replace_link_href_to_id(target_id,target_href); }); function replace_link_href_to_id(id,href) { element = document.getElementById(id); if(element) { element.href=href; } } if ( window.location.hash.substring(0,13) == "#!product_id=" ) { var ean = window.location.hash.substring(13); $('#book_div').animate({ opacity: 0.0 }, 50, function () { var position = $(window).scrollTop()-$("#inner").offset().top; if ( position < 0 ) { position = 0; } if (position > 0 && $('#search_bar').hasClass('sticky')){ $("#book_div_top").attr('class', 'sticky_compensate'); }else{ $("#book_div_top").attr('class', null); } $("#book_div_top").css("height", position); $("#book_div").load("/book_load.php?ean="+encodeURIComponent(ean), {}, function(data) { $("#book_div").html(data); $('#book_div').animate({ opacity: 1.0 }, 75); refresh_description_expansions(); }); }); } var selected = 'search-id-0'; $("#shoppingcart-id").load("/shoppingcart.php", {}, function(data) { $("#shoppingcart-id").html(data); }); $('div.search-result').live('click', function () { $('#'+selected).css("background-color", "#ffffff"); $(this).css("background-color", "#e6e6e6" ); selected = $(this).attr('id'); var ean = $(this).attr('ean'); window.location.hash = '!product_id='+encodeURIComponent(ean); $('#book_div').animate({ opacity: 0.0 }, 75, function () { var position = $(window).scrollTop()-$("#inner").offset().top; if ( position < 0 ) { position = 0; } if (position > 0 && $('#search_bar').hasClass('sticky')){ $("#book_div_top").attr('class', 'sticky_compensate'); }else{ $("#book_div_top").attr('class', null); } $("#book_div_top").css("height", position); $("#book_div").load("/book_load.php?ean="+encodeURIComponent(ean), {}, function(data) { $("#book_div").html(data); $('#book_div').animate({ opacity: 1.0 }, 250); refresh_description_expansions(); cookieConsent.useAllowedCookies('trackProduct') }); }); }); $("div.search-result").live('mouseover', function () { if ( selected != $(this).attr('id') ) { $(this).css("background-color", "#f8f8f8"); } }); $("div.search-result").live('mouseout', function () { if ( selected != $(this).attr('id') ) { $(this).css("background-color", "#ffffff"); } }); $('#print_book').live('click', function(){ console.log("print"); PrintElem('#book_div'); return false; }); $('#item-info-header').live('click', function () { $('.item-info-header-on').each(function(index) { $(this).removeClass("item-info-header-on").addClass("item-info-header-off"); }); $(this).removeClass("item-info-header-off").addClass("item-info-header-on"); var url = $(this).attr('url'); $('#item-info').html('


'); $("#item-info").load(url, {}, function(data) { $("#item-info").html(data); }); }); $("#search-more").live('click', function (event) { event.preventDefault(); loadSearchMore(); }); if ( $( "#search-more" ).length ) { window.onscroll = function (ev) { if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2) { // you're at the bottom of the page, load more content here. loadSearchMore(); } }; } function loadSearchMore() { var more_element_html = $("#search-more").html(); $("#search-more").html('


'); let last = Array.from( document.querySelectorAll('.search-result') ).pop(); let last_id = last.getAttribute("id"); var stype = $("#search-more").attr('stype'); var squery = $("#search-more").attr('squery'); var query = $("#search-more").attr('query'); let searchmore = document.getElementById('search-more'); searchmore.id = "search-more-wait"; $.get("/search-more.php?stype=" + stype + "&squery=" + squery + "&id=" + last_id, {}).done(function(data) { if ( data == '' ) { $("#search-more-wait").html('
Ei enempää hakutuloksia

'); } else { $(".search-result-tr:last").after(data); $("#search-more-wait").html('
Hae lisää...

'); let searchmore = document.getElementById('search-more-wait'); searchmore.id = "search-more"; } }); } $(".shoppingcart-add, .shoppingcart-add * ").live('click', function (event) { event.stopPropagation(); var double_click_prevention_timeout = 500; if ($(event.target).hasClass('shoppingcart-add-exclude')){ return false; } var button_root = $(this); while (!$(button_root).hasClass("shoppingcart-add")){ button_root = $(button_root).parent(); if ($(button_root).hasClass('shoppingcart-add-exclude')){ return true; } } if (button_root.attr("clicked") == "true"){ return false; }else{ button_root.attr("clicked", "true"); setTimeout(function(){ button_root.attr("clicked", null); }, double_click_prevention_timeout); } var popup = $(button_root).find('#shoppingcart-popup'); var quantity = $(button_root).find('input').val(); if ( !quantity ) { quantity = 1; } if ( popup ) { popup.find('.quantity').html(quantity); popup.animate({opacity: "show", top: "-76"}, "slow").delay(15000); popup.animate({opacity: "hide", top: "-90"}, "slow"); } var querystr = $(button_root).attr('querystr')+'&quantity='+quantity; $.get("/shoppingcart-render.php?"+querystr, {}, function(sdata) { $('#shoppingcart-icon').animate({ opacity: 0.0 }, 300, function() { $("#shoppingcart-icon").html(sdata); }); $('#shoppingcart-icon').animate({ opacity: 1.0 }, 300, function() {setTimeout(function(){ cookieConsent.useAllowedCookies('trackCart')}, 0)}); }); $('.product_card_delivery_truck').toggleClass('truck_delivered', true); }); $('#login-form').submit(function() { var login = $("#login-form-login").val(); var passwd = $("#login-form-passwd").val(); $.post("/login.php", {method: "js", login: login, passwd: passwd}, function(data) { if ( data == 'true' ) { $('#login-top').animate({ opacity: 0.0 }, 300, function() { $.get("/login-form.php?login_status=success", {}, function(sdata) { $("#login-top").html(sdata); $('#login-top').animate({ opacity: 1.0 }, 300); }); }); } else { window.location.href = '/login.php?posted=true&login='+encodeURIComponent(login); } }); return false; }); $('.help-trigger').hover(function(e) { var popid = $(this).attr("popid"); $('div.pop-up-help[popid='+popid+']').css('top', e.pageY+20).css('left', e.pageX-140); $('div.pop-up-help[popid='+popid+']').show().animate({ opacity: 1.0 }, 300); }, function() { var popid = $(this).attr("popid"); $('div.pop-up-help[popid='+popid+']').animate({ opacity: 0.0 }, 150, function () { $(this).hide() }); }); $('.bdialog').click(function(e){ var dialog_id = $(this).attr('dialog_id'); $("#"+dialog_id ).dialog({width:400}); return false; }); jQuery(".fancybox").live('click', function(){ var width = 750; var height = '90%'; if ( $(this).attr('fbsize') == 'small' ) { width = 400; height = 450; } jQuery.fancybox({ 'width' : width, 'height' : height, 'autoScale' : true, 'overlayColor': '#FFFFFF', 'transitionIn' : 'elastic', 'transitionOut' : 'elastic', 'speedIn': 300, 'speedOut': 200, 'type' : 'iframe', 'href' : $(this).attr('href') }); return false; }); $(function() { $('input[btype="calendar"]').datepicker({ dateFormat: "dd.mm.yy", dayNamesMin: ["Su", "Ma", "Ti", "Ke", "To", "Pe", "La"], monthNames: ["Tammi","Helmi","Maalis","Huhti","Touko","Kesä","Heinä","Elo","Syys","Loka","Marras","Joulu"], firstDay: 1, showOn: "button", buttonImage: "/images/calendar.png", buttonImageOnly: true }); }); var delay = (function(){ var timer = 0; return function(callback, ms){ clearTimeout (timer); timer = setTimeout(callback, ms); }; })(); $('.search-autocomplete').keyup(function() { delay(function(){ var search_str = $('#search').val(); $.get("/search-autocomplete.php?search="+encodeURIComponent(search_str), {}, function(sdata) { $("#autocomplete-result").html(sdata); }); }, 400 ); }); refresh_description_expansions(); }); function refresh_description_expansions() { $('.description_expand').each(function() { var text_field = $(this); if (text_field.data('expander-initialized')) { return; } text_field.data('expander-initialized', true); var content_id = 'expandable-content-' + Math.random().toString(36).substr(2, 9); text_field.attr('id', content_id); var show_more_text = "Näytä lisää"; var show_less_text = "Näytä vähemmän"; var bottom_fade = $('
'); text_field.append(bottom_fade); var show_button = $(''); show_button.text(show_more_text); text_field.after(show_button); text_field.attr('data-show', 'false'); show_button.on('click', function() { var is_expanded = $(this).attr('aria-expanded') === 'true'; if (is_expanded) { $(this).attr('aria-expanded', 'false'); $(this).text(show_more_text); text_field.attr('data-show', 'false'); } else { $(this).attr('aria-expanded', 'true'); $(this).text(show_less_text); text_field.attr('data-show', 'true'); } }); }); } function WriteDiv(text, id) { $("#"+id).html(text); return false; } function focus() { document.search.search.focus(); } function isCheckedPaketti(checkbox) { if ( checkbox.checked != true ) { return confirm('Oletko varma ettet halua pakettialennusta tilauksellesi?'); } return true; } function toggleDivCheckbox(checkbox, id) { if ( checkbox.checked != true ) { hidediv(id); } else { showdiv(id); } return true; } function toggle(id) { if ( document.getElementById(id).style.display == 'none' ) { showdiv(id); } else { hidediv(id); } return false; } function showhide(id1,id2) { hidediv(id2); showdiv(id1); } function hidediv(id) { if (document.getElementById) { // DOM3 = IE5, NS6 document.getElementById(id).style.display = 'none'; } else { if (document.layers) { // Netscape 4 document.id.display = 'none'; } else { // IE 4 document.all.id.style.display = 'none'; } } } function showdiv(id) { if (document.getElementById) { // DOM3 = IE5, NS6 document.getElementById(id).style.display = 'block'; } else { if (document.layers) { // Netscape 4 document.id.display = 'block'; } else { // IE 4 document.all.id.style.display = 'block'; } } } function getSelectedOption(group) { if (group[0]) { for (var i=0; imydiv'); /*optional stylesheet*/ mywindow.document.write(''); mywindow.document.write(''); mywindow.document.write(''); mywindow.document.write(data); mywindow.document.write(''); mywindow.document.close(); mywindow.print(); return true; } function getPrivacyAttributeValues(){ let attr = {"marketing":1,"analytics":2}; return attr; } function setPrivacy(attributes, all){ if (typeof all != "boolean"){ all = false; } let privacy_settings = 0; let attr_values = getPrivacyAttributeValues(); for (var attr in attr_values){ let attr_value = typeof attr_values[attr] != "undefined" ? attr_values[attr] : false; if (all || (attributes[attr] && attr_value)){ privacy_settings = privacy_settings | attr_value; } } var d = new Date(); d.setTime(d.getTime() + (365*24*60*60*1000)); var expires = "expires="+ d.toUTCString(); document.cookie = "shop_privacy="+privacy_settings+";" + expires + ";path=/; Samesite=none; Secure"; } function getPrivacy(attribute){ let privacy = null; let cookies = document.cookie.split(";"); for (let i = 0; i < cookies.length; i++){ let cookie = cookies[i].split("="); if (cookie[0].trim() == "shop_privacy"){ privacy = cookie[1]; } } // Have privacy settings been set if (typeof attribute == "undefined"){ return !(privacy === null); } let privacy_attributes = getPrivacyAttributeValues(); if (typeof privacy_attributes[attribute] == "undefined"){ return false; } if (privacy !== null){ for (var attr in privacy_attributes){ if (attr == attribute){ if (privacy & privacy_attributes[attr]){ return true; }else{ return false; } } } } return false; } function privacyPopup(){ $('#privacy_blockscreen').remove(); let privacy_header = $('

Evästeet

'); let privacy_consent_container = $(''); let privacy_consent_text = `
Käytämme evästetietoja käyttökokemuksen parantamiseksi. Käytämme myös Googlen työkaluja sekä Facebook-pikseliä laadunvalvontaan sekä markkinointitarkoituksiin, jotka vaativat toimiakseen evästeen.
`; let privacy_link = ``; let privacy_details_container = $('
'); let marketing_description = `Custobar Google Ads Meta Platforms`; let analytics_description = `Google Analytics`; let basic_description = "Tietyt evästeet ovat aina käytössä jotta esimerkiksi ostoskori sekä kirjautuminen toimivat sivustolla."; let privacy_consent_options = $(''); $(privacy_consent_options).find('.cookie_description').each(function(){ $(this).html($(this).html().trim().replace(/\n/g, '
')); }); let buttons = $('
'); let allow_all = $(''); let modify = $(''); buttons.append(allow_all, modify); let lang_flags = [ $('Vaihda kieleksi Suomi'), $('Vaihda kieleksi Englanti'), $('Vaihda kieleksi Ruotsi') ]; if (lang_flags.length > 0){ let language_bar = $('
'); for (key in lang_flags){ language_bar.append(lang_flags[key]); } privacy_consent_container.append(language_bar); } privacy_consent_container.append(privacy_header, privacy_consent_text, privacy_details_container, buttons, privacy_link); let blockscreen = $('
'); $(blockscreen).append(privacy_consent_container); $('body').append(blockscreen); setTimeout(function() { const container = $('#privacy_consent_container'); container.focus(); const firstLangLink = container.find('.privacy_language_bar a:first'); if (firstLangLink.length > 0) { firstLangLink.focus(); } else { $('#privacy_allow_all').focus(); } }, 50); $('#privacy_allow_all').on('click', function(){ //setPrivacy(null, true); cookieConsent.consentToAllCookies(); $('#privacy_blockscreen').fadeOut('fast'); }); $('#privacy_modify').off().on('click', function(){ let allow = $(''); $('#privacy_modify').replaceWith(allow); $('#privacy_details_container').append(privacy_consent_options); $('#privacy_consent_option_list input[type="checkbox"]:not(:disabled):first').focus(); let expand_button = $(''); $('.cookie_description').each(function(){ if ($(this).text().match(/\w/)){ $(this).before(expand_button.clone()); } }); cookieConsent.initUserSelections(); $('.cookie_description_expand').on('click', function(){ let description = $(this).next(); let expanded = $(description).attr('data-expanded'); if (expanded == "true"){ $(this).toggleClass('icon-angle-up', false); $(this).toggleClass('icon-angle-down', true); $(description).attr('data-expanded', 'false'); $(this).attr('aria-expanded', 'false'); } else { $(this).toggleClass('icon-angle-down', false); $(this).toggleClass('icon-angle-up', true); $(description).attr('data-expanded','true'); $(this).attr('aria-expanded', 'true'); } }); $('#privacy_allow').on('click', function(){ let marketing = $('#cookie_marketing').prop("checked"); let analytics = $('#cookie_analytics').prop("checked"); let privacy_attributes = {marketing, analytics}; cookieConsent.consentToSelectedCookies(); $('#privacy_blockscreen').fadeOut('fast'); }); }); }