$(document).ready(function(){
        
          var GROWL_DEF_TIME = 50000;

	   /**     
         *      Валидация и отправка регестрационніх данных на сервер методом POST  
         *      --sources: view/identification/registration.php
         *      --action: core/action/register.php
         **/
        $('#registr-form').validator().submit(function(e){
                if(e.isDefaultPrevented() == false) {
                        /*Заносим объект формы в переменную, чтобы уменьшить количество вызовов DOM функций*/
                        var $form = $("#registr-form");
                        /*Проверяем введен ли пароль*/
                        if($form.find(".password").length != 0) {
                                $.post("core/action/register.php", $form.serialize(), function (data){
                                        if (data.location != undefined){
                                                $.Growl.show({
                                                        'message': data.msg,
                                                        'timeout': GROWL_DEF_TIME,
                                                        'speed' : 500,
                                                        'onTimeout' : function(){
                                                                document.location.href = data.location;
                                                        }
                                                });
                                        } else {
                                                $.Growl.show({
                                                        'message': data.msg,
                                                        'timeout': GROWL_DEF_TIME,
                                                        'speed' : 500
                                                });
                                        }
                                        if(data.success == 1){
                                                $.cookie('clorp_error', parseInt(1));
                                        } else {
                                                captcha_cookie();
                                        }
                                },"json");
                        }
                }
                
                return false;
        });

	    
	    $("#loginform").validator().submit(function(e){
			if(e.isDefaultPrevented() == false) {
		        var l=$("#loginform .login").val();
		        var p=$("#loginform .password").val();
		        var c=$("#loginform .captcha").val();
		
		        if(p.length>0){
		            $.post("core/action/login.php", { l:l, p:p, c:c }, function (data){
		            	
		            	$.Growl.show({
		            		'message': data.msg,
		            		'timeout': GROWL_DEF_TIME,
		            		'speed' : 500
		            	});
		                if(data.success == 1){
		                    $.cookie('clorp_error', 1*1);
		                    //$('#loginform').html("<p>Вы успешно авторизированы!</p>");
		                    if($.cookie('href')) {
		                    	href = $.cookie('href');
		                    	$.cookie('href', null);
		                    	location = href;
		                    } else {
		                    	location = data.location;
		                    }
		                } else { 
							captcha_cookie()
		                }
		            }, "json");
		        } else {
		 		   	captcha_cookie();
		        }
		        return false; 
	        }
	    });

		$('#forgotform2').validator().submit(function(e){
			if(e.isDefaultPrevented() == false) {
				var new_password = $('#new_password').val();
				var code = $('#code').val();
	    		$.post("core/action/login.php", {new_password:new_password, code:code}, function(data){
	    			//if(data.success == 1) {
		    			$.Growl.show({
			          		'message': data.msg,
			          		'timeout': 500,
				          	'speed' : 0
		        		});
		        		//href = '<?php urlByName(_autorization); ?>';
		                location = href;
	    			//}
	    		},"json");
				
				return false; 
			} 
		});
		
	$('#forgotform1').validator().submit(function(e){
		if(e.isDefaultPrevented() == false) {
			var forgot_mail = $('.forgot_mail').val();
			
    		$.post("core/action/login.php", {forget_mail:forgot_mail}, function(data){
    			console.log(data);
    			//if(data.siccess == 1) {
	    			$.Growl.show({
		          		'message': data.msg,
		          		'timeout': 500,
			          	'speed' : 0
	        		});
    			//}
    		},"json");
			
			return false; 
		} 
	});


    function captcha_cookie() {
                if ($.cookie('clorp_error') == null) {
                        $.cookie('clorp_error', parseInt(1));
                } else $.cookie('clorp_error', parseInt($.cookie('clorp_error')) + parseInt(1));
		
                if (parseInt($.cookie('clorp_error')) > parseInt(3)) {
                        $('#captcha').css('display', 'block');
                        $('#captcha img').attr('src', 'mod/captcha/index.php?PHPSESSID='+Math.random())
                }
    
        }



    $('.out_from_profile').click(function(e) {
        e.preventDefault();
        href = $(this).attr('href');
        	$.post("core/action/logout.php", {logout:1}, function(data){
        		console.log(data);
    	    	if(data.success == 1) {
    	    		$.Growl.show({
		          		'message': 'вы вышли из своего профиля',
		          		'timeout': 500,
			          	'speed' : 0
	        		});
    	    		location = href;
    	    	}
        	}, "json");
        return false;
       }); 
		
		
});//end ready
