/**
 * Авторизация пользователя
 */
function ___AbsAuth(extraOptions) {
    
    var winId = 'AuthForm'
    var left_time_res = 10;
    var left_time = 10;
    var speed_time = 1000;
    var sum;
    var currentFormObj;
    var formError;
    var formErrorText;

    var empty_err_exists;
    var validate_err_exists;

	this.init = function(){
		if(extraOptions){
			options = setOptions(options,extraOptions);
		}
        if(!checkVars()){
            return false;
        }
        initSubmitEvents();
	}

    var checkVars = function(){
        var err = false;
        var errText = new Array;
        errText.push("Error!\n");
        if(!Abs_Vars.error_empty_fields){
            errText.push('Abs_Vars.errors.empty_fields - NOT DEFINED');
            err = true;
        } 
        if(!Abs_Vars.error_wrong_value){
            errText.push('Abs_Vars.errors.wrong_value - NOT DEFINED');
            err = true;
        }
        if(err){
            errText.push("\nJavascript will work wrong");
            alert(errText.join("\n"));
            return false;
        }
        return true;
    }

    var initSubmitEvents = function(){
		$('.Abs_AuthButton').click(function() {
			currentFormObj = $(this).parents('form');
			if (checkForm() ){
				currentFormObj.submit();
			} else {
				return false;
			}
		});
	}
	
	var cSum = 2;

    $('.abs_logout').click(function(){
        if(typeof(ABS_CURRENT_LNG) == 'undefined'){
            var url = '/component/login/';
        } else {
            var url = '/'+ABS_CURRENT_LNG + '/component/login/';
        }
        //var url = '/component/login/';
        $.post(url, {act:'logout'}, function(){
            document.location.href = document.location.href;
        });
        return false;
    });
	
	var checkEmail = function() {
		var email = trim( currentFormObj.find(".abs_auth_email").val() );
		if(!email){
            empty_err_exists = true;
            currentFormObj.find(".abs_auth_email").addClass('err_input');
			return false;
		}
		if(!emailValidate(email)){
            validate_err_exists = true;
            currentFormObj.find(".abs_auth_email").addClass('err_input');
			return false;
		}
        currentFormObj.find(".abs_auth_email").removeClass('err_input');
		return true;
	}
	
	var checkPass = function() {
        var password = trim( currentFormObj.find(".abs_auth_password").val() );
		if(!password){
            empty_err_exists = true;
            currentFormObj.find(".abs_auth_password").addClass('err_input');
			return false;
		}
		if(!passValidate(password)){
            validate_err_exists = true;
            currentFormObj.find(".abs_auth_password").addClass('err_input');
			return false;
		}
        currentFormObj.find(".abs_auth_password").removeClass('err_input');
		return true;
	}
	
	var checkForm = function() {
        formError = false;
        formErrorText = new Array;
        empty_err_exists = false;
        validate_err_exists = false;
		sum = checkEmail() + checkPass();
		if(sum < cSum){
            if(empty_err_exists){
                formErrorText.push(Abs_Vars.error_empty_fields);
            }
            if(validate_err_exists){
                formErrorText.push(Abs_Vars.error_wrong_value);
            }
            currentFormObj.find(".abs_auth_error").html(formErrorText.join("<br/>"));
			return false;
		} else {
            currentFormObj.find(".abs_auth_error").html('');
        }
		return true;
	}
	
	this.authError = function(data) {
		_error(data);
	}
	
	this.authSuccess = function() {
        
        var compare = document.location.href.replace('registration','');
        if(compare == document.location.href){
            document.location.href = document.location.href;
        } else {
            document.location.href = '/';
        }
        
        /*
        var r = document.location.href;
        if(currentFormObj.find(".redirect").val() == 'index'){
            var r = '/';
        }
		document.location.href = r;
        */
	}

    this.logout = function() {
		document.location.href = document.location.href;
	}
	
	var _error = function(str) {
        left_time = left_time_res;
        currentFormObj.hide();
        currentFormObj.parent('.auth_form').find('.timer').html(str+' &nbsp;...&nbsp;<span class="abs_auth_error_timer">'+left_time+'</span>').show();
        setTimeout(function(){AbsAuth.countdown()}, speed_time);
        //AbsAuth.countdown();
//		setTimeout(function(){
//			$('#ap-overlay-AuthForm').show();
//			$('#authFormError').hide();
//		}, 5000);
	}

    this.countdown = function() {
        left_time--;
        $('.abs_auth_error_timer').html(left_time);
        if (left_time <= 0){
            currentFormObj.show();
			currentFormObj.parent('.auth_form').find('.timer').hide();
        } else {
            setTimeout(function(){AbsAuth.countdown()}, speed_time);
        }
    }
	
	this.init();
}


