(function (window, document, $, setTimeout, clearTimeout) { var currentPlayer; var currentTimeElement; var nowPlayingImageElement; var nowPlayingTextElement; var nowPlayingUserData; var unmuteButton; var muteButton; var volumeSlider; var unpauseButton; var pauseButton; var positionSlider; var lastPlayerState; function getNowPlayingBarHtml() { var html = ''; // add return false because on iOS clicking the bar often ends up clicking the content underneath. html += '
'; return html; } function bindEvents(elem) { currentTimeElement = $('.nowPlayingBarCurrentTime', elem); nowPlayingImageElement = $('.nowPlayingImage', elem); nowPlayingTextElement = $('.nowPlayingBarText', elem); nowPlayingUserData = $('.nowPlayingBarUserDataButtons', elem); $(elem).on('swipeup', function () { Dashboard.navigate('nowplaying.html'); }); unmuteButton = $('.unmuteButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.unMute(); } }); muteButton = $('.muteButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.mute(); } }); $('.stopButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.stop(); } }); pauseButton = $('.pauseButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.pause(); } }); unpauseButton = $('.unpauseButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.unpause(); } }); $('.nextTrackButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.nextTrack(); } }); $('.previousTrackButton', elem).on('click', function () { if (currentPlayer) { currentPlayer.previousTrack(); } }); volumeSlider = $('.nowPlayingBarVolumeSlider', elem).on('change', function () { if (currentPlayer) { currentPlayer.setVolume(this.value); } })[0]; positionSlider = $('.nowPlayingBarPositionSlider', elem).on('change', function () { if (currentPlayer && lastPlayerState) { var newPercent = parseFloat(this.value); var newPositionTicks = (newPercent / 100) * lastPlayerState.NowPlayingItem.RunTimeTicks; currentPlayer.seek(Math.floor(newPositionTicks)); } })[0]; positionSlider._setPinValue = function (value) { var state = lastPlayerState; if (!state || !state.NowPlayingItem || !state.NowPlayingItem.RunTimeTicks) { this.pinValue = '--:--'; return; } var ticks = state.NowPlayingItem.RunTimeTicks; ticks /= 100; ticks *= value; this.pinValue = Dashboard.getDisplayTime(ticks); }; } function getNowPlayingBar() { var elem = $('.nowPlayingBar'); if (elem.length) { return elem; } elem = $(getNowPlayingBarHtml()).insertBefore('#footerNotifications').trigger('create'); bindEvents(elem); return elem; } function showButton(button) { button.removeClass('hide'); } function hideButton(button) { button.addClass('hide'); } function updatePlayerState(state) { if (state.NowPlayingItem && !$($.mobile.activePage).hasClass('nowPlayingPage')) { showNowPlayingBar(); } else { hideNowPlayingBar(); return; } lastPlayerState = state; if (!muteButton) { getNowPlayingBar(); } var playerInfo = MediaController.getPlayerInfo(); var playState = state.PlayState || {}; if (playState.IsPaused) { hideButton(pauseButton); showButton(unpauseButton); } else { showButton(pauseButton); hideButton(unpauseButton); } updatePlayerVolumeState(state, playerInfo); var nowPlayingItem = state.NowPlayingItem || {}; if (!positionSlider.dragging) { if (nowPlayingItem && nowPlayingItem.RunTimeTicks) { var pct = playState.PositionTicks / nowPlayingItem.RunTimeTicks; pct *= 100; positionSlider.value = pct; } else { positionSlider.value = 0; } positionSlider.disabled = !playState.CanSeek; } var timeText = Dashboard.getDisplayTime(playState.PositionTicks); if (nowPlayingItem.RunTimeTicks) { timeText += " / " + Dashboard.getDisplayTime(nowPlayingItem.RunTimeTicks); } currentTimeElement.html(timeText); updateNowPlayingInfo(state); } function updatePlayerVolumeState(state, playerInfo) { playerInfo = playerInfo || MediaController.getPlayerInfo(); if (!muteButton) { getNowPlayingBar(); } var playState = state.PlayState || {}; var supportedCommands = playerInfo.supportedCommands; var showMuteButton = true; var showUnmuteButton = true; var showVolumeSlider = true; if (supportedCommands.indexOf('Mute') == -1) { showMuteButton = false; } if (supportedCommands.indexOf('Unmute') == -1) { showUnmuteButton = false; } if (playState.IsMuted) { showMuteButton = false; } else { showUnmuteButton = false; } if (supportedCommands.indexOf('SetVolume') == -1) { showVolumeSlider = false; } if (playerInfo.isLocalPlayer && AppInfo.hasPhysicalVolumeButtons) { showMuteButton = false; showUnmuteButton = false; showVolumeSlider = false; } if (showMuteButton) { showButton(muteButton); } else { hideButton(muteButton); } if (showUnmuteButton) { showButton(unmuteButton); } else { hideButton(unmuteButton); } $(volumeSlider).visible(showVolumeSlider); if (!volumeSlider.dragging) { volumeSlider.value = playState.VolumeLevel || 0; } } var currentImgUrl; function updateNowPlayingInfo(state) { var nameHtml = MediaController.getNowPlayingNameHtml(state.NowPlayingItem) || ''; if (nameHtml.indexOf('