var video, sticker, poster, shake, timeoutID; var delta = 0.5; var script = [ { id: 1, time: 16, duration: 1, url: "https://www.foody.vn/bo-suu-tap/7upvietnam/nhung-tiem-pho-chat-luong-nen-den-7up-vietnam-8811255?utm_source=youtube&utm_medium=cpc&utm_campaign=7UP%20MV%20Pho", icon:"images/sticker.png", shake: "shake", x: 20, y: 30 }, { id: 22, time: 31, duration: 1, url: "https://www.foody.vn/bo-suu-tap/7upvietnam/nhung-tiem-pho-chat-luong-nen-den-7up-vietnam-8811255?utm_source=youtube&utm_medium=cpc&utm_campaign=7UP%20MV%20Pho", icon:"images/sticker.png", shake: "shake", x: 20, y: 30 }, { id: 2, time: 34, duration: 1, url: "https://www.foody.vn/bo-suu-tap/7upvietnam/nhung-tiem-pho-chat-luong-nen-den-7up-vietnam-8811255?utm_source=youtube&utm_medium=cpc&utm_campaign=7UP%20MV%20Pho", icon:"images/sticker.png", shake: "shake", x: 20, y: 30 }, { id: 3, time: 40, duration: 1, url: "https://www.foody.vn/bo-suu-tap/7upvietnam/nhung-tiem-pho-chat-luong-nen-den-7up-vietnam-8811255?utm_source=youtube&utm_medium=cpc&utm_campaign=7UP%20MV%20Pho", icon:"images/sticker.png", shake: "shake", x: 20, y: 30 }, { id: 4, time: 92, duration: 2, url: "https://www.foody.vn/bo-suu-tap/7upvietnam/nhung-tiem-bun-bo-hue-chat-luong-nen-den-7up-vietnam-8811858?utm_source=youtube&utm_medium=cpc&utm_campaign=7UP%20MV%20Bun%20Bo%20Hue", icon:"images/sticker.png", shake: "shake", x: 20, y: 30 }, { id: 5, time: 95.2, duration: 1, url: "https://www.foody.vn/bo-suu-tap/7upvietnam/nhung-tiem-bun-bo-hue-chat-luong-nen-den-7up-vietnam-8811858?utm_source=youtube&utm_medium=cpc&utm_campaign=7UP%20MV%20Bun%20Bo%20Hue", icon:"images/sticker_2.png", shake: "shake2", x: 220, y: 50 }, { id: 6, time: 97, duration: 6, url: "https://www.foody.vn/bo-suu-tap/7upvietnam/nhung-tiem-com-tam-chat-luong-nen-den-7up-vietnam-8812119?utm_source=youtube&utm_medium=cpc&utm_campaign=7UP%20MV%20Com%20Tam", icon:"images/sticker.png", shake: "shake", x: 20, y: 20 } ]; function init() { if(adUtils.Context.isDesktop()) { console.log("::isDesktop"); CreateInlinePlayer(true); } else { // init app if(adUtils.Context.isAndroid()) { console.log("::isAndroid"); adUtils.Context.canChromeAndroidAutoPlay(function(val){ if(adUtils.Context.chromeAndroidAutoPlayEnable) { CreateInlinePlayer(true); } else { CreateInlinePlayer(false); } }, "http://stream.adtimaserver.vn/2017/03/f74b0ffd-8ff0-4211-bc08-b4ae298aef5b.mp4"); } else if(adUtils.Context.isIos()) { console.log("::isIos"); if(adUtils.Context.isWhiteListedIos10Safari()) { CreateInlinePlayer(false); } else { // TODO: implement iOS < 10 later CreateInlinePlayer(false); } } else { console.log("//TODO: To be continued..."); CreateInlinePlayer(false); } } window.addEventListener('resize', onResize); onResize(); } function CreateInlinePlayer(autoplay) { video = document.getElementById('video'); sticker = document.getElementById('sticker'); shake = document.getElementById('shake'); poster = document.getElementById('poster'); sticker.addEventListener('click', stickerHDL); poster.addEventListener('click', replayVideo); video.addEventListener('timeupdate', onTimeUpdate); video.addEventListener('ended', onEndVideo); video.addEventListener('click', onClickVideo); video.setAttribute('webkit-playsinline', 'true'); video.setAttribute('playsinline', 'true'); enableVideos(); if(autoplay) { video.play(); poster.style.display = "none"; } else { console.log('CreateInlinePlayer: ' + autoplay); video.pause(); poster.style.display = "block"; sticker.style.display = "none"; } } function onClickVideo() { if(video.paused) { video.play(); } else { video.pause(); } } function enableVideos(everywhere) { if("makeVideoPlayableInline" in window) window.makeVideoPlayableInline(video, !video.hasAttribute('muted'), !everywhere); } function stickerHDL(evt) { window.open(sticker.url, '_blank'); } function replayVideo() { console.log('replayVideo'); clearTracking(); poster.style.display = "none"; sticker.style.display = "none"; video.currentTime = 0; video.play(); } function clearTracking() { for(var i = 0; i < script.length; i++) { var trackVO = script[i]; trackVO.isTrack = false; } } function onTimeUpdate(evt) { var del = Math.round((video.currentTime / video.duration) * 100); var currentTime = video.currentTime; var trackVO = getScript(currentTime); if(trackVO && trackVO.id != sticker.IDX) { console.log("TRACKING: " + trackVO.time + '_Played'); trackVO.isTrack = true; sticker.IDX = trackVO.id; sticker.style.display = 'block'; //sticker.style['background-image'] = "url('" + trackVO.icon + "')"; sticker.style['background-size'] = 'cover'; sticker.url = trackVO.url; sticker.style['top'] = Math.round((trackVO.y/315)*100) + '%'; sticker.style['left'] = Math.round((trackVO.x/560)*100) + '%'; sticker.classList.remove("zoomOut"); sticker.classList.add("zoomIn"); sticker.style.display = 'block'; if(trackVO.shake == "shake2") { sticker.style.width = "100px"; sticker.style.height = "100px"; } else { sticker.style.width = "150px"; sticker.style.height = "150px"; } shake.style.display = "none"; shake.src = trackVO.icon; shake.classList.add(trackVO.shake); shake.style.display = "block"; clearTimeout(hideSticker); timeoutID = setTimeout(hideSticker, trackVO.duration * 1000); } } function getScript(valTime) { var tmpScript; for ( var i = 0; i < script.length; i++) { tmpScript = script[i]; if(tmpScript.time - delta <= valTime && valTime <= tmpScript.time + delta) { return tmpScript; } } return null; } function hideSticker() { // sticker.style.display = 'none'; sticker.classList.remove("zoomIn"); sticker.classList.add("zoomOut"); shake.className = ""; sticker.IDX = ""; } function onEndVideo(evt) { console.log('video ended'); poster.style.display = "block"; } function onResize() { if(!adUtils.Context.isDesktop()) { if(window.innerWidth > window.innerHeight) { video.setAttribute('webkit-playsinline', 'true'); video.setAttribute('playsinline', 'true'); if (video.requestFullscreen) { video.requestFullscreen(); } else if (video.msRequestFullscreen) { video.msRequestFullscreen(); } else if (video.mozRequestFullScreen) { video.mozRequestFullScreen(); } else if (video.webkitRequestFullscreen) { video.webkitRequestFullscreen(); } } else { if (video.exitFullscreen) { video.exitFullscreen(); } else if (video.msExitFullscreen) { video.msExitFullscreen(); } else if (video.mozCancelFullScreen) { video.mozCancelFullScreen(); } else if (video.webkitExitFullscreen) { video.webkitExitFullscreen(); } video.removeAttribute('webkit-playsinline', 'true'); video.removeAttribute('playsinline', 'true'); } } }