$(document).ready(function(){
    $('#logoutBtn').click(LogOutUser);
    
    $("#authorisationForm").validate(
	{
        rules: {},
    	submitHandler: function(form) 
    	{
    		$(form).ajaxSubmit(
    		{
	           success: LogInUserSuccess, 
	           beforeSubmit: LogInUserPrepareRequest, 
	           dataType: "json",
	           type: "POST"
    		});
    		return false;
    	}
	});
    
    $('#passwordRestoreBlockToggler').toggle(
        function(event)
        {
            event.preventDefault();
            $('#passwordRestoreBlock').fadeIn('slow');
        },
        function(event)
        {
            event.preventDefault();
            $('#passwordRestoreBlock').fadeOut('slow');
        }
    );
    
    $('#passwordRestoreBlockCloseBtn').click(function(event){
        event.preventDefault();
        $('#passwordRestoreBlockToggler').click();
    });
    
    $('#restoreBtn').css('cursor', 'pointer');
    
    $("#restorePasswordForm").validate(
	{
        rules: {},
    	submitHandler: function(form) 
    	{
    		$(form).ajaxSubmit(
    		{
	           success: RestorePasswordSuccess, 
	           beforeSubmit: RestorePasswordPrepareRequest, 
	           dataType: "json",
	           type: "POST"
    		});
    		return false;
    	}
	});
});

var justLoggedUser = null;
function LogInUserSuccess(data, status)
{
	if (!LogInOutUserAJAXResponseHandler(data, status)) return;
	if (data.msg == "ok") 
	{
	    justLoggedUser = data.user;
	    $('#userOutBlock').slideUp('slow', ShowUserInBlock);
	};
}

function ShowUserInBlock()
{
    $('#userOutBlock').remove();
    var userInBlock = $('<div id="userInBlock" class="hide"><div class="row"><div class="user-fio">'+ justLoggedUser.FIO +'</div></div><div class="links"><a href="/user/profile/">Профиль</a> / <a href="/user/logout/" id="logoutBtn" class="grey">Выход</a></div> <!-- links --></div>');
    $('.user-area').prepend(userInBlock);
    $('#userInBlock').slideToggle('slow');
    $('#logoutBtn').click(LogOutUser);
}

function LogInUserPrepareRequest(form)
{
    //$("input:submit").attr("disabled", true);
	form.push({name:"async",value:"1"});
	form.push({name:$("#authorisationForm input:submit").attr("name"),value:"1"});
	$('.user-area').children('.error').hide();
	justLoggedUser = null;
	
	//$("#commentForm").children('div.block').children('div.buttons').append('<span id="ajaxLoading"><img src="/images/icons/ajax-loader.gif" /></span>');
}

function LogOutUser(event)
{
    event.preventDefault();
    $.post(
        $(this).attr("href"),
        {},
        LogOutUserResponse,
        "json"
    );
}

function LogOutUserResponse(data, status)
{
    if (!LogInOutUserAJAXResponseHandler(data, status)) return;
    
	if (data.msg == "ok") 
	{
	    window.location.reload();
	};
}

function LogInOutUserAJAXResponseHandler(data, status)
{
    if (status == 'success')
	{
		if (data.error)
		{
		    $('.user-area').children('.error').text(data.error);
		    $('.user-area').children('.error').fadeIn('slow');
		    
		    setTimeout("RemoveLogInOutUserAJAXError()", 10000);
		    
			return false;
		}
	}
	else
	{
		$.jGrowl("Ошибка запроса");
		return false;
	}
	return true;
}

function RemoveLogInOutUserAJAXError()
{
    $('.user-area').children('.error').fadeOut('slow');
}

function RestorePasswordPrepareRequest(form)
{
	form.push({name:"async",value:"1"});
	form.push({name:$("input:submit").attr("name"),value:"1"});
	$('#passwordRestoreBlock .error').text('').hide();
	$('#restoreAjaxLoader').show();
}

function RestorePasswordSuccess(data, status)
{
    $('#restoreAjaxLoader').hide();
	if (!RestorePasswordAJAXResponseHandler(data, status)) return;
	if (data.msg == "ok") 
	{
	    $('#passwordRestoreBlock .restoreContent').slideUp('slow', function(){
            $('#passwordRestoreBlock .restoreSuccess').slideToggle('slow');
        });
	};
}

function RestorePasswordAJAXResponseHandler(data, status)
{
    if (status == 'success')
	{
		if (data.error)
		{
		    $('#passwordRestoreBlock .error').text(data.error).fadeIn('slow');
		    
			return false;
		}
	}
	else
	{
		$.jGrowl("Ошибка запроса");
		return false;
	}
	return true;
}

function RemoveRestorePasswordAJAXError()
{
    $('#passwordRestoreBlock .error').text('').hide();
}