(function ($, window, document) {
var currentItem;
function updateTabs(page, item) {
var query = MetadataEditor.getEditQueryString(item);
$('#btnEditMetadata', page).attr('href', 'edititemmetadata.html?' + query);
$('#btnEditImages', page).attr('href', 'edititemimages.html?' + query);
}
function showLocalSubtitles(page, index) {
Dashboard.showLoadingMsg();
var popup = $('.popupSubtitleViewer', page).popup('open');
$('.subtitleContent', page).html('');
var url = 'Videos/' + currentItem.Id + '/Subtitles/' + index;
$.get(ApiClient.getUrl(url)).done(function (result) {
$('.subtitleContent', page).html(result);
Dashboard.hideLoadingMsg();
popup.popup('reposition', {});
});
}
function showRemoteSubtitles(page, id) {
Dashboard.showLoadingMsg();
var popup = $('.popupSubtitleViewer', page).popup('open');
$('.subtitleContent', page).html('\nLoading...\n\n\n');
var url = 'Providers/Subtitles/Subtitles/' + id;
$.get(ApiClient.getUrl(url)).done(function (result) {
$('.subtitleContent', page).html(result);
Dashboard.hideLoadingMsg();
popup.popup('reposition', {});
});
}
function downloadRemoteSubtitles(page, id) {
var url = 'Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + id;
$.ajax({
type: "POST",
url: ApiClient.getUrl(url)
}).done(function () {
Dashboard.alert('The download has been queued.');
});
}
function deleteLocalSubtitle(page, index) {
var msg = "Are you sure you wish to delete this subtitle file?";
Dashboard.confirm(msg, "Confirm Deletion", function (result) {
if (result) {
Dashboard.showLoadingMsg();
var url = 'Videos/' + currentItem.Id + '/Subtitles/' + index;
$.ajax({
type: "DELETE",
url: ApiClient.getUrl(url)
}).done(function () {
reload(page);
});
}
});
}
function fillSubtitleList(page, item) {
var html = '
';
var elem = $('.subtitleList', page).html(html).trigger('create');
$('.btnViewSubtitles', elem).on('click', function () {
var index = this.getAttribute('data-index');
showLocalSubtitles(page, index);
});
$('.btnDelete', elem).on('click', function () {
var index = this.getAttribute('data-index');
deleteLocalSubtitle(page, index);
});
}
function fillLanguages(page, languages) {
$('#selectLanguage', page).html(languages.map(function (l) {
return '' + l.DisplayName + ' ';
})).selectmenu('refresh');
Dashboard.getCurrentUser().done(function (user) {
var lang = user.Configuration.SubtitleLanguagePreference;
if (lang) {
$('#selectLanguage', page).val(lang).selectmenu('refresh');
}
});
}
function renderSearchResults(page, results) {
var lastProvider = '';
var html = '';
if (!results.length) {
$('.noSearchResults', page).show();
$('.subtitleResults', page).html('');
Dashboard.hideLoadingMsg();
return;
}
$('.noSearchResults', page).hide();
html += '';
var elem = $('.subtitleResults', page).html(html).trigger('create');
$('.btnViewSubtitle', elem).on('click', function () {
var id = this.getAttribute('data-subid');
showRemoteSubtitles(page, id);
});
$('.btnDownload', elem).on('click', function () {
var id = this.getAttribute('data-subid');
downloadRemoteSubtitles(page, id);
});
Dashboard.hideLoadingMsg();
}
function searchForSubtitles(page, language) {
Dashboard.showLoadingMsg();
var url = ApiClient.getUrl('Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + language);
$.getJSON(url).done(function (results) {
renderSearchResults(page, results);
});
}
function reload(page) {
$('.noSearchResults', page).hide();
MetadataEditor.getItemPromise().done(function (item) {
currentItem = item;
LibraryBrowser.renderName(item, $('.itemName', page), true);
updateTabs(page, item);
fillSubtitleList(page, item);
Dashboard.hideLoadingMsg();
});
}
function onWebSocketMessageReceived(e, data) {
var msg = data;
if (msg.MessageType === "LibraryChanged") {
if (msg.Data.ItemsUpdated.indexOf(currentItem.Id) != -1) {
console.log('Item updated - reloading subtitles');
reload($.mobile.activePage);
}
}
}
$(document).on('pageinit', "#editItemSubtitlesPage", function () {
var page = this;
$('.libraryTree', page).on('itemclicked', function (event, data) {
if (data.itemType == "livetvservice") {
return;
}
if (data.id != currentItem.Id) {
MetadataEditor.currentItemId = data.id;
MetadataEditor.currentItemName = data.itemName;
MetadataEditor.currentItemType = data.itemType;
//Dashboard.navigate('edititemmetadata.html?id=' + data.id);
//$.mobile.urlHistory.ignoreNextHashChange = true;
window.location.hash = 'editItemSubtitlesPage?id=' + data.id;
reload(page);
}
});
}).on('pagebeforeshow', "#editItemSubtitlesPage", function () {
var page = this;
$('.subtitleResults', page).empty();
Dashboard.showLoadingMsg();
reload(page);
ApiClient.getCultures().done(function (languages) {
fillLanguages(page, languages);
});
$(ApiClient).on("websocketmessage", onWebSocketMessageReceived);
}).on('pagehide', "#editItemSubtitlesPage", function () {
var page = this;
currentItem = null;
$(ApiClient).off("websocketmessage", onWebSocketMessageReceived);
});
window.EditItemSubtitlesPage = {
onSearchSubmit: function () {
var form = this;
var lang = $('#selectLanguage', form).val();
searchForSubtitles($(form).parents('.page'), lang);
return false;
}
};
})(jQuery, window, document);