_authform = new Object();

_authform.ajax = false;
_authform.backward;
_authform.ret_function=false;
	
_authform.open = function(backward, type, help_title, ret_function, help_title2)
{
	if( ret_function )
		_authform.ret_function = ret_function;
	
	if (type == undefined) type = 'login'; //signup
	if (type == 'login') {
		this.setLogin();
	} else if (type == 'invite') {
		type='signin';
		this.setSignup('invite');
	} else {
		//@mod:17.01.2012
		location.href='/user/registration/';
		return;
		this.setSignup();
	}
	$('#_auth_form .error').html("");
	
	if( help_title && help_title.length )
		$('#_auth_form #help_title').html(help_title+'<br /><br />');
	else
		$('#_auth_form #help_title').html('');
	
	if( help_title2 && help_title2.length )
		$('#_auth_form #help_title2').html(help_title2);
	else
		$('#_auth_form #help_title2').html('');	
	
	
	toneBody(true);
	$('#_auth_form').show();
	
	$('*').attr('tabindex', '-1');
	$('input').attr('tabindex', '-1');
	$('#_auth_form input[name=login]').attr('tabindex', '1');
	$('#_auth_form input[name=pass]').attr('tabindex', '2');
	$('#_auth_form #rememberme').attr('tabindex', '3');
	$('#_auth_form input:submit').attr('tabindex', '4');
	
	
	$('#_auth_form input[name=login]').focus();
	
	this.backward = (typeof(backward) == 'undefined')?document.location.pathname:backward;
};

_authform.close = function()
{
	$('a').attr('tabindex', '1');
	$('input').attr('tabindex', '1');
	if ($('#_auth_invite_reg')) {
		$('#_auth_invite_reg').remove();
		$('#_auth_reg .info').show();
	}
	toneBody(false);
	$('#_auth_form').hide();
};

_authform.setLogin = function()
{
	if (this.ajax) return;
	$('#_auth_reg').hide();
	$('#_auth_login').show();
	$('#_auth_form ul.tabs').find('li:eq(0)').addClass('active');
	$('#_auth_form ul.tabs').find('li:eq(1)').removeClass('active');
};

_authform.setSignup = function(invite)
{
	if (this.ajax) return;
	
	if (!invite) {
		location.href='/user/registration/';
		return;
	}
	
	$('#_auth_reg').show();
	$('#_auth_login').hide();
	$('#_auth_form ul.tabs').find('li:eq(0)').removeClass('active');
	$('#_auth_form ul.tabs').find('li:eq(1)').addClass('active');
};

_authform.setAjax = function(set)
{
	if (set == undefined) set = false;
	if (set)
	{
		$('#_auth_form p[name=loading]').html('<img src="/images/loading.gif" width=50/>');
		$('#_auth_form input').attr('disabled', true);
		this.ajax = true;
	}
	else
	{
		$('#_auth_form p[name=loading]').html('&nbsp;');
		$('#_auth_form input').attr('disabled', '');
		this.ajax = false;
	}
};

_authform.tryLogin = function()
{

	this.setAjax(true);
	var data = new Object();
	data['login'] = $('#_auth_login input[name=login]').val();
	data['pass'] = $('#_auth_login input[name=pass]').val();
	data['ajax'] = 'login';
	$.post('/user/authajax/', data, function(result){
		if( _authform.ret_function )
		{
			_authform.close();
			eval(_authform.ret_function);
		}
		else{
			if (result == '1')
				self.location = _authform.backward;
			else
				$('#_auth_form .error').html("The E-mail address/password combination you've entered is invalid.");
			_authform.setAjax(false);
		}
	});
};

_authform.tryReg = function()
{
	$('<form action="/user/registration/?backward='+this.backward+'" method="post">'+
	  '<input type="hidden" name="type" value="registration" />'+
	  '<input type="hidden" name="login" value="'+$('#_auth_reg input[name=login]').val()+'" />'+
	  '<input type="hidden" name="pass" value="'+$('#_auth_reg input[name=pass1]').val()+'" />'+
	  '<input type="hidden" name="cpass" value="'+$('#_auth_reg input[name=pass2]').val()+'" />'+
	  '</form>').submit();
}

_authform.forgetOpen = function()
{
	toneBody(true);
	loading(true);
	$('#_auth_form').hide();
	$('#_auth_reg').hide();
	
    $.get('/user/forget/show', function(data){
		loading(false);
		$('<div id="_auth_form_forget">'+data+'</div>')
		.appendTo('body');
    });
}

_authform.forgetClose = function()
{
	$('#_auth_form_forget').remove();
	toneBody(false);
}

_authform.forgetSend = function()
{
        if( $('#preloader_forgot_password').css('visibility') != 'hidden' )
          return;

	$('#preloader_forgot_password').css('visibility', 'visible');       
   
	$.post('/user/forget/forget', { "login": $('#FORGET_INPUT_ROW').val(), "type": "forget" }, function(data){
	    $('#_auth_form_forget').html(data);
	});
}

function open_authform( type, backward, help_title ) {_authform.open(type, backward, help_title);}
function open_forget( type, backward ) {_authform.forgetOpen();}
