function videoPlayerInit(url,full,auto){var config={debug:false,iconUrl:url+"images/plyr-20180117.svg",keyboard:{global:true},tooltips:{controls:true},captions:{active:true},fullscreen:{enabled:full},autoplay:auto,showPosterOnEnd:true,ratio:'16:9',invertTime:false};plyr=new window.Plyr("#player",config);}
var videoCountsRefreshed=false;function videoResetRefresh(){videoCountsRefreshed=false;}
function videoRefreshCounts(video){if(window.location.pathname.indexOf(video)!==-1){if(!videoCountsRefreshed||document.visibilityState=='visible'){$.post({url:'/video/'+video+'/counts/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){$('#video-view-count').html(result.view_count);$('#video-like-count').html(result.like_count);$('#video-dislike-count').html(result.dislike_count);$('#subscriber_count').html(result.subscriber_count);videoCountsRefreshed=true;}},complete:function(){setTimeout(videoRefreshCounts,60000,video);}});}else{setTimeout(videoRefreshCounts,60000,video);}}}
function videoShowSensitivityWarning(){$('video').removeAttr("autoplay");$('.video-container').addClass('hidden');$('.sidebar').addClass('hidden');$('.video-sensitivity-warning').removeClass('hidden');$('.video-sensitivity-button').click(function(){$('.video-sensitivity-warning').addClass('hidden');$('.video-container').removeClass('hidden');$('.sidebar').removeClass('hidden');$('#player').height(480).css({});});}
function videoSaveSettings(video){$.post('/video/'+video+'/save/',$('#save-settings').serialize(),function(result){$('#save-settings [id^="settings_error"]').addClass('hidden').text('');if(result.success){toastr.success('Your changes have been saved');$('#video-title').text(result.video_title);$('#video-description .teaser').html(result.video_teaser).removeClass('hidden');$('#video-description .full').html(result.video_description).addClass('hidden');if(result.video_description_more)$('#video-description .more').removeClass('hidden');else $('#video-description .more').addClass('hidden');$('#video-description .less').addClass('hidden');$('#video-hashtags ul').empty();var tags='';for(i=0;i<result.hashtags.length;i++){$('#video-hashtags ul').append('<li><a href="/hashtag/'+result.hashtags[i]+'/">#'+result.hashtags[i]+'</a></li>');if(i>0)tags+=' ';tags+='#'+result.hashtags[i];}
$('#id_hashtags').val(tags);if(result.category_locked){$('#id_category').attr('readonly',true);$('#id_category').find('option').not(':selected').remove();$('#id_category_locked').removeClass('hidden');}
if(result.sensitivity_locked){$('#id_sensitivity').attr('readonly',true);$('#id_sensitivity').find('option').not(':selected').remove();$('#id_sensitivity_locked').removeClass('hidden');}}else{toastr.error('Please resolve errors then resubmit');for(i=0;i<result.errors.length;i++){if(result.errors[i][0]=='__all__'){$('#settings_error').removeClass('hidden').text(result.errors[i][1]);}else{$('#settings_error_'+result.errors[i][0]).removeClass('hidden').text(result.errors[i][1]);}}
$('.nav-tabs a[href="#video-settings"]').tab('show');}});}
function videoAttachEvents(){$('#autoplay-toggle').off('click.video').on('click.video',function(){if($('#autoplay-toggle').prop('checked')){if(Cookies.getJSON('preferences')){Cookies.set('preferences',{theme:Cookies.getJSON('preferences').theme,autoplay:true},{expires:365,path:'/'});}else{Cookies.set('preferences',{theme:'day',autoplay:true},{expires:365,path:'/'});}
toastr.success('Autoplay next video enabled');}else{if(Cookies.getJSON('preferences')){Cookies.set('preferences',{theme:Cookies.getJSON('preferences').theme,autoplay:false},{expires:365,path:'/'});}else{Cookies.set('preferences',{theme:'day',autoplay:false},{expires:365,path:'/'});}
toastr.success('Autoplay next video disabled');}});$('#autoplay-details').off('click.video').on('click.video',function(){$('#autoplay-details').addClass('hidden');});$('#autoplay-paused').off('click.video').on('click.video',function(){if(useSPA)$('.sidebar-next .video-card a:first').click();else window.location.href=$('.sidebar-next .video-card a:first').attr('href');});$('.sharing-drop').off('click.video').on('click.video',function(){$('.sharing-bar').toggleClass('sharing-bar-open');$(this).toggleClass('sharing-drop-open').tooltip('hide');});$('#video-description span').off('click.video').on('click.video',function(){$(this).parent().children().toggleClass('hidden');});$('#p2p-info').off('click.video').on('click.video',function(){$('#p2p-modal').modal('show');});$('#p2p-enable').off('click.video').on('click.video',function(){videoEnableP2P(true,$(this).data('target'));});$('#p2p-disable').off('click.video').on('click.video',function(){videoEnableP2P(false,$(this).data('target'));});var player=document.getElementById('player');document.onkeypress=function(e){if((e||window.event).keyCode===32&&e.target==document.body){e.preventDefault();player.paused?player.play():player.pause();}};}
function videoAutoplayInit(playlist){if(typeof player!=='undefined'){player.addEventListener('ended',function(){if($(window).scrollTop()>100){if(document.getElementById('autoplay-toggle').checked){$('#autoplay-paused').removeClass('hidden');toastr.info('Autoplay temporarily paused whilst reading description and/or comments',{timeOut:2500});}}else if(playlist){setTimeout(function(){if(document.getElementById('autoplay-toggle').checked){if(useSPA)$('.sidebar-next .video-card a:first').click();else window.location.href=$('.sidebar-next .video-card a:first').attr('href');}},1000);}else{if(document.getElementById('autoplay-toggle').checked){$('#autoplay-details').removeClass('hidden');$('#autoplay-details .timer .spinner').addClass('animate');$('#autoplay-details .timer .filler').addClass('animate');$('#autoplay-details .timer .mask').addClass('animate');setTimeout(function(){if(!$('#autoplay-details').hasClass('hidden')){if(useSPA)$('.sidebar-next .video-card a:first').click();else window.location.href=$('.sidebar-next .video-card a:first').attr('href');}},6000);}}});player.addEventListener('playing',function(){$('#autoplay-details').addClass('hidden');$('#autoplay-paused').addClass('hidden');});}}
function setVideoState(video,action){$.post('/video/'+video+'/state/',{'action':action,'csrfmiddlewaretoken':Cookies.get('csrftoken')},function(result){if(result.success){$('#video-state-change-action').attr('href',result.target);if(useSPA)$('#video-state-change-action').click();else window.location.href=$('#video-state-change-action').attr('href');}else{$('#error_modal').modal('show');}});}
function videoCheckAdverts(){if($('.advert-container').length<1||$('.advert-container img').length<1||$('.advert-container img:first').height()<100){$('.advert-container').addClass('hidden');$('.membership-container').removeClass('hidden');}}
function videoEnableP2P(enabled,url){$('#p2p-modal').hide();$.post({url:'/settings/enable_p2p/',data:{'enabled':enabled,'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){spaChangePage(url,false);}}});}