function playlistAttachEvents(){$('.playlist-play').off('click.playlist').on('click.playlist',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled');$.post({url:'/playlist/play/',data:{'playlist':elem.data('playlist'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){if(useSPA){window.history.replaceState({'url':window.location.href},'',window.location.href);spaChangePage(result.url,true);}else{window.location.href=result.url;}}else{toastr.error('Failed to watch playlist');}},complete:function(){elem.removeClass('disabled');}});}});$('.playlist-randomize').off('click.playlist').on('click.playlist',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled');$.post({url:'/playlist/randomize/',data:{'playlist':elem.data('playlist'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){if(useSPA){window.history.replaceState({'url':window.location.href},'',window.location.href);spaChangePage(result.url,true);}else{window.location.href=result.url;}}else{toastr.error('Failed to randomize playlist');}},complete:function(){elem.removeClass('disabled');}});}});$('.playlist-toggle-visibility').off('click.playlist').on('click.playlist',function(){var elem=$(this);if(elem.find('span:first').hasClass('hidden')){showConfirmModal('make this playlist public',elem.data('title'),playlistToggleVisibility,[elem.data('playlist'),Cookies.get('csrftoken')]);}else{showConfirmModal('make this playlist private',elem.data('title'),playlistToggleVisibility,[elem.data('playlist'),Cookies.get('csrftoken')]);}});$('.playlist-delete').off('click.playlist').on('click.playlist',function(){var elem=$(this);showConfirmModal('delete this playlist',elem.data('title'),playlistDelete,[elem.data('playlist'),Cookies.get('csrftoken')]);});$('.playlist-edit-details').off('click.playlist').on('click.playlist',function(){$('.playlist-edit-details-modal').modal('show');});$('.playlist-edit-details-submit').off('click.playlist').on('click.playlist',function(){$.post(window.location.href+'save/',$('#playlist-edit-details-form').serialize(),function(result){$('[id^="detail-form-error"]').addClass('hidden').text('');if(result.success){$('.playlist-edit-details-modal').modal('hide');toastr.success('Your changes have been saved');$('#playlist-title').html(result.title);if(result.description.length>7){$('.playlist-details .description').html(result.description);}else{$('.playlist-details .description').html('<p>This playlist currently has no description.</p>');}}else{toastr.error('Please resolve errors then resubmit');for(i=0;i<result.errors.length;i++){if(result.errors[i][0]=='__all__'){$('#detail-form-error').removeClass('hidden').text(result.errors[i][1]);}else{$('#detail-form-error-'+result.errors[i][0]).removeClass('hidden').text(result.errors[i][1]);}}}});});$('.playlist-watch-later').off('click.playlist').on('click.playlist',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled').tooltip('hide');if(!elem.children('.active').hasClass('hidden')){elem.children().toggleClass('hidden');$.post('/playlist/watch-later/add/',{'video':elem.data('video'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){if(result.success){elem.tooltip('destroy');toastr.success('Added to watch later list');}else{elem.children().toggleClass('hidden');elem.removeClass('disabled');toastr.error(result.error);}});}}});$('.playlist-reorder').off('click.playlist').on('click.playlist',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled').tooltip('hide');if($('.playlist-video').last().attr('id')=='playlist-'+elem.data('video')&&!$('.show-more').hasClass('hidden')){extend_list.get();elem.removeClass('disabled');}else{$.post(window.location.href+'reorder/',{'action':elem.data('action'),'video':elem.data('video'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){if(result.success){if(result.target){if(elem.data('action')=='lower'){$('#playlist-'+elem.data('video')).insertBefore($('#playlist-'+result.target));if($('html, body').scrollTop()>elem.closest('.playlist-video').offset().top-90)$('html, body').scrollTop(elem.closest('.playlist-video').offset().top-90);}else if(elem.data('action')=='higher'){$('#playlist-'+elem.data('video')).insertAfter($('#playlist-'+result.target));var offset=elem.closest('.playlist-video').offset().top+elem.closest('.playlist-video').outerHeight()-$('html, body').scrollTop()-$(window).height()+5;if(offset>0)$('html, body').scrollTop($('html, body').scrollTop()+offset);}}}else{toastr.error('Failed to re-order playlist');}
elem.removeClass('disabled');});}}});$('.playlist-remove').off('click.playlist').on('click.playlist',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled').tooltip('destroy');$.post(window.location.href+'remove/',{'video':elem.data('video'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){if(result.success){toastr.success('Removed from playlist');$('#playlist-'+elem.data('video')).css({opacity:0,transition:'opacity 0.25s'}).slideUp();setTimeout(function(){$('#playlist-'+elem.data('video')).remove();if(!$('.show-more').hasClass('hidden')){if($('.playlist-list').children('.playlist-video').length<10)extend_list.get();}else{if($('.playlist-list').children('.playlist-video').length<1){$('.playlist-play-button').addClass('hidden');$('.show-more-container').append('<p>No videos found.</p>');}}},500);}else{toastr.error('Failed to remove from playlist');}});}});$('.playlist-toggle-favorite').off('click.playlist').on('click.playlist',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled');setTimeout(function(){elem.removeClass('disabled');},500);if(elem.hasClass('remove')){elem.removeClass('remove');$.post('/playlist/favorites/remove/',{'video':elem.data('video'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){if(result.success){toastr.success('Removed from favorites');}else{elem.addClass('remove');toastr.error(result.error);}});}else{elem.addClass('remove');$.post('/playlist/favorites/add/',{'video':elem.data('video'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){if(result.success){toastr.success('Added to favorites');}else{elem.removeClass('remove');toastr.error(result.error);}});}}});$('.playlist-toggle-watch-later').off('click.playlist').on('click.playlist',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled');setTimeout(function(){elem.removeClass('disabled');},500);if(elem.hasClass('remove')){elem.removeClass('remove');$.post('/playlist/watch-later/remove/',{'video':elem.data('video'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){if(result.success){toastr.success('Removed from watch later list');}else{elem.addClass('remove');toastr.error(result.error);}});}else{elem.addClass('remove');$.post('/playlist/watch-later/add/',{'video':elem.data('video'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){if(result.success){toastr.success('Added to watch later list');}else{elem.removeClass('remove');toastr.error(result.error);}});}}});}
function playlistToggleVisibility(playlist,csrf){$('.playlist-toggle-visibility').children().toggleClass('hidden');$.post('/playlist/state/',{'playlist':playlist,'csrfmiddlewaretoken':csrf},function(result){if(result.success){if(result.public){toastr.success('Playlist is now public');}else{toastr.success('Playlist is now private');}}else{$('.playlist-toggle-visibility').children().toggleClass('hidden');toastr.error(result.error);}});}
function playlistDelete(playlist,csrf){$.post('/playlist/delete/',{'playlist':playlist,'csrfmiddlewaretoken':csrf},function(result){if(result.success){window.location.href=result.target;}else{$('#error_modal').modal('show');}});}
function playlistAttachModalEvents(){$('.show-playlist-modal').off('click.playlistmodal').on('click.playlistmodal',function(){var elem=$(this);if(!elem.hasClass('disabled')){elem.addClass('disabled');$.post('/playlist/options/',{'video':elem.data('video'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){$('#create-playlist-form [class^="create-playlist-error"]').addClass('hidden').text('');if(result.success){$('.playlist-modal .options').html(result.html);$('#id_playlist').val('');playlistAttachModalEvents();$('.playlist-modal [data-toggle="tooltip"]').tooltip({container:'body',trigger:'hover'});$('.playlist-modal').modal('show');}else{toastr.error('Failed to retrieve playlist options');}
elem.removeClass('disabled');});}});$('.playlist-modal .option :checkbox').off('click.playlistmodal').on('click.playlistmodal',function(){var elem=$(this);elem.attr("disabled",true);setTimeout(function(){elem.removeAttr("disabled");},500);$('.create-playlist-entry-error').addClass('hidden').text('');if(this.checked){$.post('/playlist/'+elem.data('playlist')+'/add/',{'video':elem.data('video'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){if(result.success){toastr.success('Added to playlist');}else{$('.create-playlist-entry-error').removeClass('hidden').text(result.error);toastr.error('Failed to add to playlist');elem.prop('checked',false);}});}else{$.post('/playlist/'+elem.data('playlist')+'/remove/',{'video':elem.data('video'),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){if(result.success){toastr.success('Removed from favorites');}else{$('.create-playlist-entry-error').removeClass('hidden').text(result.error);toastr.error('Failed to remove from playlist');elem.prop('checked',true);}});}});$('.playlist-modal .create').off('click.playlistmodal').on('click.playlistmodal',function(){var elem=$(this);elem.tooltip('hide');$.post("/playlist/add/",{'video':elem.data('video'),'playlist':$('#id_playlist').val(),'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){$('#create-playlist-form [class^="create-playlist-error"]').addClass('hidden').text('');if(result.success){toastr.success('New playlist created');$('.playlist-no-options').addClass('hidden');$('.playlist-modal .options').append(result.html);$('#id_playlist').val('');playlistAttachModalEvents();$('.playlist-modal [data-toggle="tooltip"]').tooltip({container:'body',trigger:'hover'});}else{toastr.error('Please resolve errors and try again');for(i=0;i<result.errors.length;i++){if(result.errors[i][0]=='__all__'){$('.create-playlist-error').removeClass('hidden').text(result.errors[i][1]);}else{$('.create-playlist-error-'+result.errors[i][0]).removeClass('hidden').text(result.errors[i][1]);}}}});});}
function playlistExtend(extend){if(!$('.show-more').hasClass('hidden')){$('.show-more').addClass('hidden');var count=$('.playlist-list').children('.playlist-video').length;$.post(window.location.href+'extend/',{'csrfmiddlewaretoken':Cookies.get('csrftoken'),'offset':count},function(result){if(result.success){$('.playlist-list').append(result.html);spaAttachEvents();playlistAttachEvents();$('[data-toggle="tooltip"]').tooltip({container:'body',trigger:'hover'});if($('.playlist-list').children('.playlist-video').length==count+extend)$('.show-more').removeClass('hidden');}});}}