var Incito = {}; // base configuration Incito = { catalogConfig: { order_by: 'publication_date', types: 'paged,incito', offset: 0, limit: 6 }, config: { dealer_id: '', openIncito: SGN.util.getQueryParam('openIncito'), openCategory: SGN.util.getQueryParam('openCategory'), dataId: '', targetId: '', maxRetry: 3, curRetry: 0, locale: 'da-DK', weeklyLength: 8, testLogging: false, stagingServer: false }, publication: { activePublication: {} }, data: { targetElement: {}, bootStrapper: {}, viewer: {} }, regex: { weekly: { 'da-DK': /^Uge \d{1,2}/, 'nb-NO': /^Uke \d{1,2}/, 'sv-SE': /^Vecka \d{1,2}/, 'fi-FI': /^Viikko \d{1,2}/ }, b2b: { 'da-DK': /^Erhverv .*/, 'nb-NO': /^B2B .*/, 'sv-SE': /^B2B .*/, 'fi-FI': /^B2B .*/ } }, search: { searchParam: '', searchResult: [], relatedResult: [], searchWording: { 'da-DK': 'Henter resultater ', 'nb-NO': 'Searching ', 'sv-SE': 'Searching ', 'fi-FI': 'Searching ' }, resultText: { 'da-DK': [' Resultater fundet', ' Relaterede produkter fundet'], 'nb-NO': [' Resultater fundet', ' Relaterede produkter fundet'], 'sv-SE': [' Resultater fundet', ' Relaterede produkter fundet'], 'fi-FI': [' Resultater fundet', ' Relaterede produkter fundet'] } }, dateFormat: { 'da-DK': ['Fra ', '/', ' t.o.m. ', '/'], 'nb-NO': ['Fra ', '/', ' til ', '/', ' 08:00'], 'sv-SE': ['Från ', '/', ' t.o.m. ', '/'], 'fi-FI': ['', '/', ' - ', '/'] } }; //google analytics var noop = function () {}; var nga = 'dataLayer' in window ? function (ctx) { dataLayer.push({ 'event': 'shopgun', 'shopgunCategory': ctx.eventCategory, 'shopgunAction': ctx.eventAction, 'shopgunLabel': ctx.eventLabel }); } : noop; var once = function (fun) { var done = false; return function () { if (!done) { done = true; return fun.apply(this, arguments); } }; }; var getPublicationRuntimeEventLabel = function (data) { //console.log(data.label + ' - ' + data.run_from.substr(0, 10)); return data.label + ' - ' + data.run_from.substr(0, 10); //return data.run_from.substr(0, 10) + '/' + data.run_till.substr(0, 10); }; var updateQueryStringParameter = function (uri, key, value) { var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i"); var separator = uri.indexOf('?') !== -1 ? "&" : "?"; if (uri.match(re)) { return uri.replace(re, '$1' + key + "=" + value + '$2'); } else { return uri + separator + key + "=" + value; } }; // used to add configuration settings to the script before running. Incito.Configure = function(configuration){ $.extend( Incito.config, configuration ); if(Incito.config.stagingServer == true){ SGN.config.set({ coreUrl: 'https://squid-api.tjek-staging.com', graphUrl: 'https://squid-api.tjek-staging.com' }); } } // loads publication on the offerspage based on openIncito variable in the adress. // current / future are speciels calls to get those regardless of ID. Incito.LoadPublication = function(){ Incito.TestLogging(SGN.config); if(Incito.config.openIncito !== undefined && Incito.config.dataId === ''){ if(Incito.config.openIncito === 'current' || Incito.config.openIncito === 'future'){ var configuration = { dealer_id: Incito.config.dealer_id }; $.extend( configuration, Incito.catalogConfig ); var r = []; Incito.FetchCatalogs(configuration, function (err, res) { Incito.TestLogging(err); Incito.TestLogging(res); if (!err && res && res.length > 0) { //res.reverse(); current = new Date(); future = new Date(); future.setDate(current.getDate()+Incito.config.weeklyLength); for(i = 0; i < res.length; i++){ if(Incito.regex.weekly[Incito.config.locale].test(res[i].label)){ if(Incito.ToDate(res[i].run_till) > current && Incito.ToDate(res[i].run_till) < future && Incito.config.openIncito === 'current'){ Incito.TestLogging(res[i].id); Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } else if(Incito.ToDate(res[i].run_from) < future && Incito.ToDate(res[i].run_till) > future && Incito.config.openIncito === 'future'){ Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } } } if(Incito.config.openIncito === 'future' && Incito.config.dataId === ''){ Incito.config.openIncito = 'current'; for(i = 0; i < res.length; i++){ if(Incito.regex.weekly[Incito.config.locale].test(res[i].label)){ if(Incito.ToDate(res[i].run_till) > current && Incito.ToDate(res[i].run_till) < future && Incito.config.openIncito === 'current'){ Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } } } } Incito.GetPubliction(); loadingPublication = setInterval(Incito.LoadingPublication, 1000); } }); } else if (Incito.config.openIncito === 'currentB2B' || Incito.config.openIncito === 'futureB2B'){ var configuration = { dealer_id: Incito.config.dealer_id }; $.extend( configuration, Incito.catalogConfig ); var r = []; Incito.FetchCatalogs(configuration, function (err, res) { Incito.TestLogging(err); Incito.TestLogging(res); if (!err && res && res.length > 0) { current = new Date(); future = new Date(); future.setDate(current.getDate()+Incito.config.weeklyLength); for(i = 0; i < res.length; i++){ if(Incito.regex.b2b[Incito.config.locale].test(res[i].label)){ if(Incito.ToDate(res[i].run_till) > current && Incito.ToDate(res[i].run_till) < future && Incito.config.openIncito === 'currentB2B'){ Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } else if(Incito.ToDate(res[i].run_from) < future && Incito.ToDate(res[i].run_till) > future && Incito.config.openIncito === 'futureB2B'){ Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } } } if(Incito.config.openIncito === 'futureB2B' && Incito.config.dataId === ''){ Incito.config.openIncito = 'currentB2B'; for(i = 0; i < res.length; i++){ if(Incito.regex.b2b[Incito.config.locale].test(res[i].label)){ if(Incito.ToDate(res[i].run_till) > current && Incito.ToDate(res[i].run_till) < future && Incito.config.openIncito === 'futureB2B'){ Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } } } } Incito.GetPubliction(); loadingPublication = setInterval(Incito.LoadingPublication, 1000); } }); } else { Incito.config.dataId = Incito.config.openIncito; Incito.GetPubliction(); loadingPublication = setInterval(Incito.LoadingPublication, 1000); } } else if(Incito.config.dataId !== ''){ Incito.GetPubliction(); loadingPublication = setInterval(Incito.LoadingPublication, 1000); } else{ Incito.TestLogging('No publication selected'); } }; // Tries to get the publication data from the incito API. Incito.GetPubliction = function(){ Incito.TestLogging('GetPubliction: ' + Incito.config.dataId); Incito.TestLogging('TargetId: ' + Incito.config.targetId); Incito.data.targetElement = document.getElementById(Incito.config.targetId); Incito.data.bootStrapper = new SGN.IncitoPublicationKit.Bootstrapper({ el: Incito.data.targetElement, id: Incito.config.dataId, eventTracker: SGN.config.get('eventTracker') }); Incito.data.bootStrapper.fetch(function (err, res) { Incito.TestLogging(err); Incito.TestLogging(res); Incito.publication.activePublication = res; Incito.publication.LoadingCompleted = true; }); }; // loop to make sure that publication loads correctly. Incito.LoadingPublication = function(){ if(Incito.publication.LoadingCompleted){ if(Incito.publication.activePublication == {} && Incito.config.curRetry < Incito.config.maxRetries){ Incito.TestLogging('retrying to get Incito'); Incito.publication.activePublication = undefined; Incito.config.curRetry++; } else if(Incito.publication.activePublication == {} && Incito.config.curRetry >= Incito.config.maxRetry){ console.error('max retries reached, stopping'); clearInterval(loadingPublication); } else if(Incito.publication.activePublication !== {}){ clearInterval(loadingPublication); Incito.OpenPublication(); } } }; // opens the loaded publication into the target element set in configuration. Incito.OpenPublication = function(){ //Incito.TestLogging(new Date()); //Incito.TestLogging(Incito.publication.activePublication.details); Incito.data.viewer = Incito.data.bootStrapper.createViewer({ incito: Incito.publication.activePublication.incito, details: Incito.publication.activePublication.details }); Incito.data.viewer.start(); if(Incito.publication.activePublication.details.label !== undefined && Incito.publication.activePublication.details.label !== ''){ incitoDataInput.Heading.Title = Incito.publication.activePublication.details.label; nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Opened', 'eventLabel': getPublicationRuntimeEventLabel(Incito.publication.activePublication.details) }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Opened', getPublicationRuntimeEventLabel(Incito.publication.activePublication.details)); } Incito.data.viewer.incito.bind('allRendered', function () { //Incito.TestLogging('allRendered'); OffersPage.Activate(); //Incito.TestLogging(new Date()); }); Incito.AddScrollElement(); Incito.AddHotspotLinks(); }; // adds links to the products. Incito.AddHotspotLinks = function(){ SGN.CoreUIKit.on(Incito.data.targetElement, 'click', '.incito__view[data-role="offer"]', function (e) { e.preventDefault(); var id = this.getAttribute('data-id'); var meta = Incito.data.viewer.incito.ids[id]; if (!meta || !meta['tjek.offer.v1'] || !meta['tjek.offer.v1'].link) { return; } //var url = updateQueryStringParameter(meta['tjek.offer.v1'].link, 'intcid', 'INT_INCITO_BUTTON'); var url = updateQueryStringParameter(meta['tjek.offer.v1'].link, 'itm_source', 'incito'); var url = updateQueryStringParameter(url, 'itm_campaign', incitoDataInput.Heading['Title'].replace(' ', '_') + '_' + new Date().getFullYear()); var url = updateQueryStringParameter(url, 'itm_medium', 'incito_product_link'); var url = updateQueryStringParameter(url, 'itm_content', meta['tjek.offer.v1'].ids[0].value); nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Offer Opened', 'eventLabel': id }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Offer Opened', id); window.open(url, '_blank'); }); }; // activate the scroll percentage counter. Incito.AddScrollElement = function(){ var trackProgress = function (progress) { nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Read-through ' + progress + '%', 'eventLabel': getPublicationRuntimeEventLabel(Incito.publication.activePublication.details) }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Read-through ' + progress + '%', getPublicationRuntimeEventLabel(Incito.publication.activePublication.details)); }; var navigationHandlers = { page2: once(function () { nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Read-through page 2', 'eventLabel': getPublicationRuntimeEventLabel(Incito.publication.activePublication.details) }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Read-through page 2', getPublicationRuntimeEventLabel(Incito.publication.activePublication.details)); }), progress20: once(function () { trackProgress(20) }), progress40: once(function () { trackProgress(40) }), progress60: once(function () { trackProgress(60) }), progress80: once(function () { trackProgress(80) }), progress100: once(function () { trackProgress(100) }) }; Incito.data.viewer.bind('progress', function (navEvent) { if (navEvent.progress >= 100) { navigationHandlers.progress100(); } else if (navEvent.progress >= 80) { navigationHandlers.progress80(); } else if (navEvent.progress >= 60) { navigationHandlers.progress60(); } else if (navEvent.progress >= 40) { navigationHandlers.progress40(); } else if (navEvent.progress >= 20) { navigationHandlers.progress20(); } }); }; Incito.RenderCatalogs = function(configurationInput){ configuration = {}; $.extend( configuration, Incito.catalogConfig ); $.extend( configuration, configurationInput ); if(configuration.locale !== undefined){ Incito.config.locale = configuration.locale; } if(configuration.catalogList !== undefined && configuration.catalogList.length > 0){ Incito.RenderSelectedCatalogs(configuration); } else { Incito.RenderPublishedCatalogs(configuration); } }; Incito.RenderPublishedCatalogs = function(configuration) { Incito.FetchCatalogs(configuration, function (err, res) { Incito.TestLogging(res); if (err){console.error(err)} else if(!err && res && res.length > 0){ res.sort(function (a, b) { var aDate = Incito.ToDate(a.run_from).getTime(); var bDate = Incito.ToDate(b.run_from).getTime(); return bDate - aDate; }); if(res.length > 2){ $(configuration.targetElement).addClass('incito-many'); } else { $(configuration.targetElement).addClass('incito-few'); } for(i = 0; i < res.length; i++){ Incito.RenderPublishedCatalogToHtml(configuration, Incito.CustomLabeler(configuration, res[i])); } } else if(!err && res && configuration.fallback){ Incito.RenderFallback(configuration); } }); }; Incito.RenderSelectedCatalogs = function(configuration){ if(configuration.catalogList.length > 2){ $(configuration.targetElement).addClass('incito-many'); } else { $(configuration.targetElement).addClass('incito-few'); } for(i = 0; i < configuration.catalogList.length; i++) { $(configuration.targetElement).append('
'); $(configuration.targetElement + ' #publication-'+configuration.catalogList[i]).hide(); } Incito.FetchCatalogs(configuration, function (err, res) { if (err){console.error(err)} else if(!err && res && res.length > 0){ res.sort(function (a, b) { var aDate = Incito.ToDate(a.run_from).getTime(); var bDate = Incito.ToDate(b.run_from).getTime(); return bDate - aDate; }); for(i = 0; i < res.length; i++){ a = Incito.CustomLabeler(configuration, res[i]); if(configuration.catalogList.includes(a.customLabel)){ Incito.RenderPublishedCatalogToHtml(configuration, a); } } for(i = 0; i < configuration.catalogList.length; i++){ if($(configuration.targetElement + ' #publication-'+configuration.catalogList[i]).text() === ''){ if(configuration.catalogList[i] === 'current' || configuration.catalogList[i] === 'future'){ } else{ $(configuration.targetElement + ' #publication-'+configuration.catalogList[i]).show(); el = $(configuration.targetElement + ' #publication-'+configuration.catalogList[i])[0]; bs = new SGN.IncitoPublicationKit.Bootstrapper({ el: el, id: configuration.catalogList[i] }); bs.fetch(function (errB, resB) { if (errB){} if (!errB && resB) { Incito.RenderPublishedCatalogToHtml(configuration, Incito.CustomLabeler(configuration, resB.details)); } }); } } } } }); }; Incito.RenderFallback = function(configuration){ $(configuration.targetElement).addClass('incito-few'); $(configuration.targetElement).prepend('
'); html = ''; if(configuration.fallback.href && configuration.fallback.href !== ''){ html += ''; } else { html += ''; } html += ''+configuration.fallback.title+''; html += '
' + configuration.fallback.subtitle + '
'; html += ''; html += '
'; $(configuration.targetElement + ' #publication-fallback').empty(); $(configuration.targetElement + ' #publication-fallback').append(html); } Incito.RenderPublishedCatalogToHtml = function(configuration, catalog){ title = catalog.label; subtitle = Incito.FormatDate(catalog); href = configuration.campaign_href; hrefparam = ''; Incito.TestLogging(catalog.label); Incito.TestLogging(Incito.regex.weekly[configuration.locale].test(catalog.label)); if(Incito.regex.weekly[configuration.locale].test(catalog.label)){ if(configuration.weekly_href === undefined || configuration.weekly_href === ''){ return true; } href = configuration.weekly_href; } else if(Incito.regex.b2b[configuration.locale].test(catalog.label)){ if(configuration.b2b_href === undefined || configuration.b2b_href === ''){ return true; } href = configuration.b2b_href; } else { if(configuration.campaign_href === undefined || configuration.campaign_href === ''){ return true; } href = configuration.campaign_href; } if(configuration.customConfigs !== undefined && configuration.customConfigs[catalog.customLabel] !== undefined){ if(configuration.customConfigs[catalog.customLabel].title !== undefined){ title = configuration.customConfigs[catalog.customLabel].title; } if(configuration.customConfigs[catalog.customLabel].subtitle !== undefined){ subtitle = configuration.customConfigs[catalog.customLabel].subtitle; } if(configuration.customConfigs[catalog.customLabel].hrefparam !== undefined && configuration.customConfigs[catalog.customLabel].hrefparam !== ''){ hrefparam = '&' + configuration.customConfigs[catalog.customLabel].hrefparam; } } if(!$(configuration.targetElement + ' #publication-'+catalog.customTarget).length){ if(Incito.regex.weekly[configuration.locale].test(catalog.label)){ $(configuration.targetElement).prepend('
'); } else{ $(configuration.targetElement).append('
'); } } else{ $(configuration.targetElement + ' #publication-'+catalog.customTarget).show(); } html = ''; html += ''; html += ''+title+''; html += '
' + subtitle + '
'; html += ''; html += '
'; $(configuration.targetElement + ' #publication-'+catalog.customTarget).empty(); $(configuration.targetElement + ' #publication-'+catalog.customTarget).append(html); }; Incito.CustomLabeler = function(configuration, catalog){ res = {}; $.extend( res, catalog ); if(Incito.regex.weekly[configuration.locale].test(catalog.label)){ current = new Date(); future = new Date(); future.setDate(current.getDate()+7); if(Incito.ToDate(catalog.run_from) < current && Incito.ToDate(catalog.run_till) > current){ $.extend( res, { customLabel: 'current', customTarget: 'current'} ); } else if(Incito.ToDate(catalog.run_from) < future && Incito.ToDate(catalog.run_till) > future){ $.extend( res, { customLabel: 'future', customTarget: 'future'} ); } } else if(Incito.regex.b2b[configuration.locale].test(catalog.label)){ current = new Date(); future = new Date(); future.setDate(current.getDate()+7); if(Incito.ToDate(catalog.run_from) < current && Incito.ToDate(catalog.run_till) > current){ $.extend( res, { customLabel: 'currentB2B', customTarget: 'currentB2B'} ); } else if(Incito.ToDate(catalog.run_from) < future && Incito.ToDate(catalog.run_till) > future){ $.extend( res, { customLabel: 'futureB2B', customTarget: 'futureB2B'} ); } } else{ $.extend( res, { customLabel: catalog.label, customTarget: catalog.id} ); } return res; }; // create links to the catalogs defined in the configurationList. Incito.RenderCatalogList = function(configurationList) { configuration = {}; $.extend( configuration, Incito.catalogConfig ); $.extend( configuration, configurationList[0] ); if(configuration.catalog_id === undefined && (configuration.catalogs === 'all' || configuration.catalogs === 'current' || configuration.catalogs === 'future' )){ Incito.FetchCatalogs(configuration, function (err, res) { if (err){} if (!err && res && res.length > 0) { res.sort(function (a, b) { var aDate = Incito.ToDate(a.run_from).getTime(); var bDate = Incito.ToDate(b.run_from).getTime(); if (configuration.catalogs === 'current') { return aDate - bDate; } else { return bDate - aDate; } }); if(configuration.catalogs === 'current' || configuration.catalogs === 'future'){ res.length = 1; if(configuration.catalogs === 'future' && Incito.ToDate(res[0].run_from) < new Date()){ {res.length = 0;} } } Incito.RenderCatalogsToHtml(configuration, res); } Incito.RenderCatalogListNext(configurationList); }); } else if (configuration.catalog_id !== undefined){ el = document.getElementById(configuration.targetId); bs = new SGN.IncitoPublicationKit.Bootstrapper({ el: el, id: configuration.catalog_id }); bs.fetch(function (err, res) { if (err){} if (!err && res) { r = [res.details]; Incito.RenderCatalogsToHtml(configuration, r); } Incito.RenderCatalogListNext(configurationList); }); } }; // step to next catalog in the configuration array. Incito.RenderCatalogListNext = function(configurationList) { if(configurationList.length > 1){ configurationList.shift(); Incito.RenderCatalogList(configurationList); } }; // get current catalogs from the incito API. Incito.FetchCatalogs = function(configuration, callback){ SGN.CoreKit.request({ url: '/v2/catalogs', qs: configuration }, callback); }; // generate the html for the catalog link and append it to its target location. Incito.RenderCatalogsToHtml = function(configuration, res){ var html = ''; res.forEach(function (item) { html += '
'; if(configuration.customHeader !== undefined){ html += '
' + configuration.customHeader + '
'; } else { html += '
' + Incito.FormatDate(item) + '
'; } html += '' html += ''; html += ''; html += '
'; }); $('#'+configuration.targetId).append(html); if ($('#'+configuration.targetId+' a').length){ $('#'+configuration.targetId).show(); $('#'+configuration.targetId+' .incoto-header').show(); if(configuration.matchedVisibility && configuration.matchedVisibility.length){ for(i = 0; i < configuration.matchedVisibility.length; i++){ $(configuration.matchedVisibility[i]).show(); } } } else { $('#'+configuration.targetId+' .incoto-header').hide(); $('#'+configuration.targetId).hide(); if(configuration.matchedVisibility.length){ for(i = 0; i < configuration.matchedVisibility.length; i++){ $(configuration.matchedVisibility[i]).hide(); } } } }; // format the dateline, make sure to add any localisations you need to this function, set locale in configuration. Incito.FormatDate = function(item){ var runFrom = Incito.ToDate(item.run_from); var runTill = Incito.ToDate(item.run_till); var formattedDate = ''; formattedDate = [ Incito.dateFormat[Incito.config.locale][0], runFrom.getDate(), Incito.dateFormat[Incito.config.locale][1], runFrom.getMonth() + 1, Incito.dateFormat[Incito.config.locale][2], runTill.getDate(), Incito.dateFormat[Incito.config.locale][3], runTill.getMonth() + 1, Incito.dateFormat[Incito.config.locale][4] != undefined ? Incito.dateFormat[Incito.config.locale][4] : '' ].join(''); /*if(Incito.config.locale === 'da-DK'){ formattedDate = ['Fra ', runFrom.getDate(), '/', runFrom.getMonth() + 1, ' t.o.m. ', runTill.getDate(), '/', runTill.getMonth() + 1].join(''); } else if(Incito.config.locale === 'nb-NO'){ formattedDate = ['Fra ', runFrom.getDate(), '/', runFrom.getMonth() + 1, ' t.o.m. ', runTill.getDate(), '/', runTill.getMonth() + 1].join(''); } else if(Incito.config.locale === 'sv-SE'){ formattedDate = ['Från ', runFrom.getDate(), '/', runFrom.getMonth() + 1, ' t.o.m. ', runTill.getDate(), '/', runTill.getMonth() + 1].join(''); } else if(Incito.config.locale === 'fi-FI'){ formattedDate = [runFrom.getDate(), '/', runFrom.getMonth() + 1, ' - ', runTill.getDate(), '/', runTill.getMonth() + 1].join(''); }*/ return formattedDate; }; // translate date string to Data object. Incito.ToDate = function(dtstr){ var dtcomps = dtstr.replace(/\D/g, ' ').split(' '); dtcomps[1]--; return new Date(Date.UTC(dtcomps[0], dtcomps[1], dtcomps[2], dtcomps[3], dtcomps[4], dtcomps[5])); }; Incito.Search = function(){ if(Incito.search.searchData === undefined || Incito.search.searchData === null || Incito.search.searchData.length === 0){ //Incito.search.searchData = Object.keys(Incito.data.viewer.incito.ids).map(function (key) { return Incito.data.viewer.incito.ids[key]; }); Incito.search.searchData = []; tmpSearchData = Object.keys(Incito.data.viewer.incito.ids).map(function (key) { return Incito.data.viewer.incito.ids[key]; }); //Incito.TestLogging(tmpSearchData); for(i = 0; i < tmpSearchData.length; i++){ title = tmpSearchData[i]['tjek.offer.v1'].title; sku = tmpSearchData[i]['tjek.offer.v1'].products[0].id; priceText = $('.incito__view[data-id='+sku+']').attr('aria-label'); priceText = priceText.split(','); priceText = priceText[priceText.length-1]; priceText = priceText.split(' kr.').join('.-'); priceText = priceText.split('kr.').join('.-'); price = parseInt(priceText.replace('.-', '').replace('.', '')); tmpList = $('#'+Incito.config.targetId + ' .incito__view[data-role=offer][data-id='+sku+'] p'); searchString = title; $(tmpList).each(function(){ searchString += $(this).text() + ' '; }); searchString = searchString.split('-').join(' '); cg = ''; if(tmpSearchData[i]['tjek.offer.v1'].ids !== undefined && tmpSearchData[i]['tjek.offer.v1'].ids.length > 0){ for(i2 = 0; i2 < tmpSearchData[i]['tjek.offer.v1'].ids.length; i2++){ if(tmpSearchData[i]['tjek.offer.v1'].ids[i2].type === 'cg'){ cg = tmpSearchData[i]['tjek.offer.v1'].ids[i2].value; } } } a = { Sku: sku, Title: title, PriceText: priceText, Price: price, SearchString: searchString, Cg: cg } Incito.search.searchData.push(a); } } Incito.search.searchResult = []; Incito.search.relatedResult = []; if(Incito.search.searchParam != ''){ for(i = 0; i < Incito.search.searchData.length; i++){ if(Incito.search.searchData[i].SearchString.toUpperCase().includes(Incito.search.searchParam.toUpperCase())){ Incito.search.searchResult.push(Incito.search.searchData[i]); } } ta = []; for(i = 0; i < Incito.search.searchResult.length; i++){ to = { Cg: Incito.search.searchResult[i].Cg, Nbr: 1, SkuList: []}; for(ita = 0; ita < ta.length; ita++){ if(ta[ita].Cg === to.Cg){ ta[ita].Nbr++; ta[ita].SkuList.push(Incito.search.searchResult[i].Sku); to.Nbr = ta[ita].Nbr; } } if(to.Nbr === 1){ to.SkuList.push(Incito.search.searchResult[i].Sku); ta.push(to); } } for(ita = 0; ita < ta.length; ita++){ if(ta[ita].Nbr >= (Incito.search.searchResult.length/2)){ for(i = 0; i < Incito.search.searchData.length; i++){ if(Incito.search.searchData[i].Cg === ta[ita].Cg && !ta[ita].SkuList.includes(Incito.search.searchData[i].Sku)){ Incito.search.relatedResult.push(Incito.search.searchData[i]); } } } } } Incito.RenderSearch(); }; Incito.RenderSearch = function(){ if(Incito.search.searchResult.length > 0){ nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Searched', 'eventLabel': Incito.search.searchParam + ' - ( ' + Incito.search.searchResult.length + ' Results )' }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Searched', Incito.search.searchParam + ' - ( ' + Incito.search.searchResult.length + ' Results )'); } $('#Incito_SearchResultArea').empty(); Incito.search.searchResult = Incito.search.searchResult.sort(function(a, b){return a.Price - b.Price}); Incito.search.relatedResult = Incito.search.relatedResult.sort(function(a, b){return a.Price - b.Price}); Incito.dateFormat[Incito.config.locale][0] if(Incito.search.searchResult.length > 0){ resultHtml = ''; resultHtml = '
'+Incito.search.searchResult.length+Incito.search.resultText[Incito.config.locale][0]+'
'; $('#Incito_SearchResultArea').append(resultHtml); } for(i = 0; i < Incito.search.searchResult.length; i++){ resultHtml = ''; resultHtml += '
'; resultHtml += '
'+Incito.search.searchResult[i].Title+'
'; resultHtml += '
'; resultHtml += '
'+Incito.search.searchResult[i].PriceText+'
'; resultHtml += '
'; $('#Incito_SearchResultArea').append(resultHtml); }; if(Incito.search.relatedResult.length > 0){ resultHtml = ''; resultHtml = '
'+Incito.search.relatedResult.length+Incito.search.resultText[Incito.config.locale][1]+'
'; $('#Incito_SearchResultArea').append(resultHtml); } for(i = 0; i < Incito.search.relatedResult.length; i++){ resultHtml = ''; resultHtml += '
'; resultHtml += '
'+Incito.search.relatedResult[i].Title+'
'; resultHtml += '
'; resultHtml += '
'+Incito.search.relatedResult[i].PriceText+'
'; resultHtml += '
'; $('#Incito_SearchResultArea').append(resultHtml); }; $('.Incito_SearchResult').click(function(){ //Incito.TestLogging($(this).attr('value')); OffersPage.CloseMobileMenu(); /*$([document.documentElement, document.body]).animate({ scrollTop: $('div.incito__view[data-id="'+$(this).attr('value')+'"]').offset().top }, 1000);*/ $('#incito-area').removeClass('incito-fade-in'); OffersPage.ScrollToDataId($(this).attr('value'), 1); $('#incito-area').addClass('incito-fade-in'); }); }; Incito.SearchLoading = function(){ if($('.searchLoading .dots').length === 0){ $('#Incito_SearchResultArea').empty(); $('#Incito_SearchResultArea').append('
'+Incito.search.searchWording[Incito.config.locale]+'
'); var searchLoading = 0; var loadingDotsAnimationInterval = setInterval(function(){ if($('.searchLoading .dots').length){ if(searchLoading < 3){ $('.searchLoading .dots').append('.'); searchLoading++; } else { $('.searchLoading .dots').empty(); searchLoading=0; } } else { clearInterval(loadingDotsAnimationInterval); } }, 500); } }; Incito.TestLogging = function(log){ if(Incito.config.testLogging){ console.log(log); } } var offersList = []; var searchList = []; function createOffersList(obj){ if(obj.child_views !== undefined && obj.child_views.length > 0){ for(i = 1; i < obj.child_views.length; i++){ if(obj.child_views[i].role === 'offer') { offersList.push(obj.child_views[i]); } if(obj.child_views[i].child_views !== undefined && obj.child_views[i].child_views.length > 0){ //createOffersList(obj.child_views[i]); } } } }