function notificationAttachEvents(){$('.notification-view').off('click.notification').on('click.notification',function(event){var elem=$(this);if(event.ctrlKey||event.shiftKey||event.metaKey||(event.button&&event.button==1)){$('#notification-'+elem.data('notification')+' .notification-read').removeClass('hidden');$('#notification-'+elem.data('notification')+' .notification-unread').addClass('hidden');$('#notification-'+elem.data('notification')+' .notification-toggle-read').addClass('unread');$('#notification-'+elem.data('notification')+' .notification-toggle-read .fa-envelope').addClass('hidden');$('#notification-'+elem.data('notification')+' .notification-toggle-read .fa-envelope-open').removeClass('hidden');}else event.preventDefault();if(!elem.hasClass('disabled')){elem.addClass('disabled');$.post({url:'/notification/view/',data:{'notification':elem.data('notification'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){if(!(event.ctrlKey||event.shiftKey||event.metaKey||(event.button&&event.button==1))){if(useSPA){window.history.replaceState({'url':window.location.href},'',window.location.href);spaChangePage(result.url,true);}else window.location.href=result.url;}
$('#menu-bar-notifications').html(result.html);$('#notifylink').tooltip({container:'body',trigger:'hover'});}else{toastr.error('Failed to view notification');}},complete:function(){elem.removeClass('disabled');}});}});$('.notification-toggle-read').off('click.notification').on('click.notification',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled');var unread=elem.hasClass('unread');elem.toggleClass('unread');elem.children().toggleClass('hidden');$('#notification-'+elem.data('notification')+' .notification-read').toggleClass('hidden');$('#notification-'+elem.data('notification')+' .notification-unread').toggleClass('hidden');$.post({url:'/notification/read/',data:{'notification':elem.data('notification'),'unread':unread,'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){if(unread){toastr.success('Notification marked unread');elem.attr('title','Mark read').tooltip('fixTitle').tooltip('hide');}else{toastr.success('Notification marked read');elem.attr('title','Mark unread').tooltip('fixTitle').tooltip('hide');}
$('#menu-bar-notifications').html(result.html);$('#notifylink').tooltip({container:'body',trigger:'hover'});}else{elem.toggleClass('unread');elem.children().toggleClass('hidden');$('#notification-'+elem.data('notification')+' .notification-read').toggleClass('hidden');$('#notification-'+elem.data('notification')+' .notification-unread').toggleClass('hidden');toastr.error('Failed to change notification read state');}},complete:function(){elem.removeClass('disabled');}});}});$('.notification-delete').off('click.notification').on('click.notification',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled').tooltip('destroy');$.post({url:'/notification/clear/',data:{'notification':elem.data('notification'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){toastr.success('Notification deleted');$('#notification-'+elem.data('notification')).css({opacity:0,transition:'opacity 0.25s'}).slideUp();setTimeout(function(){$('#notification-'+elem.data('notification')).remove();if(!$('.show-more').hasClass('hidden')){if($('.notification-list').children('.notification-item').length<10)extend_list.get();}else{if($('.notification-list').children('.notification-item').length<1){$('.show-more-container').append('<p>No notifications found.</p>');}}},500);$('#menu-bar-notifications').html(result.html);$('#notifylink').tooltip({container:'body',trigger:'hover'});}else{toastr.error('Failed to delete notification');}},complete:function(){elem.removeClass('disabled');}});}});$('.notification-readall').off('click.notification').on('click.notification',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled');$.post({url:'/notification/readall/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){toastr.success('All notifications marked read');$('#menu-bar-notifications').html(result.html);$('#notifylink').tooltip({container:'body',trigger:'hover'});$('.notification-list').children('.notification-item').each(function(){$(this).find('.notification-read').removeClass('hidden');$(this).find('.notification-unread').addClass('hidden');$(this).find('.notification-toggle-read').not('.unread').each(function(){$(this).addClass('unread');$(this).attr('title','Mark unread').tooltip('fixTitle').tooltip('hide');$(this).children().toggleClass('hidden');});});}else{toastr.error('Failed to mark all notifications read');}},complete:function(){elem.removeClass('disabled');}});}});$('.notification-clearall').off('click.notification').on('click.notification',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled').tooltip('destroy');$.post({url:'/notification/clearall/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){toastr.success('All notifications deleted');$('#menu-bar-notifications').html(result.html);$('#notifylink').tooltip({container:'body',trigger:'hover'});$('.show-more').addClass('hidden');$('.show-more-container').append('<p>No notifications found.</p>');$('.notification-list').children('.notification-item').css({opacity:0,transition:'opacity 0.25s'}).slideUp();setTimeout(function(){$('.notification-list').children('.notification-item').remove();},500);}else{toastr.error('Failed to delete all notifications');}},complete:function(){elem.removeClass('disabled');}});}});}
var notificationRefreshed=false;function notificationRefresh(repeat){if(!notificationRefreshed||document.visibilityState=='visible'){$.post({url:'/notifications/refresh/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){$('#notifylink').tooltip('destroy');$('#menu-bar-notifications').html(result.html);$('#notifylink').tooltip({container:'body',trigger:'hover'});notificationRefreshed=true;}},complete:function(){if(repeat)setTimeout(notificationRefresh,300000,repeat);}});}else{if(repeat)setTimeout(notificationRefresh,300000,repeat);}}
function notificationExtend(extend){if(!$('.show-more').hasClass('hidden')){$('.show-more').addClass('hidden');var count=$('.notification-list').children('.notification-item').length;$.post(window.location.href+'extend/',{'csrfmiddlewaretoken':Cookies.get('csrftoken'),'offset':count},function(result){if(result.success){$('.notification-list').append(result.html);spaAttachEvents();notificationAttachEvents();playlistAttachEvents();$('[data-toggle="tooltip"]').tooltip({container:'body',trigger:'hover'});if($('.notification-list').children('.notification-item').length==count+extend)$('.show-more').removeClass('hidden');}});}}