/**
 * Авторизация пользователя
 */
function _AbsAuthTop(container,inText) {
    
    var left_time_res = 5;
    var left_time = 5;
    var speed_time = 1000;
    var currentFormObj;
    var formError;
    var formErrorText;

    var empty_err_exists;
    var validate_err_exists;

	var init = function(){
        if(inText){
            inTextGo();
        }
        if(!checkVars()){
            return false;
        }
        initSubmitEvents();
	}
    
    var inTextGo = function(){
        
        $(container).find('.in_text').click(function(){
            $(this).next('.auth_input').focus();
        });
        
        $(container).find('.authForm .auth_email').focus(function() { inTextTitle( $(this), 'in' ); });
        $(container).find('.authForm .auth_password').focus(function() { inTextTitle( $(this), 'in' ); }); 
        
        $(container).find('.authForm .auth_email').change(function() { inTextTitle( $(this), 'in' ); });
        $(container).find('.authForm .auth_password').change(function() { inTextTitle( $(this), 'in' ); }); 
        
        $(container).find('.authForm .auth_email').keyup(function() { inTextTitle( $(this), 'in' ); });
        $(container).find('.authForm .auth_password').keyup(function() { inTextTitle( $(this), 'in' ); }); 
        
        $(container).find('.authForm .auth_email').focusout(function() { inTextTitle( $(this), 'out' ); });
        $(container).find('.authForm .auth_password').focusout(function() { inTextTitle( $(this), 'out' ); }); 

    }
    
    var inTextTitle = function(obj, type){
        if(type == 'in'){
            $(obj).prev('.in_text').hide();
        }
        if(type == 'out'){
            if( trim($(obj).val()) ){
                $(obj).prev('.in_text').hide();
            } else {
                $(obj).prev('.in_text').show();
            }
        }
    }

    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(){
		$(container).find('.Abs_AuthButton').click(function() {
			currentFormObj = $(this).parents('form');
			if (checkForm() ){
				currentFormObj.submit();
                return false;
			} else {
				return false;
			}
		});
	}

    
	
	var checkEmail = function() {
		var email = trim( currentFormObj.find(".auth_email").val() );
		if(!email){
			return false;
		}
		if(!emailValidate(email)){
			return false;
		}
		return true;
	}
	
	var checkPass = function() {
        var password = trim( currentFormObj.find(".auth_password").val() );
		if(!password){
			return false;
		}
		if(!passValidate(password)){
			return false;
		}
		return true;
	}
	
	var checkForm = function() {
        if(!checkEmail() || !checkPass()){
            _error();
            return;
        }
        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 = '/';
        }
	}

    this.logout = function() {
		document.location.href = document.location.href;
	}
	
	var _error = function(str) {
        left_time = left_time_res;
        $(container).find('.authForm').hide();
        $(container).find('.authFormError').show();
        $(container).find('.abs_auth_error_timer').html(left_time);
        setTimeout(function(){countdown()}, speed_time);
	}

    var countdown = function() {
        left_time--;
        //console.info(left_time);
        $(container).find('.abs_auth_error_timer').html(left_time);
        if (left_time <= 0){
            $(container).find('.authForm').show();
            $(container).find('.authFormError').hide();
        } else {
            setTimeout(function(){countdown()}, speed_time);
        }
    }
	
	init();
}



/**
 * Авторизация пользователя
 */
function _AbsAuthOnPage(container,extraOptions) {
    
    var left_time_res = 10;
    var left_time = 10;
    var speed_time = 1000;
    var currentFormObj;
    var formError;
    var formErrorText;
    var currentObj = this;
    var empty_err_exists;
    var validate_err_exists;

	this.init = function(){
        initSubmitEvents();
	}

    var initSubmitEvents = function(){
		$(container).find('.Abs_AuthButton').click(function() {
			currentFormObj = $(this).parents('form');
			if (checkForm() ){
				currentFormObj.submit();
			} else {
				return false;
			}
		});
	}
    
    var checkForm = function() {
        var email = trim( $(container).find('.abs_auth_email').val() );
        var password = trim( $(container).find('.abs_auth_password').val() );
        if(!email || !password){
            $(container).find('.abs_auth_error').html( $(container).find('.error_empty_fields').val() ).show();
            return false;
        } else {
            $(container).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 = '/';
        }

	}

    this.logout = function() {
		document.location.href = document.location.href;
	}
  
    
    var _error = function(str) {
        left_time = left_time_res;
        $(container).find('.authWrap').hide();
        $(container).find('.authFormError').show();
        //currentFormObj.parents('.on_page_login_wrap').find('.abs_auth_error_timer').html(left_time);
        setTimeout(function(){countdown()}, speed_time);
	}

    countdown = function() {
        left_time--;
        console.warn(left_time);
        $(container).find('.abs_auth_error_timer').html(left_time);
        if (left_time <= 0){
            $(container).find('.authWrap').show();
            $(container).find('.authFormError').hide();
        } else {
            setTimeout(function(){countdown()}, speed_time);
        }
    }
	
	this.init();
}    


$(document).ready(
	function(){
		$('.abs_logout').live('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;
		});
	}
);