/**
 * Авторизация пользователя
 */
function _AbsPopupAuth(extraOptions) {

	var winId = 'AuthForm'
    var left_time_res = 10;
    var left_time = 10;
    var speed_time = 1000;
    var sum;
    

	this.init = function(){
		if(extraOptions){
			options = setOptions(options,extraOptions);
		}
	}

	var cSum = 2;

	$('.popupLoginLink').click(function(e){

        $('#status2').html(e.pageX +', '+ e.pageY);
        
		//var cont = $('#authContainer').html();

        var cont =

            '<div class="absAuthForm">'+
                '<form id="login_form" method="post" action="/component/login" target="IrFrame" class="">'+
                    '<input type="hidden" value="auth" name="act">'+
                    '<div class="fTitle">E-mail</div>'+
                    '<input type="text" value="" class="authField auth_email" name="auth_email">'+
                    '<div class="sp">&nbsp;</div>'+
                    '<div class="fTitle">Password</div>'+
                    '<input type="password" value="" class="authField auth_password" name="auth_password">'+
                    '<div class="auth_error ">&nbsp;</div>'+
                    '<div class="fTitle">&nbsp;</div>'+
                    '<div class="rem"><input type="checkbox" name="remember" id="auth_rem"/> Stay signed in</div>'+
                    '<div class="submit_wrap"><input type="button" class="loginLink" value="Sign In"></div>'+
                    '<div class="a1"><a href="/registration/remind">Password Recovery</a>&nbsp;&nbsp;&nbsp;<a href="/registration/">Register</a></div>'+
                '</form>'+
            '</div>';

		AbsPopupWindow = new _AbsPopupWindow({
			width: 		270,
			left: e.pageX - 45,
			top: e.pageY + 15 - $(document).scrollTop(),
			id: 		winId,
			content: 	cont,
			header:		'Sign In',
			reloadContent : true,
			resetPosition: true
		});
		return false;
	});


	$('.loginLink').live('click',function(){
        var check =  checkEmail() + checkPass();
        var obj = $(this).parents('#login_form');
        if(check != 2){
            obj.find('.auth_error').html('Incorrect E-mail or Password').show();
        } else {
            obj.find('.auth_error').html('&nbsp;').hide();
            $('#login_form').submit();
        }
		return false;
	});

	$('.hideAuthForm').click(function(){
		$('.authLinksWrap').show();
		$('.authWrap').hide();
		$('body').css('background-position','0 -34px');
		return false;
	});

    $('.abs_logout').click(function(){
        if(typeof(ABS_CURRENT_LNG) == 'undefined'){
            var url = '/component/login/';
        } else {
            var url = '/'+ABS_CURRENT_LNG + '/component/login/';
        }
        $.post(url, {act:'logout'}, function(){
            document.location.href = document.location.href;
        });
        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;
		}
		return true;
	}


	this.authError = function(data) {
		$('.auth_error').html('Wrong E-mail or Password').show();
	}

	this.authSuccess = function() {
        var compare = document.location.href.replace('registration','');
        if(compare == document.location.href){
            document.location.href = document.location.href;
        } else {
            document.location.href = '/';
        }
	}

    this.logout = function() {
		document.location.href = document.location.href;
	}

	this.init();
}


$(document).ready(
	function(){
		//AbsAuth = new _AbsPopupAuth();
	}
);
    
    
/**
 * Авторизация пользователя
 */
