script2.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. lb.player = function lbPlayer() {
  2. var self = this;
  3. this.upNextTimer;
  4. this.current;
  5. this.title;
  6. this.subtitle;
  7. this.video;
  8. this.videoID;
  9. this.imgbg;
  10. this.elPlayer = document.getElementById('pl-player');
  11. this.elVideo = document.getElementById('pl-video');
  12. this.elTitle = document.getElementById('pl-player-title').getElementsByTagName('h1')[0];
  13. this.elSubtitle = document.getElementById('pl-player-title').getElementsByTagName('h3')[0];
  14. this.elimgbg = document.getElementById('pl-imgbg');
  15. this.elSeek = document.getElementById('pl-playback-progress');
  16. this.elTime = document.getElementById('pl-playback-time');
  17. this.elRuntime = document.getElementById('pl-runtime');
  18. this.elLoaded = document.getElementById('pl-playback-loaded');
  19. this.elProgressBarWrap = document.getElementById('pl-playback-progress-wrap');
  20. this.elProgressPosition = document.getElementById('pl-playback-position');
  21. this.elInfoTitle = document.getElementById('pl-info-series');
  22. this.elInfoSubtitle = document.getElementById('pl-info-episode');
  23. this.elPopup = document.getElementById('pl-next-popup');
  24. this.elPopupImage = document.getElementById('pl-next-popup-image');
  25. this.elPopupSubtitle = document.getElementById('pl-next-popup-subtitle');
  26. this.elUpNext = document.getElementById('pl-up-next');
  27. this.elUpNextWatchAgain = document.getElementById('pl-watch-again');
  28. this.elUpNextBg = document.getElementById('pl-up-next-bg');
  29. this.elUpNextImage = document.getElementById('pl-up-next-image');
  30. this.elUpNextTitle = document.getElementById('pl-up-next-title').getElementsByTagName('h1')[0];
  31. this.elUpNextSubtitle = document.getElementById('pl-up-next-title').getElementsByTagName('h3')[0];
  32. this.elUpNextCounter = document.getElementById('pl-up-next-timer');
  33. this.elUpNextGo = document.getElementById('pl-up-next-go');
  34. this.elControls = document.getElementById('pl-player-ctrl-wrap');
  35. this.elControlsPlayback = document.getElementById('pl-playback-ctrls-wrap');
  36. this.elControlsAudio = document.getElementById('pl-audio-ctrls-wrap');
  37. this.elControlPlay = document.getElementById('pl-playback-play');
  38. this.elControlPause = document.getElementById('pl-playback-pause');
  39. this.elControlVolume = document.getElementById('pl-audio-volume');
  40. this.elControlMute = document.getElementById('pl-audio-mute');
  41. this.elControlsNav = document.getElementById('pl-navigation-ctrls-wrap');
  42. this.elControlNext = document.getElementById('pl-next');
  43. this.finalScreen = document.getElementById('pl-final-screen');
  44. this.finalWatchAgain = document.getElementById('pl-final-watch-again');
  45. this.elVolumeWrap = document.getElementById('pl-volume-wrap');
  46. this.elVolume = document.getElementById('pl-volume');
  47. this.elVolumeBar = document.getElementById('pl-volume-bar');
  48. this.hookListeners();
  49. this.setCurrent(0);
  50. };
  51. lb.player.prototype.setTitle = function (value) {
  52. this.title = value;
  53. this.elTitle.innerHTML = this.elInfoTitle.innerHTML = value;
  54. }
  55. lb.player.prototype.setSubtitle = function (value) {
  56. this.subtitle = value;
  57. this.elSubtitle.innerHTML = this.elInfoSubtitle.innerHTML = value;
  58. }
  59. lb.player.prototype.setimgbg = function (value) {
  60. this.imgbg = value;
  61. this.elimgbg.style.backgroundImage = 'url("' + value + '")';
  62. }
  63. lb.player.prototype.setVideo = function (value) {
  64. var self = this;
  65. this.videoID = value;
  66. if(this.video) {
  67. this.video.loadVideoById(value);
  68. } else {
  69. this.video = new YT.Player(this.elVideo, {
  70. height: '768',
  71. width: '1366',
  72. videoId: value,
  73. playerVars: {
  74. controls: 0,
  75. disablekb: 1,
  76. enablejsapi: 1,
  77. loop: 0,
  78. modestbranding: 1,
  79. rel: 0,
  80. start: 0,
  81. showinfo: 0,
  82. iv_load_policy: 3,
  83. cc_load_policy: 1
  84. },
  85. events: {
  86. 'onReady': self.onPlayerReady.bind(this),
  87. 'onStateChange': self.onPlayerStateChange.bind(this)
  88. }
  89. });
  90. }
  91. }
  92. lb.player.prototype.changeInfo = function (title, subtitle, imgbg, video) {
  93. this.setTitle(title);
  94. this.setSubtitle(subtitle);
  95. this.setimgbg(imgbg);
  96. this.setVideo(video);
  97. }
  98. lb.player.prototype.showTime = function () {
  99. setInterval((function() {
  100. var remaining = this.video.getDuration() - this.video.getCurrentTime(),
  101. s = Math.floor(remaining) % 60,
  102. m = Math.floor(remaining / 60) % 60,
  103. h = Math.floor((remaining / 60) / 60) % 60;
  104. this.elTime.innerHTML = (h < 10 ? '' + h : h) + ':' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s);
  105. }).bind(this), 100);
  106. }
  107. lb.player.prototype.showLoaded = function () {
  108. setInterval((function() {
  109. this.elLoaded.style.width = this.video.getVideoLoadedFraction() * 100 + '%';
  110. }).bind(this), 100);
  111. }
  112. lb.player.prototype.showProgressPosition = function () {
  113. setInterval((function() {
  114. if(this.isSeeking) return;
  115. this.elProgressPosition.style.width = (this.video.getCurrentTime() / this.video.getDuration()) * 100 + '%';
  116. }).bind(this), 100);
  117. }
  118. lb.player.prototype.setCurrent = function(current) {
  119. clearInterval(this.upNextTimer);
  120. this.elPlayer.classList.add('loading');
  121. this.current = current;
  122. this.changeInfo(
  123. lb.videos[current].title,
  124. lb.videos[current].subtitle,
  125. lb.videos[current].imgbg,
  126. lb.videos[current].video);
  127. this.elUpNext.style.display = 'none';
  128. this.finalWatchAgain.style.backgroundImage =
  129. this.elUpNextWatchAgain.style.backgroundImage = 'url("' + lb.videos[current].thumb480 + '")';
  130. this.setNext();
  131. if(this.current === lb.videos.length - 1) {
  132. this.finalWatchAgain.style.backgroundImage = 'url("' + lb.videos[0].thumb480 + '")';
  133. }
  134. }
  135. lb.player.prototype.setNext = function () {
  136. if(this.current === lb.videos.length - 1){
  137. this.elPopupImage.style.backgroundImage = 'url("/img/obsabsorver.jpg")';
  138. this.elPopupSubtitle.innerHTML = "Outros títulos";
  139. return;
  140. }
  141. this.elUpNextBg.style.backgroundImage = 'url("' + lb.videos[this.current + 1].imgbg + '")';
  142. this.elUpNextImage.style.backgroundImage = 'url("' + lb.videos[this.current + 1].thumb480 + '")';
  143. this.elPopupImage.style.backgroundImage = 'url("' + lb.videos[this.current + 1].thumb130 + '")';
  144. this.elPopupSubtitle.innerHTML = this.elUpNextSubtitle.innerHTML = lb.videos[this.current + 1].subtitle;
  145. this.elUpNextTitle.innerHTML = lb.videos[this.current + 1].title;
  146. this.elRuntime.innerHTML = lb.videos[this.current + 1].runtime;
  147. }
  148. lb.player.prototype.hookListeners = function () {
  149. this.elPlayer.addEventListener('contextmenu', function(e){ e.preventDefault() });
  150. this.elControlPlay.addEventListener('click', this.onPlayClick.bind(this));
  151. this.elControlPause.addEventListener('click', this.onPauseClick.bind(this));
  152. this.elControlVolume.addEventListener('click', this.onVolumeClick.bind(this));
  153. this.elControlMute.addEventListener('click', this.onMuteClick.bind(this));
  154. this.elPopup.getElementsByClassName('inner')[0].addEventListener('click', this.onNextClick.bind(this));
  155. this.elControlNext.addEventListener('click', this.onNextClick.bind(this));
  156. this.elUpNextGo.addEventListener('click', this.onNextClick.bind(this));
  157. this.elUpNextWatchAgain.addEventListener('click', this.onWatchAgainClick.bind(this));
  158. this.finalWatchAgain.addEventListener('click', this.onWatchAgainClick.bind(this));
  159. this.elControlsAudio.addEventListener('mouseenter', this.onVolumeControlsHover.bind(this));
  160. this.elControlsAudio.addEventListener('mouseleave', this.onVolumeControlsLeave.bind(this));
  161. this.elControlsNav.addEventListener('mouseenter', this.onNextHover.bind(this));
  162. this.elControlsNav.addEventListener('mouseleave', this.onNextLeave.bind(this));
  163. this.elSeek.addEventListener('mousedown', this.startSeek.bind(this));
  164. document.addEventListener('mouseup', this.stopSeek.bind(this));
  165. this.elVolume.addEventListener('mousedown', this.startVolumeChange.bind(this));
  166. document.addEventListener('mouseup', this.stopVolumeChange.bind(this));
  167. this.elPlayer.addEventListener('mousemove', this.showControls.bind(this));
  168. window.addEventListener('keypress', (function(e) {
  169. var k = e.keyCode || e.which;
  170. if(k === 0 || k === 32) {
  171. if(this.video.getPlayerState() <= 0) return;
  172. if(this.video.getPlayerState() === 1) {
  173. this.onPauseClick();
  174. } else {
  175. this.onPlayClick();
  176. }
  177. }
  178. }).bind(this));
  179. document.getElementById('pl-fs').addEventListener('click', (function() {
  180. var elem = this.elPlayer;
  181. if (elem.requestFullscreen) {
  182. elem.requestFullscreen();
  183. } else if (elem.msRequestFullscreen) {
  184. elem.msRequestFullscreen();
  185. } else if (elem.mozRequestFullScreen) {
  186. elem.mozRequestFullScreen();
  187. } else if (elem.webkitRequestFullscreen) {
  188. elem.webkitRequestFullscreen();
  189. }
  190. document.getElementById('pl-fs-wrap').classList.add('fs');
  191. }).bind(this));
  192. document.getElementById('pl-fs-ex').addEventListener('click', (function(){
  193. if (document.exitFullscreen) {
  194. document.exitFullscreen();
  195. } else if (document.msExitFullscreen) {
  196. document.msExitFullscreen();
  197. } else if (document.mozCancelFullScreen) {
  198. document.mozCancelFullScreen();
  199. } else if (document.webkitExitFullscreen) {
  200. document.webkitExitFullscreen();
  201. }
  202. document.getElementById('pl-fs-wrap').classList.remove('fs');
  203. }).bind(this));
  204. }
  205. lb.player.prototype.onPlayerReady = function () {
  206. this.video.playVideo();
  207. this.showTime();
  208. this.showLoaded();
  209. this.showProgressPosition();
  210. }
  211. lb.player.prototype.onPlayerStateChange = function (e) {
  212. switch(e.data) {
  213. case -1: // Not initialized
  214. break;
  215. case 0: // Ended
  216. this.onPause.call(this);
  217. this.onEnded.call(this);
  218. break;
  219. case 1: // Playing
  220. this.onPlay.call(this);
  221. break;
  222. case 2: // Paused
  223. this.onPause.call(this);
  224. break;
  225. case 3: // Buffering
  226. break;
  227. case 5: // Cued
  228. break;
  229. }
  230. }
  231. lb.player.prototype.onPlay = function () {
  232. this.elPlayer.classList.remove('loading');
  233. this.elControlsPlayback.classList.add('pause');
  234. this.elControlsPlayback.classList.remove('play');
  235. this.elVolumeBar.style.height = this.video.getVolume() + '%';
  236. this.showControls();
  237. }
  238. lb.player.prototype.onPause = function () {
  239. this.elControlsPlayback.classList.add('play');
  240. this.elControlsPlayback.classList.remove('pause');
  241. }
  242. lb.player.prototype.onEnded = function () {
  243. if(this.current < lb.videos.length - 1) {
  244. this.elUpNext.style.display = 'block';
  245. var time = 15;
  246. this.upNextTimer = setInterval((function() {
  247. if(time > 0) {
  248. time--;
  249. this.elUpNextCounter.innerHTML = time + ' segundos';
  250. } else {
  251. this.onNextClick(this);
  252. this.elUpNextCounter.innerHTML = '15 segundos';
  253. }
  254. }).bind(this), 1000);
  255. } else {
  256. this.finalScreen.style.display = 'block';
  257. }
  258. }
  259. lb.player.prototype.onPlayClick = function () {
  260. this.video.playVideo();
  261. }
  262. lb.player.prototype.onPauseClick = function () {
  263. this.video.pauseVideo();
  264. }
  265. lb.player.prototype.onVolumeClick = function () {
  266. this.video.mute();
  267. this.elControlsAudio.classList.add('mute');
  268. this.elControlsAudio.classList.remove('volume');
  269. }
  270. lb.player.prototype.onMuteClick = function () {
  271. this.video.unMute();
  272. this.elControlsAudio.classList.add('volume');
  273. this.elControlsAudio.classList.remove('mute');
  274. }
  275. lb.player.prototype.onNextClick = function () {
  276. if(this.current < lb.videos.length - 1) {
  277. this.setCurrent(this.current + 1);
  278. } else {
  279. this.video.pauseVideo();
  280. this.finalScreen.style.display = 'block';
  281. }
  282. }
  283. lb.player.prototype.onWatchAgainClick = function () {
  284. this.finalScreen.style.display = 'none';
  285. if(this.current === lb.videos.length - 1) {
  286. this.setCurrent(0);
  287. } else {
  288. this.setCurrent(this.current);
  289. }
  290. }
  291. lb.player.prototype.hideVolumeWrap = function () {
  292. this.elVolumeWrap.style.display = 'none';
  293. this.elVolumeWrap.removeEventListener('transitionend', this.hideVolumeWrapBind)
  294. }
  295. lb.player.prototype.onVolumeControlsHover = function () {
  296. this.hideSeekBar();
  297. this.elVolumeWrap.removeEventListener('transitionend', this.hideVolumeWrapBind);
  298. this.elVolumeWrap.style.display = 'block';
  299. setTimeout((function(){
  300. this.elVolumeWrap.style.opacity = 1;
  301. }).bind(this), 10);
  302. }
  303. lb.player.prototype.onVolumeControlsLeave = function () {
  304. this.showSeekBar();
  305. this.hideVolumeWrapBind = (function() {
  306. this.elVolumeWrap.style.display = 'none';
  307. this.elVolumeWrap.removeEventListener('transitionend', this.hideVolumeWrapBind);
  308. }).bind(this);
  309. this.elVolumeWrap.addEventListener('transitionend', this.hideVolumeWrapBind);
  310. var timer = setInterval((function() {
  311. if(this.isChangingVolume) {
  312. return;
  313. }
  314. this.elVolumeWrap.style.opacity = 0;
  315. clearInterval(timer);
  316. }).bind(this),10);
  317. }
  318. lb.player.prototype.hideNext = function () {
  319. this.elPopup.style.display = 'none';
  320. this.elPopup.removeEventListener('transitionend', this.hideNextBind)
  321. }
  322. lb.player.prototype.onNextHover = function () {
  323. this.hideSeekBar();
  324. this.elPopup.removeEventListener('transitionend', this.hideNextBind);
  325. this.elPopup.style.display = 'block';
  326. setTimeout((function(){
  327. this.elPopup.style.opacity = 1;
  328. }).bind(this),10);
  329. }
  330. lb.player.prototype.onNextLeave = function () {
  331. this.showSeekBar();
  332. this.hideNextBind = (function() {
  333. this.elPopup.style.display = 'none';
  334. this.elPopup.removeEventListener('transitionend', this.hideNextBind);
  335. }).bind(this);
  336. this.elPopup.addEventListener('transitionend', this.hideNextBind);
  337. this.elPopup.style.opacity = 0;
  338. }
  339. lb.player.prototype.startSeek = function (e) {
  340. this.isSeeking = true;
  341. this.bindSeek = this.seek.bind(this);
  342. this.bindSeek(e);
  343. this.elSeek.addEventListener('mousemove', this.bindSeek);
  344. }
  345. lb.player.prototype.stopSeek = function (e) {
  346. if(!this.isSeeking) return;
  347. var layerX = e.clientX - this.elSeek.getBoundingClientRect().left,
  348. seconds = (layerX / this.elSeek.clientWidth) * this.video.getDuration();
  349. seconds = (seconds < 0 ? 0 : seconds > this.video.getDuration()-1 ? this.video.getDuration()-1 : seconds);
  350. this.video.seekTo(seconds, true);
  351. this.isSeeking = false;
  352. this.elSeek.removeEventListener('mousemove', this.bindSeek);
  353. }
  354. lb.player.prototype.seek = function(e) {
  355. var layerX = e.clientX - this.elSeek.getBoundingClientRect().left,
  356. seconds = (layerX / this.elSeek.clientWidth) * this.video.getDuration(),
  357. progress = (seconds / this.video.getDuration()) * 100;
  358. progress = (progress < 0 ? 0 : progress > 100 ? 100 : progress);
  359. seconds = (seconds < 0 ? 0 : seconds > this.video.getDuration() ? this.video.getDuration() : seconds);
  360. this.video.seekTo(seconds, false);
  361. this.elProgressPosition.style.width = progress + '%';
  362. }
  363. lb.player.prototype.startVolumeChange = function(e) {
  364. this.isChangingVolume = true;
  365. this.bindVolumeChange = this.volumeChange.bind(this);
  366. this.bindVolumeChange(e);
  367. this.elVolume.addEventListener('mousemove', this.bindVolumeChange);
  368. }
  369. lb.player.prototype.stopVolumeChange = function(e) {
  370. if(!this.isChangingVolume) return;
  371. var layerY = e.clientY - this.elVolume.getBoundingClientRect().top,
  372. volume = 100 - (layerY / this.elVolume.clientHeight) * 100;
  373. this.isChangingVolume = false;
  374. this.elVolume.removeEventListener('mousemove', this.bindVolumeChange);
  375. }
  376. lb.player.prototype.volumeChange = function(e) {
  377. if(window.YT === undefined) return;
  378. var layerY = e.clientY - this.elVolume.getBoundingClientRect().top,
  379. volume = 100 - (layerY / this.elVolume.clientHeight) * 100;
  380. volume = (volume < 0 ? 0 : volume > 100 ? 100 : volume);
  381. this.video.setVolume(volume);
  382. this.elVolumeBar.style.height = volume + '%';
  383. };
  384. lb.player.prototype.hideSeekBar = function () {
  385. this.elProgressBarWrap.classList.add('hide');
  386. }
  387. lb.player.prototype.showSeekBar = function () {
  388. this.elProgressBarWrap.classList.remove('hide');
  389. }
  390. lb.player.prototype.hideControls = function () {
  391. this.controlsHideBind = (function(){
  392. document.getElementById('exit-button').style.display = 'none';
  393. document.getElementById('exit-button').addEventListener('transitionend', this.controlsHideBind);
  394. this.elControls.style.display = 'none';
  395. this.elControls.addEventListener('transitionend', this.controlsHideBind);
  396. }).bind(this);
  397. document.getElementById('exit-button').addEventListener('transitionend', this.controlsHideBind);
  398. document.getElementById('exit-button').classList.add('hide');
  399. this.elControls.addEventListener('transitionend', this.controlsHideBind);
  400. this.elControls.classList.add('hide');
  401. }
  402. lb.player.prototype.showControls = function () {
  403. clearTimeout(this.controlsTimer);
  404. document.getElementById('exit-button').style.display = 'block';
  405. document.getElementById('exit-button').removeEventListener('transitionend', this.controlsHideBind);
  406. document.getElementById('exit-button').classList.remove('hide');
  407. this.elControls.style.display = 'block';
  408. this.elControls.removeEventListener('transitionend', this.controlsHideBind);
  409. this.elControls.classList.remove('hide');
  410. this.controlsTimer = setTimeout(this.hideControls.bind(this), 3000);
  411. }