// THROTTLE function throttle(fn, threshhold, scope) { threshhold || (threshhold = 250); var last, deferTimer; return function () { var context = scope || this; var now = +new Date, args = arguments; if (last && now < last + threshhold) { // hold on to it clearTimeout(deferTimer); deferTimer = setTimeout(function () { last = now; fn.apply(context, args); }, threshhold); } else { last = now; fn.apply(context, args); } }; } // SECTIONS MANIPULATION $(document).ready(function () { var sections = $('section.container'); $(window).scroll(throttle(function () { var currentPosition = $(this).scrollTop(); sections.each(function () { var $el = $(this); var offset = $el.offset(), height = $el.height(); var top = offset.top, bottom = top + height; $el.toggleClass('selected', (currentPosition >= top && currentPosition < bottom)); }); }, 60)); }); // MANIPULATE MENU, CREDITS AND SHARE BUTTONS $(document).ready(function () { $(".menuToggleButton").click(function () { $("body").toggleClass("mobileMenuExpanded"); }); $(".creditsToggleButton").click(function () { $("body").toggleClass("creditsExpanded"); }); $(".socialLinks li span").click(function () { $(this).toggleClass("showThisShare"); }); }); // CHECKS IF VIDEO1 IS APPEARING ON SCREEN & CHANGE NAME ON SCREEN RESIZE $(document).ready(function () { checkIfMobile(); var maxWidth = window.matchMedia("(max-width: 500px)"); if (maxWidth.matches) { console.log($('#nameWork')); $('#nameWork')[0].childNodes[0].nodeValue = "Edwin"; console.log($('.copyright')); $('.copyright')[0].childNodes[0].textContent = "© Expresso - Impresa Publishing S.A. 2015"; showScrollBtn(); } else { if ($(window).scrollTop() !== 0) { rotateBtnImg(); var video1 = document.getElementById("video_1"); video1.autoplay = false; showScrollBtn(); } else { showScrollBtn(); } } }); //to hide creditsList when click outside div $(document).click(function (e) { var container = $(".creditsToggleButton"); if (!container.is(e.target) // if the target of the click isn't the container... && container.has(e.target).length === 0) // ... nor a descendant of the container { $('.creditsList').hide(); $("body").toggleClass("creditsExpanded"); } else { $('.creditsList').show(); } }); // oncanplay of video1 function switchBtn() { var video_1_currT = videojs('#video_1'); if (video_1_currT.currentTime() === 0) { rotateBtnImg(); showSkipBtn(); } } //check position of mobile device function isMobDeviceUp() { if ($(window).height() < $(window).width()) { // if mobile device is horizontal, appear an image all over the screen $('.rotateScreen')[0].style.visibility = "visible"; } else { $('.rotateScreen')[0].style.visibility = "hidden"; } } function checkIfMobile() { var maxWidth = window.matchMedia("(max-device-width: 500px)"), maxHeight = window.matchMedia("(max-device-height: 500px)"); if (maxWidth.matches || maxHeight.matches) { isMobDeviceUp(); } } window.onresize = function () { checkIfMobile(); } // AUTOPLAY VIDEOS function isReadyToPlay(elem) { var docViewTop = $(window).scrollTop(); var docViewBottom = docViewTop + $(window).height(); var elemTop = $(elem).offset().top; var elemBottom = elemTop + $(elem).height(); var topFraction = 10; var bottomFraction = 250; elemTop = elemTop - topFraction; elemBottom = elemBottom - bottomFraction; //check videos 1 & 2 position videos_position(); //return ( (elemBottom <= docViewBottom) && (elemTop >= docViewTop) ); return ( elemBottom > docViewTop && elemTop < docViewTop ) || ( elemTop < docViewBottom && elemBottom > docViewBottom ) || ( elemTop > docViewTop && elemBottom < docViewBottom ); } //check if we already pass trough video 2 var video_2_passTrough = false; function videos_position() { var $a = $('#video_1'), $b = $('#video_2'), $c = $('#video_6'), $d = $('#beforeLastVideo'); var video_1_heigthTop = inViewport($a), video_2_heigthTop = inViewport($b), video_6_heigthTop = inViewport($c), text_heigthTop = inViewport($d); //detect when video 1 appear if (video_1_heigthTop > 0 && video_2_passTrough && video_2_heigthTop === 0 || window.matchMedia("(max-width: 500px)").matches) { var video_1_currT = videojs('#video_1'); video_2_passTrough = false; var maxWidth = window.matchMedia("(max-width: 500px)"); if (!maxWidth.matches) { if (video_1_currT.currentTime() >= 94) { skipIntro(); } else { var video1 = document.getElementById("video_1"); video1.autoplay = true; if (!window.matchMedia("(max-width: 500px)").matches) { video_1_currT.play() } showSkipBtn(); } } } //detect when video 2 appear if (video_2_heigthTop > 0 && !video_2_passTrough) { if (video_1_heigthTop > 0) { var video_1_currT = videojs('#video_1'); video_1_currT.pause(); } video_2_passTrough = true; showScrollBtn(); } //if video1 and last are not appearing if (video_1_heigthTop === 0 && video_6_heigthTop === 0) { var video_1_currT = videojs('#video_1'); video_1_currT.pause(); showScrollBtn(); } //detect when last video appear if (video_6_heigthTop > 0 && text_heigthTop === 0) { showRiseUpBtn(); } } function inViewport($el) { var H = $(window).height(), r = $el[0].getBoundingClientRect(), t = r.top, b = r.bottom; return Math.max(0, t > 0 ? H - t : (b < H ? b : H)); } $(window).scroll(throttle(function () { //check videos 1 & 2 position videos_position(); $('.modernizr_no-touch .jsPlay video').each(function () { var $el = $(this).attr('data-i-target'); if ($el) { $el = $($el); } else { $el = this; } if (isReadyToPlay($el)) { this.play(); } else { this.pause(); } }); }, 60)); $('.modernizr_no-touch .jsPlay video').on('play', function () { console.log($(this)); $('.modernizr_no-touch .jsPlay video').not($(this).get(0)).each(function () { $(this).get(0).pause(); }); }); //REQUIRE JS requirejs.config({ baseUrl: 'common/js', paths: { 'videojs': 'plugins/video-js' } }); require(['videojs'], function (videojs) { videojs.options.flash.swf = "common/js/plugins/video-js.swf"; }); requirejs(["analytics"], function (Analytics) { var netscopeParameter = document.location.pathname; if (netscopeParameter === '/') { netscopeParameter = 'Homepage_do_site'; } var analytics = new Analytics({ googleanalytics: { id: 'UA-4263503-16', configObject: { 'cookieDomain': 'sapo.pt' } }, sapoanalytics: { swakt: "24b385df-4bb8-4e29-a51f-defae04777fd", swasection: document.location.pathname, swasubsection: document.location.pathname, swasectiongrp: 'Impresa Publishing', swasubsectiongrp: 317344, swacontent: '', swachannel: document.title }, netscope: { id: 'pzObuT8RwBPt9Har2hEswLb.LSnwzSdM1eV554gHvI7.Q7', extraparameters: netscopeParameter } }); analytics.init(); analytics.trackPage(document.location.pathname, document.title); }); // NEXT PAGE function nextPage() { var data, scroll, viewport, value = 0; scroll = $(window).scrollTop(); viewport = document.documentElement && document.documentElement.clientHeight || window.innerHeight; data = $('.anchorTarget').map(function (idx, el) { var $el = $(el); return { "$el": el, offset: $el.offset().top - scroll }; }).filter(function (idx, data) { return data.offset > viewport / 2; }).toArray(); data = data && data[0]; if (data && (data.offset < viewport || $(data.$el).hasClass('anchorForce'))) { value = scroll + data.offset; } else { value = scroll + (viewport * 0.9 | 0); } $("html, body").animate({ scrollTop: (value - 33) }, 'slow', 'swing'); return value; } function rotateBtnImg() { $(".navButton").addClass("transform"); } var currTime = 0; function skipIntro() { var video_1 = videojs('#video_1'); //hide play button video_1.bigPlayButton.hide(); //hide controls video_1.controlBar.hide(); currTime = video_1.currentTime(); video_1.play() video_1.currentTime(94); //hide skip button $("#skipBtn")[0].style.visibility = "hidden"; $('.layoutVersion_320 .navButton').css('width', ''); //rotate arrows $(".navButton").removeClass("transform"); //show button $("#scrollBtn")[0].style.visibility = "visible"; $("#playBtn")[0].style.visibility = "visible"; } // It goes to top of screen function riseUp() { $('html, body').animate({scrollTop: 0}); } function showScrollBtn() { //hide button $("#skipBtn")[0].style.visibility = "hidden"; $("#playBtn")[0].style.visibility = "hidden"; $("#riseUpBtn")[0].style.visibility = "hidden"; $('.layoutVersion_320 .navButton').css('width', '200px'); //rotate arrows $(".navButton").removeClass("transform"); $(".navButton").removeClass("transformUp"); //show scrolldown button $("#scrollBtn")[0].style.visibility = "visible"; } function showSkipBtn() { $('.layoutVersion_320 .navButton').css('width', '200px'); var video_1 = videojs('#video_1'); // hide poster image video_1.posterImage.hide(); //hide scrolldown button $("#scrollBtn")[0].style.visibility = "hidden"; $("#riseUpBtn")[0].style.visibility = "hidden"; //rotate arrows $(".navButton").removeClass("transformUp"); $(".navButton").addClass("transform"); //show skip button $("#skipBtn")[0].style.visibility = "visible"; } function showRiseUpBtn() { //hide button $("#scrollBtn")[0].style.visibility = "hidden"; $("#skipBtn")[0].style.visibility = "hidden"; //rotate arrow $(".navButton").addClass("transformUp"); //show button $("#riseUpBtn")[0].style.visibility = "visible"; } function playVideo() { $("#scrollBtn")[0].style.visibility = "hidden"; $("#playBtn")[0].style.visibility = "hidden"; $("#skipBtn")[0].style.visibility = "visible"; var video_1 = videojs('#video_1'); video_1.posterImage.hide(); video_1.controlBar.show(); video_1.play() if (currTime >= 94) { video_1.currentTime(0); } else { video_1.currentTime(currTime); } } // FIX VH AND VW FOR IOS 7 $(function () { var iOS = navigator.userAgent.match(/(iPod|iPhone|iPad)/); if (iOS) { function iosVhHeightBug() { var height = $(window).height(); setHeight100vh = $(".solidColorWrapper, .pinnedMedia .fullWidth .pictureWrapper, .pinnedMedia .fullWidth .videoWrapper, .scrollableContent .slide"); setMinHeight50vh = $(".scrollableContent"); setMinHeight100vh = $(".container .inner.viewportHeight"); setPaddingTop15vh = $(".layoutVersion_lt1024 .scrollableContent.contentInLine .listContainer"); setPaddingBottom50vh = $(".scrollableContent .listContainer"); $(setHeight100vh).css('height', height); $(setMinHeight50vh).css('min-height', (height * 0.5)); $(setMinHeight100vh).css('min-height', height); $(setPaddingTop15vh).css('padding-top', height * 0.15); $(setPaddingBottom50vh).css('padding-bottom', height * 0.5); } iosVhHeightBug(); $(window).bind('resize', iosVhHeightBug); } });