define(['layoutManager', 'datetime', 'jQuery', 'mediaInfo', 'scrollStyles'], function (layoutManager, datetime, $, mediaInfo) {
var currentItem;
function getPromise() {
var id = getParameterByName('id');
if (id) {
return ApiClient.getItem(Dashboard.getCurrentUserId(), id);
}
var name = getParameterByName('genre');
if (name) {
return ApiClient.getGenre(name, Dashboard.getCurrentUserId());
}
name = getParameterByName('musicgenre');
if (name) {
return ApiClient.getMusicGenre(name, Dashboard.getCurrentUserId());
}
name = getParameterByName('gamegenre');
if (name) {
return ApiClient.getGameGenre(name, Dashboard.getCurrentUserId());
}
name = getParameterByName('musicartist');
if (name) {
return ApiClient.getArtist(name, Dashboard.getCurrentUserId());
}
else {
throw new Error('Invalid request');
}
}
function reload(page) {
Dashboard.showLoadingMsg();
getPromise().then(function (item) {
reloadFromItem(page, item);
});
}
function reloadFromItem(page, item) {
currentItem = item;
var context = getContext(item);
LibraryMenu.setBackButtonVisible(true);
LibraryMenu.setMenuButtonVisible(false);
LibraryBrowser.renderName(item, $('.itemName', page), false, context);
LibraryBrowser.renderParentName(item, $('.parentName', page), context);
LibraryMenu.setTitle(item.SeriesName || item.Name);
Dashboard.getCurrentUser().then(function (user) {
renderImage(page, item, user);
setInitialCollapsibleState(page, item, context, user);
renderDetails(page, item, context);
var hasBackdrop = false;
// For these types, make the backdrop a little smaller so that the items are more quickly accessible
if (item.Type == 'MusicArtist' || item.Type == "MusicAlbum" || item.Type == "Playlist" || item.Type == "BoxSet" || item.MediaType == "Audio" || !layoutManager.mobile) {
$('#itemBackdrop', page).addClass('noBackdrop').css('background-image', 'none');
require(['backdrop'], function (backdrop) {
backdrop.setBackdrops([item]);
});
}
else {
//$('#itemBackdrop', page).addClass('noBackdrop').css('background-image', 'none');
//Backdrops.setBackdrops(page, [item]);
hasBackdrop = LibraryBrowser.renderDetailPageBackdrop(page, item);
}
var transparentHeader = hasBackdrop && page.classList.contains('noSecondaryNavPage');
LibraryMenu.setTransparentMenu(transparentHeader);
var canPlay = false;
if (item.Type == 'Program') {
var now = new Date();
if (now >= datetime.parseISO8601Date(item.StartDate, true) && now < datetime.parseISO8601Date(item.EndDate, true)) {
$('.btnPlay', page).removeClass('hide');
canPlay = true;
} else {
$('.btnPlay', page).addClass('hide');
}
}
else if (MediaController.canPlay(item)) {
$('.btnPlay', page).removeClass('hide');
canPlay = true;
}
else {
$('.btnPlay', page).addClass('hide');
}
if (item.LocalTrailerCount && item.PlayAccess == 'Full') {
$('.btnPlayTrailer', page).removeClass('hide');
} else {
$('.btnPlayTrailer', page).addClass('hide');
}
if (LibraryBrowser.enableSync(item, user)) {
$('.btnSync', page).removeClass('hide');
} else {
$('.btnSync', page).addClass('hide');
}
if (item.Type == 'Program' && item.TimerId) {
$('.btnCancelRecording', page).removeClass('hide');
} else {
$('.btnCancelRecording', page).addClass('hide');
}
if (item.Type == 'Program' && (!item.TimerId && !item.SeriesTimerId)) {
if (canPlay) {
$('.btnRecord', page).removeClass('hide');
$('.btnFloatingRecord', page).addClass('hide');
} else {
$('.btnRecord', page).addClass('hide');
$('.btnFloatingRecord', page).removeClass('hide');
}
} else {
$('.btnRecord', page).addClass('hide');
$('.btnFloatingRecord', page).addClass('hide');
}
if (!item.LocalTrailerCount && item.RemoteTrailers.length && item.PlayAccess == 'Full') {
$('.btnPlayExternalTrailer', page).removeClass('hide').attr('href', item.RemoteTrailers[0].Url);
} else {
$('.btnPlayExternalTrailer', page).addClass('hide').attr('href', '#');
}
var groupedVersions = (item.MediaSources || []).filter(function (g) {
return g.Type == "Grouping";
});
if (user.Policy.IsAdministrator && groupedVersions.length) {
$('.splitVersionContainer', page).show();
} else {
$('.splitVersionContainer', page).hide();
}
if (LibraryBrowser.getMoreCommands(item, user).length > 0) {
$('.btnMoreCommands', page).removeClass('hide');
} else {
$('.btnMoreCommands', page).addClass('hide');
}
if (user.Policy.IsAdministrator) {
$('.chapterSettingsButton', page).show();
} else {
$('.chapterSettingsButton', page).hide();
}
if (item.Type == "Person" && item.PremiereDate) {
try {
var birthday = datetime.parseISO8601Date(item.PremiereDate, true).toDateString();
$('#itemBirthday', page).show().html(Globalize.translate('BirthDateValue').replace('{0}', birthday));
}
catch (err) {
$('#itemBirthday', page).hide();
}
} else {
$('#itemBirthday', page).hide();
}
if (item.Type == "Person" && item.EndDate) {
try {
var deathday = datetime.parseISO8601Date(item.EndDate, true).toDateString();
$('#itemDeathDate', page).show().html(Globalize.translate('DeathDateValue').replace('{0}', deathday));
}
catch (err) {
$('#itemBirthday', page).hide();
}
} else {
}
if (item.Type == "Person" && item.ProductionLocations && item.ProductionLocations.length) {
var gmap = '' + item.ProductionLocations[0] + '';
$('#itemBirthLocation', page).show().html(Globalize.translate('BirthPlaceValue').replace('{0}', gmap));
} else {
$('#itemBirthLocation', page).hide();
}
});
if (item.LocationType == "Offline") {
$('.offlineIndicator', page).show();
}
else {
$('.offlineIndicator', page).hide();
}
var isMissingEpisode = false;
if (item.LocationType == "Virtual" && item.Type == "Episode") {
try {
if (item.PremiereDate && (new Date().getTime() >= datetime.parseISO8601Date(item.PremiereDate, true).getTime())) {
isMissingEpisode = true;
}
} catch (err) {
}
}
if (isMissingEpisode) {
$('.missingIndicator', page).show();
}
else {
$('.missingIndicator', page).hide();
}
setPeopleHeader(page, item);
page.dispatchEvent(new CustomEvent("displayingitem", {
detail: {
item: item,
context: context
},
bubbles: true
}));
Dashboard.hideLoadingMsg();
}
function renderImage(page, item, user) {
LibraryBrowser.renderDetailImage(page.querySelector('.detailImageContainer'), item, user.Policy.IsAdministrator && item.MediaType != 'Photo');
}
function refreshImage(page, item, user) {
LibraryBrowser.refreshDetailImageUserData(page.querySelector('.detailImageContainer'), item);
}
function onWebSocketMessage(e, data) {
var msg = data;
var page = $($.mobile.activePage)[0];
if (msg.MessageType === "UserDataChanged") {
if (currentItem && msg.Data.UserId == Dashboard.getCurrentUserId()) {
var key = currentItem.UserData.Key;
var userData = msg.Data.UserDataList.filter(function (u) {
return u.Key == key;
})[0];
if (userData) {
currentItem.UserData = userData;
Dashboard.getCurrentUser().then(function (user) {
refreshImage(page, currentItem, user);
});
}
}
}
}
function setPeopleHeader(page, item) {
if (item.MediaType == "Audio" || item.Type == "MusicAlbum" || item.MediaType == "Book" || item.MediaType == "Photo") {
$('#peopleHeader', page).html(Globalize.translate('HeaderPeople'));
} else {
$('#peopleHeader', page).html(Globalize.translate('HeaderCastAndCrew'));
}
}
function getContext(item) {
return getParameterByName('context');
}
function renderNextUp(page, item, user) {
var section = page.querySelector('.nextUpSection');
var userData = item.UserData || {};
if (item.Type != 'Series' || !userData.PlayedPercentage) {
section.classList.add('hide');
return;
}
ApiClient.getNextUpEpisodes({
SeriesId: item.Id,
UserId: user.Id
}).then(function (result) {
if (result.Items.length) {
section.classList.remove('hide');
} else {
section.classList.add('hide');
}
var html = LibraryBrowser.getPosterViewHtml({
items: result.Items,
shape: "detailPage169",
showTitle: true,
displayAsSpecial: item.Type == "Season" && item.IndexNumber,
overlayText: true,
lazy: true,
overlayPlayButton: true
});
var itemsContainer = section.querySelector('.nextUpItems');
itemsContainer.innerHTML = html;
ImageLoader.lazyChildren(itemsContainer);
$(itemsContainer).createCardMenus();
});
}
function setInitialCollapsibleState(page, item, context, user) {
$('.collectionItems', page).empty();
if (item.Type == 'TvChannel') {
$('#childrenCollapsible', page).removeClass('hide');
renderChannelGuide(page, item, user);
}
else if (item.Type == 'Playlist') {
$('#childrenCollapsible', page).removeClass('hide');
renderPlaylistItems(page, item, user);
}
else if (item.Type == 'Studio' || item.Type == 'Person' || item.Type == 'Genre' || item.Type == 'MusicGenre' || item.Type == 'GameGenre' || item.Type == 'MusicArtist') {
$('#childrenCollapsible', page).removeClass('hide');
renderItemsByName(page, item, user);
}
else if (item.IsFolder) {
if (item.Type == "BoxSet") {
$('#childrenCollapsible', page).addClass('hide');
} else {
$('#childrenCollapsible', page).removeClass('hide');
}
renderChildren(page, item, user, context);
}
else {
$('#childrenCollapsible', page).addClass('hide');
}
if (item.Type == 'Series') {
renderNextUp(page, item, user);
} else {
page.querySelector('.nextUpSection').classList.add('hide');
}
if (item.MediaSources && item.MediaSources.length) {
renderMediaSources(page, item);
}
var chapters = item.Chapters || [];
if (!chapters.length || !AppInfo.enableDetailPageChapters) {
$('#scenesCollapsible', page).hide();
} else {
$('#scenesCollapsible', page).show();
renderScenes(page, item, user, 3);
}
if (!item.SpecialFeatureCount || item.SpecialFeatureCount == 0 || item.Type == "Series") {
$('#specialsCollapsible', page).addClass('hide');
} else {
$('#specialsCollapsible', page).removeClass('hide');
renderSpecials(page, item, user, 6);
}
if (!item.People || !item.People.length) {
$('#castCollapsible', page).hide();
} else {
$('#castCollapsible', page).show();
renderCast(page, item, context, enableScrollX() ? null : 6);
}
if (item.PartCount && item.PartCount > 1) {
$('#additionalPartsCollapsible', page).removeClass('hide');
renderAdditionalParts(page, item, user);
} else {
$('#additionalPartsCollapsible', page).addClass('hide');
}
$('#themeSongsCollapsible', page).hide();
$('#themeVideosCollapsible', page).hide();
if (item.Type == "MusicAlbum") {
renderMusicVideos(page, item, user);
} else {
$('#musicVideosCollapsible', page).hide();
}
renderThemeMedia(page, item, user);
if (enableScrollX()) {
renderCriticReviews(page, item);
} else {
renderCriticReviews(page, item, 1);
}
}
function renderDetails(page, item, context, isStatic) {
renderSimilarItems(page, item, context);
if (!isStatic) {
renderSiblingLinks(page, item, context);
}
if (item.Taglines && item.Taglines.length) {
$('.tagline', page).html(item.Taglines[0]).show();
} else {
$('.tagline', page).hide();
}
var topOverview = page.querySelector('.topOverview');
var bottomOverview = page.querySelector('.bottomOverview');
var seasonOnBottom = screen.availHeight < 800 || screen.availWidth < 600;
if (item.Type == 'MusicAlbum' || item.Type == 'MusicArtist' || (item.Type == 'Season' && seasonOnBottom)) {
LibraryBrowser.renderOverview([bottomOverview], item);
topOverview.classList.add('hide');
bottomOverview.classList.remove('hide');
} else {
LibraryBrowser.renderOverview([topOverview], item);
topOverview.classList.remove('hide');
bottomOverview.classList.add('hide');
}
LibraryBrowser.renderAwardSummary($('#awardSummary', page), item);
$('.itemMiscInfo', page).each(function () {
mediaInfo.fillPrimaryMediaInfo(this, item, {
interactive: true
});
});
$('.itemGenres', page).each(function() {
LibraryBrowser.renderGenres(this, item, null, isStatic);
});
LibraryBrowser.renderStudios($('.itemStudios', page), item, isStatic);
renderUserDataIcons(page, item);
LibraryBrowser.renderLinks(page.querySelector('.itemExternalLinks'), item);
$('.criticRatingScore', page).html((item.CriticRating || '0') + '%');
if (item.CriticRatingSummary) {
$('#criticRatingSummary', page).show();
$('.criticRatingSummaryText', page).html(item.CriticRatingSummary);
} else {
$('#criticRatingSummary', page).hide();
}
renderTags(page, item);
renderSeriesAirTime(page, item, isStatic);
if (item.Players) {
$('#players', page).show().html(item.Players + ' Player');
} else {
$('#players', page).hide();
}
if (item.ArtistItems && item.ArtistItems.length && item.Type != "MusicAlbum") {
$('.artist', page).show().html(getArtistLinksHtml(item.ArtistItems, context));
} else {
$('.artist', page).hide();
}
if (item.MediaSources && item.MediaSources.length && item.Path) {
$('.audioVideoMediaInfo', page).removeClass('hide');
} else {
$('.audioVideoMediaInfo', page).addClass('hide');
}
if (item.MediaType == 'Photo') {
$('.photoInfo', page).removeClass('hide');
renderPhotoInfo(page, item);
} else {
$('.photoInfo', page).addClass('hide');
}
renderTabButtons(page, item);
}
function renderPhotoInfo(page, item) {
var html = '';
var attributes = [];
if (item.CameraMake) {
attributes.push(createAttribute(Globalize.translate('MediaInfoCameraMake'), item.CameraMake));
}
if (item.CameraModel) {
attributes.push(createAttribute(Globalize.translate('MediaInfoCameraModel'), item.CameraModel));
}
if (item.Altitude) {
attributes.push(createAttribute(Globalize.translate('MediaInfoAltitude'), item.Altitude.toFixed(1)));
}
if (item.Aperture) {
attributes.push(createAttribute(Globalize.translate('MediaInfoAperture'), 'F' + item.Aperture.toFixed(1)));
}
if (item.ExposureTime) {
var val = 1 / item.ExposureTime;
attributes.push(createAttribute(Globalize.translate('MediaInfoExposureTime'), '1/' + val + ' s'));
}
if (item.FocalLength) {
attributes.push(createAttribute(Globalize.translate('MediaInfoFocalLength'), item.FocalLength.toFixed(1) + ' mm'));
}
if (item.ImageOrientation) {
//attributes.push(createAttribute(Globalize.translate('MediaInfoOrientation'), item.ImageOrientation));
}
if (item.IsoSpeedRating) {
attributes.push(createAttribute(Globalize.translate('MediaInfoIsoSpeedRating'), item.IsoSpeedRating));
}
if (item.Latitude) {
attributes.push(createAttribute(Globalize.translate('MediaInfoLatitude'), item.Latitude.toFixed(1)));
}
if (item.Longitude) {
attributes.push(createAttribute(Globalize.translate('MediaInfoLongitude'), item.Longitude.toFixed(1)));
}
if (item.ShutterSpeed) {
attributes.push(createAttribute(Globalize.translate('MediaInfoShutterSpeed'), item.ShutterSpeed));
}
if (item.Software) {
attributes.push(createAttribute(Globalize.translate('MediaInfoSoftware'), item.Software));
}
html += attributes.join('
');
$('.photoInfoContent', page).html(html);
}
function renderTabButtons(page, item) {
var elem = $('.tabDetails', page)[0];
var text = elem.textContent || elem.innerText || '';
if (text.trim()) {
$('.detailsSection', page).removeClass('hide');
} else {
$('.detailsSection', page).addClass('hide');
}
}
function getArtistLinksHtml(artists, context) {
var html = [];
for (var i = 0, length = artists.length; i < length; i++) {
var artist = artists[i];
html.push('' + artist.Name + '');
}
html = html.join(' / ');
if (artists.length == 1) {
return Globalize.translate('ValueArtist', html);
}
if (artists.length > 1) {
return Globalize.translate('ValueArtists', html);
}
return html;
}
function renderSiblingLinks(page, item, context) {
$('.lnkSibling', page).addClass('hide');
if ((item.Type != "Episode" && item.Type != "Season" && item.Type != "Audio" && item.Type != "Photo")) {
return;
}
var promise;
if (item.Type == "Season") {
promise = ApiClient.getSeasons(item.SeriesId, {
userId: Dashboard.getCurrentUserId(),
AdjacentTo: item.Id
});
}
else if (item.Type == "Episode" && item.SeasonId) {
// Use dedicated episodes endpoint
promise = ApiClient.getEpisodes(item.SeriesId, {
seasonId: item.SeasonId,
userId: Dashboard.getCurrentUserId(),
AdjacentTo: item.Id
});
} else {
promise = ApiClient.getItems(Dashboard.getCurrentUserId(), {
AdjacentTo: item.Id,
ParentId: item.ParentId,
SortBy: 'SortName'
});
}
context = context || '';
promise.then(function (result) {
var foundExisting = false;
for (var i = 0, length = result.Items.length; i < length; i++) {
var curr = result.Items[i];
if (curr.Id == item.Id) {
foundExisting = true;
}
else if (!foundExisting) {
$('.lnkPreviousItem', page).removeClass('hide').attr('href', 'itemdetails.html?id=' + curr.Id + '&context=' + context);
}
else {
$('.lnkNextItem', page).removeClass('hide').attr('href', 'itemdetails.html?id=' + curr.Id + '&context=' + context);
}
}
});
}
function enableScrollX() {
return browserInfo.mobile && AppInfo.enableAppLayouts && screen.availWidth <= 1000;
}
function getPortraitShape() {
return enableScrollX() ? 'overflowPortrait' : 'detailPagePortrait';
}
function getSquareShape() {
return enableScrollX() ? 'overflowSquare' : 'detailPageSquare';
}
function getThumbShape() {
return enableScrollX() ? 'overflowBackdrop' : 'detailPage169';
}
function renderSimilarItems(page, item, context) {
if (item.Type == "Movie" || item.Type == "Trailer" || item.Type == "Series" || item.Type == "Program" || item.Type == "Recording" || item.Type == "Game" || item.Type == "MusicAlbum" || item.Type == "MusicArtist" || item.Type == "ChannelVideoItem") {
$('#similarCollapsible', page).show();
}
else {
$('#similarCollapsible', page).hide();
return;
}
var shape = item.Type == "MusicAlbum" || item.Type == "MusicArtist" ? getSquareShape() : getPortraitShape();
var screenWidth = $(window).width();
var screenHeight = $(window).height();
var options = {
userId: Dashboard.getCurrentUserId(),
limit: screenWidth > 800 && shape == "detailPagePortrait" ? 4 : 4,
fields: "PrimaryImageAspectRatio,UserData,SyncInfo,CanDelete"
};
if (screenWidth >= 800 && screenHeight >= 1000) {
options.limit *= 2;
}
if (enableScrollX()) {
options.limit = 12;
}
ApiClient.getSimilarItems(item.Id, options).then(function (result) {
if (!result.Items.length) {
$('#similarCollapsible', page).hide();
return;
}
var elem = $('#similarCollapsible', page).show();
$('.similiarHeader', elem).html(Globalize.translate('HeaderIfYouLikeCheckTheseOut', item.Name));
var html = '';
if (enableScrollX()) {
html += '
' + Globalize.translate('HeaderTags') + '
'; for (var i = 0, length = item.Tags.length; i < length; i++) { html += '' + cast.Name + '
'; var role = cast.Role ? Globalize.translate('ValueAsRole', cast.Role) : cast.Type; if (role == "GuestStar") { role = Globalize.translate('ValueGuestStar'); } role = role || ""; var maxlength = 40; if (role.length > maxlength) { role = role.substring(0, maxlength - 3) + '...'; } html += '' + role + '
'; html += '' + cast.Name + '
'; //var role = cast.Role ? Globalize.translate('ValueAsRole', cast.Role) : cast.Type; //if (role == "GuestStar") { // role = Globalize.translate('ValueGuestStar'); //} //role = role || ""; //var maxlength = 40; //if (role.length > maxlength) { // role = role.substring(0, maxlength - 3) + '...'; //} //html += '' + role + '
'; //html += '