function _AbsAuth(extraOptions) {
    
    var winId = 'AuthForm'
    var left_time_res = 1;
    var left_time = 1;
    var speed_time = 1000;
    var sum;
    var currentFormObj;
    var formError;
    var formErrorText;

    var empty_err_exists;
    var validate_err_exists;

	this.init = function(){
		if(extraOptions){
			options = setOptions(options,extraOptions);
		}
        if(!checkVars()){
            return false;
        }
        initSubmitEvents();
	}

    var checkVars = function(){
        var err = false;
        var errText = new Array;
        errText.push("Error!\n");
        if(!Abs_Vars.error_empty_fields){
            errText.push('Abs_Vars.errors.empty_fields - NOT DEFINED');
            err = true;
        } 
        if(!Abs_Vars.error_wrong_value){
            errText.push('Abs_Vars.errors.wrong_value - NOT DEFINED');
            err = true;
        }
        if(err){
            errText.push("\nJavascript will work wrong");
            alert(errText.join("\n"));
            return false;
        }
        return true;
    }

    var initSubmitEvents = function(){
		$('.Abs_AuthButton').click(function() {
			currentFormObj = $(this).parents('form');
			if (checkForm() ){
				currentFormObj.submit();
			} else {
				return false;
			}
		});
	}
	
	var cSum = 2;

    $('.abs_logout').click(function(){
        if(typeof(ABS_CURRENT_LNG) == 'undefined'){
            var url = '/component/login/';
        } else {
            var url = '/'+ABS_CURRENT_LNG + '/component/login/';
        }
        //var url = '/component/login/';
        $.post(url, {act:'logout'}, function(){
            document.location.href = document.location.href;
        });
        return false;
    });
	
	var checkLogin = function() {
		var login = trim( currentFormObj.find(".abs_auth_login").val() );
		if(!login){
            empty_err_exists = true;
            currentFormObj.find(".abs_auth_login").addClass('err_input');
            currentFormObj.find(".abs_auth_login").parent('td').addClass('err_td');
			return false;
		}
        currentFormObj.find(".abs_auth_login").removeClass('err_input');
        currentFormObj.find(".abs_auth_login").parent('td').removeClass('err_td');
		return true;
	}
	
	var checkPass = function() {
        var password = trim( currentFormObj.find(".abs_auth_password").val() );
		if(!password){
            empty_err_exists = true;
            currentFormObj.find(".abs_auth_password").addClass('err_input');
            currentFormObj.find(".abs_auth_password").parent('td').addClass('err_td');
			return false;
		}
		if(!passValidate(password)){
            console.warn('!passValidate '+currentFormObj.find(".abs_auth_password").parent('td').lengt);
            validate_err_exists = true;
            currentFormObj.find(".abs_auth_password").addClass('err_input');
            currentFormObj.find(".abs_auth_password").parent('td').addClass('err_td');
			return false;
		}
        currentFormObj.find(".abs_auth_password").removeClass('err_input');
        currentFormObj.find(".abs_auth_password").parent('td').removeClass('err_td');
		return true;
	}
	
	var checkForm = function() {
        formError = false;
        formErrorText = new Array;
        empty_err_exists = false;
        validate_err_exists = false;
		sum = checkLogin() + checkPass();
		if(sum < cSum){
            if(empty_err_exists){
                formErrorText.push(Abs_Vars.error_empty_fields);
            }
            if(validate_err_exists){
                formErrorText.push(Abs_Vars.error_wrong_value);
            }
            currentFormObj.find(".abs_auth_error").html(formErrorText.join("<br/>"));
			return false;
		} else {
            currentFormObj.find(".abs_auth_error").html('');
        }
		return true;
	}
	
	this.authError = function(data) {
		_error(data);
	}
	
	this.authSuccess = function() {
        
        var compare = document.location.href.replace('registration','');
        if(compare == document.location.href){
            document.location.href = document.location.href;
        } else {
            document.location.href = '/';
        }
        
        /*
        var r = document.location.href;
        if(currentFormObj.find(".redirect").val() == 'index'){
            var r = '/';
        }
		document.location.href = r;
        */
	}

    this.logout = function() {
		document.location.href = document.location.href;
	}
	
	var _error = function(str) {
        left_time = left_time_res;
        currentFormObj.hide();
        currentFormObj.parent('.auth_form').find('.timer').html(str+' &nbsp;...&nbsp;<span class="abs_auth_error_timer">'+left_time+'</span>').show();
        setTimeout(function(){AbsAuth.countdown()}, speed_time);
        //AbsAuth.countdown();
//		setTimeout(function(){
//			$('#ap-overlay-AuthForm').show();
//			$('#authFormError').hide();
//		}, 5000);
	}

    this.countdown = function() {
        left_time--;
        $('.abs_auth_error_timer').html(left_time);
        if (left_time <= 0){
            currentFormObj.show();
			currentFormObj.parent('.auth_form').find('.timer').hide();
        } else {
            setTimeout(function(){AbsAuth.countdown()}, speed_time);
        }
    }
	
	this.init();
}    




