$(function(){
    $('.updatecaptchareg').click(function(){$('.captchaimgreg').attr('src','?captcha&reg=1&'+(new Date).getTime())});
    $('.updatecaptcha').click(function(){$('.captchaimg').attr('src','?captcha&'+(new Date).getTime())});
    
    $('#login-butt').toggle(
        function () {
            $('.login-pop-up').fadeIn(600);
            $('.registration-pop-up').fadeOut(600);
        },
        function () {
            $('.login-pop-up').fadeOut();
        }
    );
    $('#registration-butt').toggle(
        function () {
            $('.registration-pop-up').fadeIn(600);
            $('.login-pop-up').fadeOut(600);
        },
        function () {
            $('.registration-pop-up').fadeOut(600);
        }
    );
    
    
    $('#ui-state-active').click(function(){
        $(this).attr('aria-pressed',false);
        $(this).removeClass('ui-state-active');
    });
    
    $('#exitbutt').click(function(){
        $("#logout-user").fadeIn(600);
        setTimeout(function() { window.location.href = '?auth=logout'; }, 2000);
        return false;
    });
    
    $('#loginenterf').click(function(){
        $("#errorreg").hide().html('');
        $.ajax({
            type:'post', url:'?login=login',data: $('#formloginh').serialize(),dataType:'text',
            success:function(e){
                if(e=='ok'){
                    //$('#formloginh input').val('');
	            $("#registration-pop-up").hide();
	            $("#login-auth").show();
 		 setTimeout(function() { $("#login-auth").fadeOut(600); }, 1000);
		 setTimeout(function() { window.location.reload(true); }, 2000);
                    
                }
                else{
                    //$('#errorloginf').html('<span style="color:red">'+e+'</span>');
			//$('#titlelogin').attr('style','color:red;font-weight:600;');
		        $("#logini").attr("style","border:1px solid #870001;");
			$("#logini2").attr("style","border:1px solid #870001;");
                 $("#errorreg").html('Логин и пароль введены неверно.').show();
                }
                return false;
            }
        });
        return false;
    });

    $('.aply_img .text').click(function(){
      $('.yes_real_photo', this).toggleClass('active');
      $('.no_real_photo', this).toggleClass('active');
    });

    $('#registrenterf').click(function(){
        $("#errorreg").hide().html('');
        $.ajax({
            type:'post', url:'?login=registr',data: $('#formregistrh').serialize(),dataType:'text',
            success:function(e){
                if(e=='ok'){
                    $('#registerform input').val('');
	            $("#registration-pop-up").hide();
	            $("#registration-end").show();
		    setTimeout(function(){ $("#registration-end").fadeOut(600); }, 2000);
                    return false;
                }
                else{
                    $('#registerform input').attr("style","border:1px solid #44100b;");
		    if(e<=5) {
                        if(e==1) {
                            $("#regi1").attr("style","border:1px solid #870001;");
                            $("#errorreg").html('Такой логин уже используется.').show();
                        }
                        if(e==2) {
                            $("#regi2").attr("style","border:1px solid #870001;");
                            $("#errorreg").html('Такой e-mail уже используется.').show();
                        }
                        if(e==3) {
                            $("#regi3").attr("style","border:1px solid #870001;");
                            $("#errorreg").html('Слишком короткий пароль.').show();
                        }
                        if(e==4) {
                            $("#regi3").attr("style","border:1px solid #870001;");
                            $("#regi4").attr("style","border:1px solid #870001;");
                            $("#errorreg").html('Пароли не совпадают.').show();
                        }
                        if(e==5) {
                            $("#regi"+e).attr("style","border:1px solid #870001;");
                            $("#errorreg").html('Неверно введен код.').show();
                        }
		    } else {
                        $("#errorreg").html(e).show();
		    }
                    return false;
                }
                return false;
            }
        });
        return false;
    });
    
    
    $('#forgotpassb').click(function(){
        $(".errorregforgot").hide().html('');
        $.ajax({
            type:'post', url:'?login=forgot',data: $('#formforgot').serialize(),dataType:'text',
            success:function(e){
                if(e=='ok'){
                    $('#formforgot input').val('');
	            $("#forgotpass-pop-up").hide();
	            $("#forgotok").show();
		    setTimeout(function() { $("#forgotok").fadeOut(600); }, 2000);
                }
                else{
                    $("#logini").attr("style","border:1px solid #870001;");
                    $(".errorregforgot").html(e).show();
                }
                return false;
            }
        });
        return false;
    });
    
});	

