function load(i)
{
    if(i == 1)
        bootbox.dialog('<center><i class="fa fa-spin fa-spinner" style="font-size: 20px;"></i> Loading...</center> <a class="close" id="new_load"></a>');
    else
        $('#new_load').click();
}
function loading(show)
{
    loading_active_cvar = show;


    $.getJSON(home+'section/loader', 
    function(data)
    {
    	$.each(data, function(i, val)
		{
			if(show)
			{	
				if(data['number'] == '0'){
					$('#loadinginfo').css('display', 'block');
				}else{
					if(data['number'] == '3')
						$('#load_info_3').css('display', '-webkit-box');
					else{
						if(data['number'] == '4')
							$('#load_info_4').css('display', 'flex');
						else
							$('#load_info_'+data['number']).css('display', 'block');
							loading_active(0)
					}
				}
			} else {
				if(data['number'] == '0')
					$('#loadinginfo').show();
				else
					$('#load_info_'+data['number']).hide();
			}
		});
	});
}

function loading_active(i)
{
    if(!loading_active_cvar)
        return false;

    if(i==0){
        $('#load_info_1').html('Выполнение операции');
        i=1
    }else if(i==1){
        $('#load_info_1').html('. Выполнение операции .');
        i=2
    }else if(i==2){
        $('#load_info_1').html('.. Выполнение операции ..');
        i=3
    }else if(i==3){
        $('#load_info_1').html('... Выполнение операции ...');
        i=0
    }
    setTimeout(function(){
        loading_active(i)
    }, 200);

    return null
}

function help_notice_check()
{
	$.getJSON(home+'help/section/notice/go',
	function(data)
	{
		$.each(data, function(i, val)
		{
			if(i == 'empty')
			{
				hnt = false; help_notice_title(true);
				$('#help_notice').css('display', 'none');
			}

			if(i == 'reply')
			{
				if(val)
				{
					if(val > getCookie('help') || getCookie('help') == undefined)
					{
						setCookie('help', val, {expires: 86400});
						help_notice_sound();
					}
				}

				$('#help_notice').css('display', 'block');
			}

			if(i == 'notice')
			{
				help_notice_sound();
				$('#help_notice').css('display', 'block');
				$('#help_notice_block').css('display', 'block');
				$('#help_notice_block').html(val);
			}
		});
	});

	setTimeout(function() {help_notice_check()}, 10000);
}

function help_notice_sound()
{

	hnt = true; help_notice_title();

	var audio = new Audio();
	audio.preload = 'auto';
	audio.src = '/notice.wav';
	audio.play();
}

function help_notice_title(stop)
{
	if(document.title == title && !stop)
		document.title = 'Новое сообщение';
	else
		document.title = title;

	if(hnt)
		setTimeout(function() {help_notice_title()}, 1000);
}

function setCookie(name, value, options)
{
	options = options || {};

	var expires = options.expires;

	if(typeof expires == 'number' && expires)
	{
		var d = new Date();
		d.setTime(d.getTime() + expires * 1000);
		expires = options.expires = d;
	}

	if(expires && expires.toUTCString)
		options.expires = expires.toUTCString();

	value = encodeURIComponent(value);

	var updatedCookie = name + '=' + value + '; path=/';

	for (var propName in options)
	{
		updatedCookie += '; ' + propName;
		var propValue = options[propName];

		if(propValue !== true)
			updatedCookie += '=' + propValue;
	}

  document.cookie = updatedCookie;
}

function getCookie(name)
{
	var matches = document.cookie.match(new RegExp(
		"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
	));

	return matches ? decodeURIComponent(matches[1]) : undefined;
}

function deleteCookie(name)
{
	setCookie(name, '', {expires: -1})
}

$(document).ready(function(){
	$('.spoiler').click(function(){
		$(this).parent().children('div.spoiler_main').toggle(0);
	});

	hljs.initHighlightingOnLoad();
});