(function () { videoPlayer = function(mediaPlayer, item, startPosition, user) { if (mediaPlayer == null) { throw new Error("mediaPlayer cannot be null"); } if (item == null) { throw new Error("item cannot be null"); } if (user == null) { throw new Error("user cannot be null"); } var self = mediaPlayer; var currentItem; var timeout; var video; var culturesPromise; var idleState = true; var fullscreenExited = false; self.initVideoPlayer = function () { video = playVideo(item, startPosition, user); enhancePlayer(); return video }; self.toggleFullscreen = function () { if (self.isFullScreen()) { if (document.cancelFullScreen) { document.cancelFullScreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitCancelFullScreen) { document.webkitCancelFullScreen(); } $('#videoPlayer').removeClass('fullscreenVideo'); } else { requestFullScreen(document.body); } }; self.resetEnhancements = function () { var footer = $("#footer"); var videoBackdrop = $("#videoBackdrop", footer); var nowPlayingBar = $("#nowPlayingBar", videoBackdrop); footer.append(nowPlayingBar); videoBackdrop.remove(); }; self.exitFullScreen = function () { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.mozExitFullScreen) { document.mozExitFullScreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } fullscreenExited = true; } self.isFullScreen = function () { return document.fullscreen || document.mozFullScreen || document.webkitIsFullScreen || document.msFullscreenElement ? true : false; } self.showSubtitleMenu = function () { var flyout = $('#subtitleFlyout'); if (!flyout.is(':visible')) { culturesPromise = culturesPromise || ApiClient.getCultures(); $("html").css("cursor", "progress"); culturesPromise.done(function (cultures) { $("html").css("cursor", "default"); flyout.html(getSubtitleTracksHtml(currentItem, cultures)).trigger('create').scrollTop(0); toggleFlyout(flyout, '#subtitleButton'); }); } else { toggleFlyout(flyout, '#subtitleButton'); } }; self.showQualityFlyout = function () { var flyout = $('#qualityFlyout'); if (!flyout.is(':visible')) { flyout.html(getQualityFlyoutHtml(currentItem)).scrollTop(0); } toggleFlyout(flyout, '#qualityButton'); }; self.showChaptersFlyout = function () { var flyout = $('#chaptersFlyout'); if (!flyout.is(':visible')) { flyout.html(getChaptersFlyoutHtml(currentItem)).scrollTop(0); } toggleFlyout(flyout, '#chaptersButton'); }; self.showAudioTracksFlyout = function () { var flyout = $('#audioTracksFlyout'); if (!flyout.is(':visible')) { culturesPromise = culturesPromise || ApiClient.getCultures(); $("html").css("cursor", "progress"); culturesPromise.done(function (cultures) { $("html").css("cursor", "default"); flyout.html(getAudioTracksHtml(currentItem, cultures)).trigger('create').scrollTop(0); toggleFlyout(flyout, '#audioTracksButton'); }); } else { toggleFlyout(flyout, '#audioTracksButton'); } }; $(document).on('webkitfullscreenchange mozfullscreenchange fullscreenchange', function () { var nowPlayingBar = $('#nowPlayingBar'); if (self.isFullScreen()) { enterFullScreen(); idleState = true; } else { nowPlayingBar.removeClass("highPosition"); exitFullScreenToWindow(); } }); $(function () { $('#chaptersFlyout').on('click', '.mediaFlyoutOption', function () { var ticks = parseInt(this.getAttribute('data-positionticks')); self.changeStream(ticks); hideFlyout($('#chaptersFlyout')); }); $('#audioTracksFlyout').on('click', '.mediaFlyoutOption', function () { if (!$(this).hasClass('selectedMediaFlyoutOption')) { var index = parseInt(this.getAttribute('data-index')); self.changeStream(self.getCurrentTicks(), { AudioStreamIndex: index }); } hideFlyout($('#audioTracksFlyout')); }); $('#subtitleFlyout').on('click', '.mediaFlyoutOption', function () { if (!$(this).hasClass('selectedMediaFlyoutOption')) { var index = parseInt(this.getAttribute('data-index')); self.changeStream(self.getCurrentTicks(), { SubtitleStreamIndex: index }); } hideFlyout($('#subtitleFlyout')); }); $('#qualityFlyout').on('click', '.mediaFlyoutOption', function () { if (!$(this).hasClass('selectedMediaFlyoutOption')) { var maxWidth = parseInt(this.getAttribute('data-maxwidth')); var bitrate = parseInt(this.getAttribute('data-bitrate')); localStorage.setItem('preferredVideoBitrate', bitrate); self.changeStream(self.getCurrentTicks(), { MaxWidth: maxWidth, Bitrate: bitrate }); } hideFlyout($('#qualityFlyout')); }); }); function requestFullScreen(element) { // Supports most browsers and their versions. var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen; if (requestMethod) { // Native full screen. requestMethod.call(element); } else { enterFullScreen(); } } function enhancePlayer() { // Show loading animation $(".ui-loader").show(); $("html").css("cursor", "wait"); var footer = $("#footer"); var nowPlayingBar = $("#nowPlayingBar", footer); var mediaElement = $("#mediaElement", footer); var play = $("
"); var pause = $(""); mediaElement.append(play).append(pause); var videoBackdrop = $(""); var videoPlayer = $("") .append(mediaElement) .append(nowPlayingBar); videoPlayer.hide(); videoBackdrop.append(videoPlayer); footer.append(videoBackdrop); // Stop playback on browser back button nav $(window).on("popstate", function () { self.stop(); return; }); $(video) .on("click", function (e) { if (this.paused) { self.unpause(); } else { self.pause(); } }) .on("dblclick", function () { self.toggleFullscreen(); }) .on("seeking", function (e) { $("html").css("cursor", "wait"); }) .on("seeked", function (e) { $("html").css("cursor", "default"); }) .on("loadstart", function () { $("html").css("cursor", "progress"); }) .on("playing", function (e) { $(".ui-loader").hide(); $("html").css("cursor", "default"); videoPlayer.fadeIn(); }); $(".mediaFlyoutContainer").on("click", "a", function (e) { if (confirm("This option will close the video player. Proceed?")) { self.stop(); } else { e.preventDefault(); } }); changeHandler("fullscreenchange"); changeHandler("mozfullscreenchange"); changeHandler("webkitfullscreenchange"); changeHandler("msfullscreenchange"); $(document).on("keyup.enhancePlayer", function (e) { if (fullscreenExited) { videoPlayer.removeClass("fullscreenVideo"); fullscreenExited = false; return; } if (e.keyCode == 27) { self.stop(); $(this).unbind("keyup.enhancePlayer"); } }); video.play(); fullscreenExited = false; }; function changeHandler(event) { document.addEventListener(event, function () { fullscreenExited = self.isFullScreen() == false; }); } function enterFullScreen() { var player = $("#videoPlayer") player.addClass("fullscreenVideo"); }; function exitFullScreenToWindow() { var player = $("#videoPlayer") player.removeClass("fullscreenVideo"); } function toggleFlyout(flyout, button) { $(document.body).off("mousedown.mediaflyout").on("mousedown.mediaflyout", function (e) { var elem = $(e.target); var flyoutId = flyout[0].id; var safeItems = button + ',#' + flyoutId; if (!elem.is(safeItems) && !elem.parents(safeItems).length) { hideFlyout(flyout); } }); var visible = $(flyout).is(":visible"); if (!visible) { flyout.slideDown(); } else { $(button).blur(); hideFlyout(flyout); } } function hideFlyout(flyout) { flyout.slideUp().empty(); $(document.body).off("mousedown.hidesearchhints"); } function getChaptersFlyoutHtml(item) { var html = ''; var currentTicks = self.getCurrentTicks(); var chapters = item.Chapters || []; for (var i = 0, length = chapters.length; i < length; i++) { var chapter = chapters[i]; var isSelected = false; if (currentTicks >= chapter.StartPositionTicks) { var nextChapter = chapters[i + 1]; isSelected = !nextChapter || currentTicks < nextChapter.StartPositionTicks; } if (isSelected) { html += '