jQuery(document).ready(function(){
	//alert('page logon');
	var pageObj = new TopLogon();
});

function TopLogon()
{
	this.logon = jQuery.cookie('logon');
	
	if(this.logon == null || this.logon == '')
	{// Юзер не залогинен
		jQuery('.top-logon-notlogged').removeClass('form-invisible');
		jQuery('.top-logon-logged').addClass('form-invisible');
	}
	else
	{// Юзер залогинен
		this.showLoggedUser();
		//jQuery('.top-logon-notlogged').addClass('form-invisible');
		//jQuery('.top-logon-logged').removeClass('form-invisible');
	}
	
	this.createHandlers();
	
}

TopLogon.prototype.createHandlers = function()
{
	var self = this;
	/*
	jQuery('#cerebro_top_logon').click(function(){
		//alert('logon');
		self.userLogonClick();
	});
	*/
	jQuery('#cerebro_top_logoff').click(function(){
		//alert('logon');
		self.userLogoffClick();
	});
	
};
/*
TopLogon.prototype.userLogonClick = function()
{
	if(jQuery('#tlf_login').val()=='' || jQuery('#tlf_password').val()=='')
	{return;}
	
	var param = '';
	param += 'login='+jQuery('#tlf_login').val();
	param += '&password='+jQuery('#tlf_password').val();
	
	var answer = getAll__('user_login', param);
	if(answer.length > 0)
	{
		// Проверяем статус ответа сервера
		if(answer[0].code.toUpperCase() == 'SECURITY ERROR')
		{// Ошибка безопасности
			this.hideProfile();
		}
		else if(answer[0].code.toUpperCase() == 'AJAX ERROR')
		{// Ошибка Ajax
			alert('Sorry. Server is not aviable now. Try later');
		}
		else if(answer[0].code.toUpperCase() != 'OK')
		{// Непонятная ошибка
			alert(answer[0].code+': '+answer[0].message);
			this.hideProfile();
		}
		else
		{
			this.showLoggedUser();
			//alert('Success!');
		}
	}
	else
	{// Этого по идее вообще не может быть, но на всякий случай проверим
		alert('Sorry. Server is not aviable now. Try later');
		this.hideProfile();
	}
};
*/
TopLogon.prototype.userLogoffClick = function()
{
	jQuery('#tlf_login').val('');
	jQuery('#tlf_password').val('');
	jQuery('.top-logon-notlogged').removeClass('form-invisible');
	jQuery('.top-logon-logged').addClass('form-invisible');
	jQuery.cookie('logon', '', {path: '/'});
	
	// Если открыта страница профиля
	if(typeof pageObj != 'undefined' && typeof pageObj.hideProfile == 'function')
	{
		pageObj.hideProfile();
	}
};

TopLogon.prototype.showLoggedUser = function()
{
	// TODO Убрать класс form-invisible из других css
	
	jQuery('.top-logon-notlogged').addClass('form-invisible');
	jQuery('.top-logon-logged').removeClass('form-invisible');
	
	// TODO Добавить запрос имени пользователя
	if(document.location.href != cerebroHostUrl+'/en/cerebro_page/user-profile' && document.location.href != cerebroHostUrl+'/ru/cerebro_page/user-profile')
	{// Открыта не страница профиля, запрашиваем полное имя юзера
		var loggedUser = getAll__('get_logged_user', '', new CerebroUser);
		if(loggedUser.length > 0)
		{
			if(loggedUser[0].code.toUpperCase() == 'SECURITY ERROR')
			{// Ошибка безопасности
				this.userLogoffClick();
			}
			else if(loggedUser[0].code.toUpperCase() == 'AJAX ERROR')
			{// Ошибка Ajax
				//alert('Sorry. Server is not aviable now. Try later');
				this.userLogoffClick();
			}
			else if(loggedUser[0].code.toUpperCase() != 'OK')
			{// Непонятная ошибка
				//alert(profile[0].code+': '+profile[0].message);
				this.userLogoffClick();
			}
			else
			{// Сгенерился ответ
				if(loggedUser.length >= 2 && typeof loggedUser[1] == 'object')
				{
					jQuery('#cerebro_top_username').text(loggedUser[1].full_name);
				}
				else
				{
					alert('Sorry. Server returns empty user data. Try later');
					this.userLogoffClick();
				}
			}
		}
		else
		{
			// Этого по идее вообще не может быть, но на всякий случай проверим
			//alert('Sorry. Server is not aviable now. Try later');
			this.userLogoffClick();
		}
	}
};

