/**
 * Авторизация пользователя
 */
function _AbsAuth(extraOptions) {

	var winId = 'AuthForm'

	this.init = function(){
		if(extraOptions){
			options = setOptions(options,extraOptions);
		}
	}
	
	var cSum = 2;

	$('.popupLoginLink').click(function(){
		var cont = $('#authContainer').html();
		AbsPopupWindow = new _AbsPopupWindow({
			width: 		300,
			left: 100,
			top: 100,
			id: 		winId,
			content: 	cont,
			header:		'Авторизация',
			reloadContent : true,
			resetPosition: false
		});
		return false;
	});
	
	
	$('.loginLink').click(function(){
		$('.authLinksWrap').hide();
		$('.authWrap').show();
		$('body').css('background-position','0 0');
		return false;
	});
	
	$('.hideAuthForm').click(function(){
		$('.authLinksWrap').show();
		$('.authWrap').hide();
		$('body').css('background-position','0 -34px');
		return false;
	});
	
	var checkEmail = function() {
		var email = trim( $("#ap-overlay-AuthForm .auth_email").val() );
		if(!email){
			return false;
		}
		if(!emailValidate(email)){
			return false;
		}
		return true;
	}
	
	var checkPass = function() {
		var password = trim( $("#ap-overlay-AuthForm .auth_password").val() );
		if(!password){
			return false;
		}
		if(!passValidate(password)){
			return false;
		}
		return true;
	}
	
	this.checkForm = function() {
		sum = checkEmail() + checkPass();
		if(sum < cSum){
			_error('Проверьте правильность заполнения полей email и пароль');
			return false;
		}
		return true;
	}
	
	this.authError = function() {
		_error('Неправильный email или пароль');
	}
	
	this.authSuccess = function() {
		document.location.href = document.location.href;
	}
	
	var _error = function(str) {
		$('.authErrorSpan').html('Ошибка! '+str);
		$('.authForm').hide();
		$('.authFormError').show();
		setTimeout(function(){
			$('.authForm').show();
			$('.authFormError').hide();
		}, 5000);
	}
	
	this.init();
}

$(document).ready(
	function(){
		AbsAuth = new _AbsAuth();
	}
);


