_authform = new Object();

_authform.ajax = false;
_authform.backward = '/';

_authform.open = function(backward, type, help_title, help_title2)
{
	if (type == undefined) type = 'login'; //signup
	if (type == 'login') {
		this.setLogin();
	} else if (type == 'invite') {
		type='signup';
		this.setSignup(true);
	} else {
		location.href='/user/registration/';
		//this.setSignup();
	}
	$('#_auth_form .error').html("");


	if( $('#_auth_form').attr('cloned') != "1" )
	{
		var my_form = $('<div cloned="1" id="_auth_form" class="regauth_popup">'+$('#_auth_form').html()+'</div>');
		$('#_auth_form').remove();
		$(my_form).appendTo('body');
	}


	if( help_title && help_title.length )
		$('#_auth_form #help_title').html(help_title+'<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('');



	//$('#_auth_form input[type=text]').val("");
	//$('#_auth_form input[type=password]').val("");
	toneBody(true);
	$('#_auth_form').show();
	$('#_auth_form .bg').css('height','280px');
	this.backward = ( typeof(backward) == 'undefined' || backward == '' )?document.location.pathname:backward;
};

_authform.close = function()
{
	toneBody(false);
	if ($('#_auth_invite_reg')) {
		$('#_auth_invite_reg').remove();
		$('#_auth_reg div :first').show();
	}
	$('#_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="/style/images/loading.gif" width=50/>');
		$('#_auth_form input').attr('disabled', true);
		this.ajax = true;
	}
	else
	{
		$('#_auth_form p[name=loading]').html('');
		$('#_auth_form input').attr('disabled', '');
		this.ajax = false;
	}
};

_authform.tryLogin = function()
{
	this.setAjax(true);
	var data = {
		'login':$('#_auth_login input[name=login]').val(),
		'pass':$('#_auth_login input[name=pass]').val(),
		'ajax':'login'
	};
	$.post('/user/authajax/', data, function(result) {
		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);

    $.get('/user/forget/show', function(data){
		loading(false);
		$('<div id="_auth_form_forget">'+data+'</div>')
		.appendTo('body').find('div.regauth_popup').show();
    });
}

_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);
		$('#forgotpass').show();
	});
}

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

function close_only_auth_form()
{
	$('#_auth_form').hide();
}

