$(document).ready(function(){

	/* ====================== BUILDING ====================== */
	var append = `
	<div class="overlay"></div>
	<div class="mobile-menu">
		<div class="mobile-menu__header d-flex ai-center">
			<button class="mobile-menu__btn-close btn-without-bg fal fa-times" aria-label="Закрыть мобильное меню"></button>
		</div>
		<div class="mobile-menu__content"></div>
	</div>
	<button id="scrolltop" class="scrolltop fal fa-arrow-up" aria-label="Наверх"></button>
	`;
	$('body').append(append);
	
	/* ====================== EVENTS ====================== */

    function scrollerCheck() { 
		$('.scroller').each(function(){
			var scroller = $(this).find('.scroller__content'), scrollerLeft = $(this).find('.scroller__btn--prev');
			scroller.scrollLeft() > 50 ? scrollerLeft.addClass('is-active') : scrollerLeft.removeClass('is-active'); 
		});
	};
	setInterval(scrollerCheck, 300);

	$(document).on('click','.scroller__btn',function(){
		var aa = $(this), a = aa.closest('.scroller').find('.scroller__content'), 
		shift = a.find('.item:first').outerWidth() + 20,
		b = a[0].scrollWidth, c = a.scrollLeft(), d = a.parent().outerWidth(), 
		e = shift + c, f = c - shift, bb = b - d + 5;
		if ( aa.hasClass('scroller__btn--prev') ) {
			a.animate({ scrollLeft: f + 'px' });
		} else {
			e > bb ? a.animate({ scrollLeft: '0px' }) : a.animate({ scrollLeft: e + 'px' });
			console.log(e+' '+bb);
		};
	});	
	
	$(".theme-toggle").click(function(){
		if (localStorage.getItem('theme') === 'dark') {
			localStorage.removeItem('theme');
		} else {
			localStorage.setItem('theme','dark');
		};
		switchLight();
	});
	
	$('.search-block__input').blur(function(){
		$('body').removeClass('search-is-active');
	});
	$('.search-block__input').focus(function(){
		var scrWidth = window.innerWidth - document.body.offsetWidth;
		$('body').css({'--scrWidth':scrWidth+'px'});
		$('body').toggleClass('search-is-active');
		var searchWidth = $('.search-block__input').outerWidth();
		$('body').css({'--searchWidth':searchWidth+'px'});
	});
	$(document).on('click','.js-show-login',function(){
		$('.overlay, .login').fadeIn(200);
		$('body').addClass('modal-is-opened');
		return false;
	})
	.on('click','.js-show-login-panel',function(){
		$('.header__login-panel').fadeToggle(209);
	})
	.on('click','.mobile-menu__content .header__menu-has-submenu > a',function(){
		$(this).parent().toggleClass('is-active');
		$(this).parent().siblings().removeClass('is-active');
		return false;
	});	



	$(".page__btn-dl").click(function(){
		//$('.page__dl').fadeIn(0, function() {
			$('html, body').animate({scrollTop:$('.page__dl').offset().top - 10}, 800);
			return false;
		//});
	});
	
	$(document).on('click','.pagination__btn-loader a',function(){
		var urlNext = $(this).attr('href'), scrollNext = $(this).offset().top;
		$.ajax({
			url: urlNext,
			beforeSend: function() { ShowLoading('<p class="bolder">Загрузка</p>Пожалуйста, подождите...','right','top'); },			 
			success: function(data) {
				$('#pagination').remove(); $('#dle-content').append($('#dle-content', data).html());
				window.history.pushState("", "", urlNext);
				$('html, body').animate({scrollTop:scrollNext}, 800);	
				$('.pagination__btn-loader span').text('Больше нет новостей');
				HideLoading('');
			},
			error: function() { HideLoading(''); alert('что-то пошло не так'); }
		});
		return false;
	});

	$(".js-show-mobile-menu").click(function(){
		if ($('.mobile-menu--is-generated').length === 0) {
			$('.header__logo').clone().addClass('flex-grow-1').prependTo('.mobile-menu__header');
			$('.js-this-in-mobile-menu').each(function() {
				$(this).clone().appendTo('.mobile-menu__content');
			});	
		};
		$('.overlay').fadeIn(200);
		$('.mobile-menu').addClass('is-active mobile-menu--is-generated');
		$('body').addClass('mobile-menu-is-opened');
	});
	$(document).on('click','.overlay, .login__close, .mobile-menu__btn-close',function(){
		$('.overlay, .login, .sinfo').fadeOut(200,function(){ $('#sinfo').remove(); });
		$('.mobile-menu').removeClass('is-active');
		$('body').removeClass('modal-is-opened mobile-menu-is-opened trl-is-opened sinfo-is-opened');
	});
	
	/* ====================== DLE SCRIPTS ====================== */

	$(document).on('click','.ac-form__editor textarea, .fr-wrapper, .ac-form',function(){
		$('.ac-form').addClass('is-active').find('.message-info').show();
		$('.ac-toggle').removeClass('ac-toggle');
	})
	.on('click','.login__social a, .serv__social a',function(){
	   	var href = $(this).attr('href'), width  = 820, height = 420, 
	   		left   = (screen.width  - width)/2, top   = (screen.height - height)/2-100;   
		auth_window = window.open(href, 'auth_window', "width="+width+",height="+height+",top="+top+",left="+left+"menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
       	return false;
	}); 
	$('.js-comm-author').each(function(){
        var a = $(this), b = a.closest('.js-comm'), c = a.text().substr(0,1), 
            f = b.find('.js-comm-avatar'), e = f.children('img').attr('src'),
			d = ["#c57c3b","#753bc5","#79c53b","#eb3b5a","#45aaf2","#2bcbba","#778ca3"], rand = Math.floor(Math.random() * d.length);
		if (e == '/dleimages/noavatar.png') {
            f.html('<div class="comm__letter d-flex jc-center ai-center" style="background-color:'+d[rand]+'">'+c+'</div>');
		};
    });	
	var gotop = $('#scrolltop'); 
	$(window).scroll (function () {
		if ( $(this).scrollTop () > 300 ) { gotop.addClass('is-active'); } 
		else { gotop.removeClass('is-active'); }
	});	
	gotop.click(function(){
		$('html, body').animate({ scrollTop : 0 }, 'slow');
	});
});

/* END */