$(document).ready(function(){
	
	$("body").addClass("js");
	
	$('body').on('click', '.fav-guest', function() {
	DLEalert("Регистрируйтесь, и вы сможете добавлять новости, создавать свои закладки, оценивать и многое другое!", 'Необходима регистрация');
	return false;
	});
	
	$('#mosaic #dle-content #bottom-nav').insertAfter($('#dle-content'));
	
	if ($(window).width() > 1220) {
		$('.nav-menu li:has(.hidden-menu)').hover(function() {
				$(this).find('.hidden-menu').fadeToggle(200);
				$(this).toggleClass('menuactive');
		});	
	}; 
		
	$('.nav-menu li:has(ul)').addClass('submenu');
	
	
	$('body').on('click','#pagi-load a',function(){
		var $urlNext = $(this).attr('href');
		var $scrollNext = $(this).offset().top - 200;
        if ($urlNext !== undefined) {
			$.ajax({
				url: $urlNext,
				beforeSend: function() {
					ShowLoading('');
				},			 
                success: function(data) {
                    $('#bottom-nav').remove();
                    $('#dle-content').append($('#dle-content', data).html());
                    $('#dle-content').after($('#bottom-nav'));
					window.history.pushState("", "", $urlNext);
					$('html, body').animate({scrollTop:$scrollNext}, 800);	
					HideLoading('');
                },
				  error: function() {				
					HideLoading('');
					alert('что-то пошло не так');
				  }
			});
		};
		return false;
	});
	
	$('.slaider-wrp:first').addClass('slaider-activ').find('.slaider-big').fadeIn();
	$('.slaider-wrp:first').find('.slaider-mini').addClass('mini-activ');
	
	
	function showNext() {
		clearTimeout(timerId);
		var currentBlock = $('.slaider-activ');
		var nextBlock = currentBlock.next('.slaider-wrp').length ? currentBlock.next('.slaider-wrp') : $('.slaider-wrp:first');
		currentBlock.find('.slaider-big').fadeOut();
		$('.slaider-wrp').removeClass('slaider-activ');
		nextBlock.addClass('slaider-activ').find('.slaider-big').fadeIn();
		currentBlock.find('.slaider-mini').removeClass('mini-activ');
		nextBlock.find('.slaider-mini').addClass('mini-activ');
		timerId = setTimeout(showNext, 4000);
	};
	
	timerId = setTimeout(showNext, 4000);

	
	$('.slaider-mini').click(function() {
		clearTimeout(timerId);
		$('.slaider-wrp').removeClass('slaider-activ');
		$('.slaider-big').fadeOut();
		$(this).parent().addClass('slaider-activ').find('.slaider-big').fadeIn();
		$(this).parent().addClass('slaider-activ').find('.slaider-big').fadeIn();
		$('.slaider-wrp').find('.slaider-mini').removeClass('mini-activ');
		$(this).parent().addClass('slaider-activ').find('.slaider-mini').addClass('mini-activ');
		
		
	});
		
	$('.slaider-big').hover(
		function() {
		clearTimeout(timerId);
	}, function() {
		timerId = setTimeout(showNext, 4000);
	});
	
	$('body').append('<div class="close-overlay" id="close-overlay"></div><ul class="side-panel" id="side-panel"></ul>');
	$('.nav-menu').each(function() {
		$($(this).html()).appendTo('#side-panel');
	});	
	$(".show-menu").click(function(){
		$('#side-panel').addClass('active');
		$("#close-overlay").fadeIn(200);
	});
	$(".close-overlay").click(function(){
		$('#side-panel').removeClass('active');
		$('#close-overlay').fadeOut(200);
	}); 
	
	$('.tabs-sel').delegate('span:not(.current)', 'click', function() {
		$(this).addClass('current').siblings().removeClass('current')
		.parents('.tabs-box').find('.tabs-b').hide().eq($(this).index()).fadeIn(400);
	});
	
	
	$('.full-text table').each(function(){
		$(this).wrap("<div class='table-resp' />");
	});
	
	$("#login-box").dialog({
		autoOpen: false,
		modal: true,
		show: 'fade',
		hide: 'fade',
		width: 320
	});

	$('.show-login').click(function(){
		$('#login-box').dialog('open');
	});
	
	$('#show-login, #ac-av').html($('#lb-ava').html());
	
	$(".add-comm-btn").click(function(){
		$("#add-comm-form").slideToggle(200);
	});
	$(".reply").click(function(){
		$("#add-comm-form").slideDown(200);
	});
	$('body').on('click','.ac-textarea textarea, .fr-wrapper',function(){
		$('.add-comm-form').addClass('active').find('.ac-protect').slideDown(400);
	});
	$('.lb-soc a, .ac-soc a').on('click',function(){
	   var href = $(this).attr('href');
       var width  = 820;
       var height = 420;
       var left   = (screen.width  - width)/2;
       var 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;
	});
    $('#dle-content > #dle-ajax-comments').appendTo($('#full-comms')); 
	
	$('body').append('<div id="gotop"><span class="fa fa-chevron-up"></span></div>');
	var $gotop=$('#gotop'); 
	$(window).scroll (function () {
		if ($(this).scrollTop () > 300) {$gotop.fadeIn(200);
		} else {$gotop.fadeOut(200);}
	});	
	$gotop.click(function(){
		$('html, body').animate({ scrollTop : 0 }, 'slow');
	});
	
	if ($(window).width() > 1220) {	
	var LinkTitle = $(document).attr('title');
	var LinkUrl = window.location.href;
	LinkTitle=encodeURIComponent(LinkTitle);
	LinkUrl=encodeURIComponent(LinkUrl);
	var leftvar = (screen.width-600)/2;
	var topvar = (screen.height-400)/2;
	
	var LinksItem = [
	'http://vkontakte.ru/share.php?url='+LinkUrl+'" onClick="popupWin = window.open(this.href, \'contacts\', \'location,width=600,height=400,left='+leftvar+',top='+topvar+'\'); popupWin.focus(); return false;"',
	'http://twitter.com/share?text='+LinkTitle+'&url='+LinkUrl+'" onClick="popupWin = window.open(this.href, \'contacts\', \'location,width=600,height=400,left='+leftvar+',top='+topvar+'\'); popupWin.focus(); return false;"',
	'http://www.facebook.com/sharer.php?u='+LinkUrl+'&t='+LinkTitle+'" onClick="popupWin = window.open(this.href, \'contacts\', \'location,width=600,height=400,left='+leftvar+',top='+topvar+'\'); popupWin.focus(); return false;"',
	'https://plus.google.com/share?url='+LinkUrl+'" onClick="popupWin = window.open(this.href, \'contacts\', \'location,width=600,height=400,left='+leftvar+',top='+topvar+'\'); popupWin.focus(); return false;"',
	'http://www.odnoklassniki.ru/dk?st.cmd=addShare&st._surl='+LinkUrl+'&title='+LinkTitle+'" onClick="popupWin = window.open(this.href, \'contacts\', \'location,width=600,height=400,left='+leftvar+',top='+topvar+'\'); popupWin.focus(); return false;"'
	];
	
	$('body').append('<div class="share-big"></div>');  
	$('.share-big').each(function(){
		var $share = $(this);
		for (i=0; i<LinksItem.length; i++)
		{
			var getLinks = $('<a href="'+LinksItem[i]+' target="_blank"></a>');
			getLinks.appendTo($share);
		}
	});
	};
});

/* end */