// ==== Offerspage script ==== // Created by : Fredrik Frederiksen // Version : 2.0.1 // Update Date : 22/01-2019 // ==== ==== var OffersPage = {}; var loadingScrollPos = 0; //used to start the offerspage. //call from a document ready after incito loading has started. OffersPage.Activate = function() { //Incito.TestLogging('activate'); if(Incito.config.openCategory != undefined) OffersPage.IntialPosition(); OffersPage.MenuWidth(); OffersPage.SetMenuTitle(); OffersPage.CreateMenu(); OffersPage.CreateBanners(); OffersPage.CreateMenuSectors(); OffersPage.ActivateBackButton(); OffersPage.ActivateMobileButton(); OffersPage.ActivateScrollDetection(); OffersPage.ActivateSearch(); }; //find initial position in the incito baased on url param 'openCategory'. OffersPage.IntialPosition = function(){ initialCat = {}; for(i = 0; i < incitoDataInput.Menu.length; i++){ for(ti = 0; ti < incitoDataInput.Menu[i]['data-ids'].length; ti++){ if(incitoDataInput.Menu[i]['data-ids'][ti] === Incito.config.openCategory){ initialCat = incitoDataInput.Menu[i]; } } } initialTarget = ''; Incito.TestLogging(initialCat['data-ids']); if(initialCat['data-ids'] !== undefined){ $('.incito .incito__view ').each(function(){ if($(this).attr('data-id') !== undefined){ var dataId = $(this).attr('data-id'); for(i = 0; i < initialCat['data-ids'].length; i++){ if(initialTarget === '' && initialCat['data-ids'][i] === dataId){ initialTarget = dataId } } } }); Incito.TestLogging(initialTarget == ''); if(!initialTarget == ''){ OffersPage.ScrollToDataId(initialTarget, 1); } } }; //Fixes the width of the menu in desktop mode to avoid overlapping the catalog. //Runs as loop on OffersPage.Activate. OffersPage.MenuWidth = function(){ if($('body').width() > 685){ if($('#'+Incito.config.targetId+' > .incito > .incito__view').width() !== undefined){ if($('body').width() - $('#'+Incito.config.targetId+' > .incito > .incito__view').width() < 300){ $('#offers-menu-area').width($('body').width() - $('#'+Incito.config.targetId+' > .incito > .incito__view').width()); } } } }; //Activates the backbutton for mobile users. OffersPage.ActivateBackButton = function(){ $('#back-button').click(function(){ nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Top Buttons Clicked', 'eventLabel': 'Back Button' }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Top Buttons Clicked', 'Back Button'); if (window.history.length === 1 || document.referrer === undefined || !document.referrer.includes(window.location.hostname)) { document.location = incitoDataInput.Heading['ReturnPage']; } else { window.history.back(); } }); }; //Sets the title at the top of the menu based on included data file. //Runs as loop on OffersPage.Activate. OffersPage.SetMenuTitle = function(){ if($('head title').text().indexOf(incitoDataInput.Heading['Title']) === -1){ $('head title').append(incitoDataInput.Heading['Title']); } if(Incito.publication.activePublication.details !== undefined){ $('#offers-menu-title-area').prepend(incitoDataInput.Heading['Title']); incitoDataInput.Heading['TitleSub'] = Incito.FormatDate(Incito.publication.activePublication.details); if(incitoDataInput.Heading['TitleSubCustom'].length){ for(i = 0; i < incitoDataInput.Heading['TitleSubCustom'].length; i++){ if(incitoDataInput.Heading['TitleSubCustom'][i]['Label'] === Incito.publication.activePublication.details.label){ incitoDataInput.Heading['TitleSub'] = incitoDataInput.Heading['TitleSubCustom'][i]['TitleSub']; } } } $('#offers-menu-title-area .offers-menu-title-sub').append(incitoDataInput.Heading['TitleSub']); } }; //Variables used by OffersPage.CreateMenu() var menuItems = []; //Loop to ensure all categories gets added to the menu while the catalog loads. //Runs as loop on OffersPage.Activate. OffersPage.CreateMenu = function(){ $('#offers-menu-categories').addClass('active'); $('.loading-area').addClass('fade-away'); $('#incito-area').addClass('incito-fade-in'); $('#offers-menu-area').removeClass('waiting'); OffersPage.AddMenuButtons(); OffersPage.SetActiveCategory(); }; //Adds buttons to the menu OffersPage.AddMenuButtons = function(){ testerVar = 0; //loop through the incito catalog and find matching categories in datasheet $('.incito .incito__view ').each(function(){ if($(this).attr('data-id') !== undefined){ var dataId = $(this).attr('data-id'); for(i = 0; i < incitoDataInput.Menu.length; i++){ for(ti = 0; ti < incitoDataInput.Menu[i]['data-ids'].length; ti++){ //Incito.TestLogging(incitoDataInput.Menu[i]['data-ids'][ti]); //Incito.TestLogging(dataId); if(incitoDataInput.Menu[i]['data-ids'][ti] === dataId){ var addToMenu = true; for(mi = 0; mi < menuItems.length; mi++){ if(menuItems[mi]['Title'] === incitoDataInput.Menu[i]['Title']){ addToMenu = false; } } if(addToMenu){ menuItems.push({'Title':incitoDataInput.Menu[i]['Title'], 'data-ids': incitoDataInput.Menu[i]['data-ids'], 'Target': dataId}); } } } } } }); //add new categories to the menu for(i = $('#offers-menu-categories .offers-menu-categories-button').length; i < menuItems.length; i++){ $('#offers-menu-categories').append('
'+menuItems[i]['Title']+'
'); } //rebind the click event for scroll on menu items $('.offers-menu-categories-button').unbind('click'); $('.offers-menu-categories-button').click(function(){ OffersPage.CloseMobileMenu(); $('#incito-area').removeClass('incito-fade-in'); OffersPage.ScrollToDataId($(this).attr('value'), 1); $('#incito-area').addClass('incito-fade-in'); /*$([document.documentElement, document.body]).animate({ scrollTop: $('div.incito__view[data-id="'+$(this).attr('value')+'"]').offset().top }, 1000);*/ nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Category Changed', 'eventLabel': $(this).attr('data-gaAction') }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Category Changed', $(this).attr('data-gaAction')); }); //test if menu needs scroll pointers OffersPage.ActivateScrollPointers(); OffersPage.SetProgressCategory(); $('.new-fade-in').removeClass('new-fade-in'); }; //Adds an event to scrolling to add the indicator for current category OffersPage.ActivateScrollDetection = function(){ $(window).scroll(function(){ OffersPage.SetActiveCategory(); OffersPage.SetProgressCategory(); }); }; //Finds the currently active category and adds a marker to it in the menu. OffersPage.SetActiveCategory = function(){ var first = ''; $("#incito-area #incito__publication .incito .incito__view").each(function() { if ($(this).offset().top > ($(window).scrollTop() - $(this).height() + 150)) { first = $(this).attr('data-id'); if(first != undefined) { for(i = 0; i < incitoDataInput.Menu.length; i++){ for(ti = 0; ti < incitoDataInput.Menu[i]['data-ids'].length; ti++){ if(incitoDataInput.Menu[i]['data-ids'][ti] === first){ $('#offers-menu-categories .offers-menu-categories-button').each(function(){ if($(this).text() == incitoDataInput.Menu[i]['Title']){ if(!$(this).hasClass('active')){ $('#offers-menu-categories .offers-menu-categories-button').removeClass('active'); $(this).addClass('active'); } } }); return false; } } } } } }); }; OffersPage.SetProgressCategory = function() { if(!$('.sgn-incito__progress .progressCategory').length){ currCategory = $('#offers-menu-categories .offers-menu-categories-button.active').text(); if(currCategory === ''){ currCategory = $('#offers-menu-categories .offers-menu-categories-button').first().text(); } $('.sgn-incito__progress').append('
'+currCategory+'
'); } }; OffersPage.ScrollToDataId = function(dataId, timer){ $([document.documentElement, document.body]).animate({ scrollTop: $('div.incito__view[data-id="'+dataId+'"]').offset().top }, timer); }; //add the scrolling markers to the menu when scroll is requered. OffersPage.ActivateScrollPointers = function(){ $('.scroll-pointer').removeClass('show'); if($('#offers-menu-left-content > div.active').height() > $('#offers-menu-left-content').height()) { if($('#offers-menu-left-content').scrollTop() === 0){ $('.scroll-pointer.scroll-pointer-top').removeClass('show'); } else { $('.scroll-pointer.scroll-pointer-top').addClass('show'); } if(($('#offers-menu-left-content > div.active').height() - $('#offers-menu-left-content').height()) - $('#offers-menu-left-content').scrollTop() < 2){ $('.scroll-pointer.scroll-pointer-bot').removeClass('show'); } else { $('.scroll-pointer.scroll-pointer-bot').addClass('show'); } $('#offers-menu-left-content').unbind('scroll'); $('#offers-menu-left-content').scroll(function(){ if($('#offers-menu-left-content').scrollTop() === 0){ $('.scroll-pointer.scroll-pointer-top').removeClass('show'); } else { $('.scroll-pointer.scroll-pointer-top').addClass('show'); } if(($('#offers-menu-left-content > div.active').height() - $('#offers-menu-left-content').height()) - $('#offers-menu-left-content').scrollTop() < 2){ $('.scroll-pointer.scroll-pointer-bot').removeClass('show'); } else { $('.scroll-pointer.scroll-pointer-bot').addClass('show'); } }); } }; //Creates the menu sectors at the top of the menu area. OffersPage.CreateMenuSectors = function(){ $('#offers-menu-right-content').append('
'); $('#offers-menu-right-content').append('
Menu
'); for(i = 0; i < incitoDataInput.Information.length; i++){ htmlGen = ''; htmlGen += '
'; htmlGen += incitoDataInput.Information[i]['Icon']; htmlGen += '
'+incitoDataInput.Information[i]['Title']+'
'; $('#offers-menu-right-content').append(htmlGen); $('#offers-menu-left-content').append('
'+incitoDataInput.Information[i]['Content']+'
'); } $('#offers-menu-categories-button').addClass('active'); $('#offers-menu-right-content > div').click(function(){ if($(this).attr('target') !== undefined){ $('#offers-menu-right-content > div').removeClass('active'); $('#offers-menu-left-content > div').removeClass('active'); $(this).addClass('active'); $('#offers-menu-left-content > div[name="'+$(this).attr('target')+'"]').addClass('active'); nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Top Buttons Clicked', 'eventLabel': $(this).attr('data-gaAction') }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Top Buttons Clicked', $(this).attr('data-gaAction')); $('.scroll-pointer').removeClass('show'); setTimeout(OffersPage.ActivateScrollPointers, 1000); } }); $('#offers-menu-totop').click(function(){ OffersPage.CloseMobileMenu(); $('#incito-area').removeClass('incito-fade-in'); $([document.documentElement, document.body]).animate({ scrollTop: $('#incito__publication').offset().top }, 1); $('#incito-area').addClass('incito-fade-in'); nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Top Buttons Clicked', 'eventLabel': $(this).attr('data-gaAction') }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Top Buttons Clicked', $(this).attr('data-gaAction')); }); }; //Creates the banners at the bottom of the menu area, if any are supplied in the data file. OffersPage.CreateBanners = function(){ if(incitoDataInput.Banners !== undefined && incitoDataInput.Banners.length > 0){ for(i = 0; i < incitoDataInput.Banners.length; i++){ $('#offers-menu-banner-area .offers-menu-banner-carousel').append(''); } $('#offers-menu-area').addClass('banner-active'); offersPageBannerCarouselInterval = setInterval(OffersPage.BannerCarousel, 10000); window.onresize(); } else { $('#offers-menu-banner-area').hide(); } }; //variables used by the carousel. var bannerCarouselWidth = 0; var offersPageBannerCarouselCurrent = 0; //spins the banner carousel. OffersPage.BannerCarousel = function(){ offersPageBannerCarouselCurrent++; if(offersPageBannerCarouselCurrent >= incitoDataInput.Banners.length){ offersPageBannerCarouselCurrent = 0; } $('#offers-menu-banner-area .offers-menu-banner-carousel').css('transform', 'translateX(-' + offersPageBannerCarouselCurrent * bannerCarouselWidth + 'px)'); }; //fix for banner size during resize. window.onresize = function(){ bannerHeightInterval = setInterval(function(){ if($('#offers-menu-banner-area').height() > 100){ clearInterval(bannerHeightInterval); h = $('#offers-menu-area').height() - $('#offers-menu-title-area').height() - $('#offers-menu-banner-area').height(); h = h - 80; $('.banner-active #offers-menu-left-background #offers-menu-left-content').height(h); h = h - 15; $('.banner-active #offers-menu-left-background .scroll-pointer.scroll-pointer-bot').css('top', h); bannerCarouselWidth = $('#offers-menu-banner-area').width(); } }, 1000); } //activate the button on mobile to open the menu. OffersPage.ActivateMobileButton = function(){ $('#mobile-button').click(function(){ $('#mobile-button').toggleClass('open-mobile'); $('#offers-menu-area').toggleClass('open-mobile'); $('.sgn-incito__progress').toggleClass('hidden'); nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Top Buttons Clicked', 'eventLabel': 'Menu Button' }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Top Buttons Clicked', 'Menu Button'); }); }; //make the menu hide itself. OffersPage.CloseMobileMenu = function(){ $('.open-mobile').removeClass('open-mobile'); $('.sgn-incito__progress').removeClass('hidden'); }; OffersPage.GetUrlVars = function() { var vars = [], hash; var hashes = window.location.href.slice(window.location.href.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 searchDelay; OffersPage.ActivateSearch = function(){ $('#Incito_SearchBox').on('keyup', function() { if($(this).val() != ''){ Incito.SearchLoading(); } tmpSearchTerm = $(this).val(); tmpSearchTerm = tmpSearchTerm.split('-').join(' '); Incito.search.searchParam = tmpSearchTerm; clearTimeout(searchDelay); searchDelay = setTimeout(Incito.Search, 1000); }); };