var searchPageCounter=0;function searchAttachEvents(){$('#id_query').keydown(function(event){if(event.keyCode===13)$('#search-button').click();});$('#id_kind').change(function(){if($(this).val()=='channel'){$('#id_duration').prop('disabled',true);$('#id_sort').prop('disabled',true);}else{$('#id_duration').prop('disabled',false);$('#id_sort').prop('disabled',false);}})
$('#search-button').off('click.search').on('click.search',function(e){$('#search-button').prop('disabled',true);$('#search-information-top').html('');$('#search-results').html('');$('#search-information-bottom').html('').addClass('hidden');searchPageCounter=0;searchExtend(true,true);});}
function searchRenderChannelResult(data){if(!$('#result-channel-'+data.id).length)$('#search-results').append('<div id="result-channel-'+data.id+'" class="channel-result-container"><div class="channel-result-image-container">'+
'<a href="'+data.path+'" class="spa">'+
'<img class="img-responsive lazyload" src="'+data.images.loading+'" data-src="'+data.images.thumbnail+'" onerror="this.src=\''+data.images.error+'\';this.onerror=\'\';" alt="channel image">'+
'<img class="img-responsive play-overlay" src="'+data.images.action+'" alt="play">'+
'<div class="channel-result-bottom-shadow"></div>'+
'<div class="channel-result-name">'+data.name+'</div>'+
'</a>'+
'</div>'+
'<div class="channel-result-text-container">'+
'<div class="channel-result-title"><a href="'+data.path+'" class="spa">'+data.name+'</a></div>'+
'<div class="channel-result-creator"><a href="'+data.creator_path+'" class="spa">'+data.creator_name+'</a></div>'+
'<div class="channel-result-description">'+data.description+'</div><div class="channel-result-shadow"></div>'+
'<div class="channel-result-details"><span><i class="fas fa-users fa-fw"></i> '+data.subscribers+' subscribers</span></div>'+
'</div></div>');}
function searchRenderVideoResult(data){if(!$('#result-video-'+data.id).length)$('#search-results').append('<div id="result-video-'+data.id+'" class="video-result-container"><div class="video-result-image-container">'+
'<a href="'+data.path+'" class="spa"><div class="video-result-image">'+
'<img class="img-responsive lazyload" src="'+data.images.loading+'" data-src="'+data.images.thumbnail+'" onerror="this.src=\''+data.images.error+'\';this.onerror=\'\';" alt="video image">'+
'<img class="img-responsive play-overlay" src="'+data.images.action+'" alt="play">'+
'<img class="img-responsive video-sensitivity-overlay" src="'+data.images.sensitivity+'" alt="'+data.sensitivity+'">'+
'<span class="video-views"><i class="far fa-eye"></i> '+data.views+'</span>'+
'<span class="video-duration">'+data.duration+'</span>'+
'</div></a>'+
'<span class="action-button playlist-watch-later" data-video="'+data.id+'" data-toggle="tooltip" data-placement="bottom" title="Watch later">'+
'<span class="active fa-layers"><i class="fal fa-square"></i><i class="fas fa-clock" data-fa-transform="shrink-6"></i></span>'+
'<span class="fa-layers hidden"><i class="fal fa-square"></i><i class="fas fa-check" data-fa-transform="shrink-6"></i></span>'+
'</span>'+
'</div>'+
'<div class="video-result-text-container">'+
'<div class="video-result-title"><a href="'+data.path+'" class="spa">'+data.name+'</a></div>'+
'<div class="video-result-channel"><a href="'+data.channel_path+'" class="spa">'+data.channel_name+'</a></div>'+
'<div class="video-result-text">'+data.description+'</div><div class="video-result-shadow"></div>'+
'<div class="video-result-details text-left"><span>'+data.published+'</span></div>'+
'</div></div>');}
function searchExtend(force,showerror){if(force||!$('.show-more').hasClass('hidden')){$('.show-more').addClass('hidden');$('.show-more-wait').removeClass('hidden');$.post('/api/search/list/',{'csrfmiddlewaretoken':Cookies.get('csrftoken'),'query':$('#id_query').val(),'kind':$('#id_kind').val(),'duration':$('#id_duration').val(),'sort':$('#id_sort').val(),'page':searchPageCounter},function(result){if(result.success){window.history.replaceState({'url':result.url},'',result.url);if(result.total>0){searchPageCounter+=1;for(i=0;i<result.results.length;i++){if(result.results[i].kind=='channel')searchRenderChannelResult(result.results[i]);if(result.results[i].kind=='video')searchRenderVideoResult(result.results[i]);}
var count=$('#search-results').children('[class$="-result-container"]').length;toastr.success('Showing '+count+' of '+result.total+' matches');$('#search-information-top').html('Showing '+count+' of '+result.total+' matches.');if(result.count>0){$('#search-information-bottom').html('Showing '+count+' of '+result.total+' matches.').removeClass('hidden');}else{$('#search-information-bottom').html('').addClass('hidden');}
spaAttachEvents();playlistAttachEvents();if(count<result.total)$('.show-more').removeClass('hidden');}else{toastr.success('No matches were found.');$('#search-information-top').html('No matches were found.');$('#search-results').html('');$('#search-information-bottom').html('').addClass('hidden');}}else{if(showerror)toastr.error('Invalid search query string');$('#search-results').html('');$('#search-information-top').html('To run a search enter the query above and click Search. Minimum of 3 characters.');$('#search-information-bottom').html('').addClass('hidden');}}).fail(function(){toastr.error('Please refresh the page and repeat the action. If the issue persists please raise a support ticket.','Unexpected Error Occurred',{timeOut:2500});}).always(function(){$('#search-button').prop('disabled',false);$('.show-more-wait').addClass('hidden');});}}