//<![CDATA[


var _main                  = {

    init        : function() {

        this.before();

    },

    before      : function() {
        _loader.core();
    },

    after       : function() {
        validate.init();
        validateforgotpassword.init();
        qmodal.init();
        captcha.init();
    }
};


var captcha                = {

    init        : function() {

        if (this.check())  this.core();

    },

    check       : function() {

        return true;

    },

    core        : function() {

    	$('li.captcha em a').click( function() {

    		captcha.reload();
    		return false;

    	});
    },

    reload   	: function() {

		$("#security_image").attr({
          src: "/img/captcha.jpg?"+Math.random(),
          title: "Digite o código de segurança exibido na imagem",
          alt: "Digite o código de segurança exibido na imagem"
        });

        $("#captcha").val('');

    }

};


var validate               = {

    init        : function() {

        if (this.check())  this.core();

    },

    check       : function() {

        return ( _exists('#login-us') && typeof jQuery.fn.validate == 'function'  ) ? true : false;

    },

    core        : function() {

		$('#login-us').validate({

			rules   :{
				login           :{
					required	: true,
					email		: true
				},
				password        :{
					required	: true,
					minlength	: 8
				}
			},
			messages:{
				login           :{
					required	: 'Campo de preenchimento obrigatório.',
					email		: 'E-mail inválido.'
				},
				password        :{
					required	: 'Campo de preenchimento obrigatório.',
					minlength	: 'Campo deve conter no mínimo 8 caracteres.'
				}
			},

            onkeyup             : false,
            submitHandler       : function(form) {

                $(form).ajaxSubmit({
                    dataType        : 'json',
                    success         : validate.result
                });
            }

		});
    },

    result      : function(data) {

        var status  = data.claro.status;
        var redirect  = data.claro.redirect;

        if(status==1){
        	window.location.href = redirect;
        }else{
        	$('#password').val('');

        	$('#error').remove();

        	var html    = '<div id="error">';

        	$.each(data.claro.error, function(i, item) {
                html    += '<p>' + item + '</p>\n';
            });

           html += '</div>';

           $('#login-us').after(html);
        }

    }

};


var validateforgotpassword = {

    init        : function() {

        if (this.check())  this.core();

    },

    check       : function() {

        return ( _exists('#forgotpassword') &&
                typeof jQuery.fn.validate == 'function'  &&
                typeof jQuery.fn.ajaxSubmit == 'function' ) ? true : false;

    },

    core        : function() {

		$('#forgotpassword').validate({

			rules   :{
				emailforgot     :{
					required	: true,
					email		: true
                    //remote      : '/check-email/'
				},
				captcha         :{
					required	: true,
                    remote      : '/check-captcha/'
				}
			},
			messages:{
				emailforgot     :{
					required	: 'Campo de preenchimento obrigatório.',
					email		: 'E-mail inválido.'
                    //remote      : jQuery.format('O e-mail {0} já está em uso, utilize outro.')
				},
				captcha         :{
					required	: 'Campo de preenchimento obrigatório.',
					remote      : 'O campo de código de segurança está incorreto.'
				}
			},

            onkeyup             : false,
            submitHandler       : function(form) {

                $(form).ajaxSubmit({
                    dataType        : 'json',
                    success         : validateforgotpassword.result
                });
            }

		});
    },

    result      : function(data, teste) {

        var status  = data.claro.status;

        $('#error').remove();

        var html    = '<div id="error"><ol>';

        if (status == 0) {
            $.each(data.claro.error, function(i, item) {
                html    += '<li>' + item + '</li>\n';
           });

           html += '</ol></div>';

           $('form#forgotpassword').after(html);

        } else {

            var html    = '';

            $.get('/redefinir_sucesso/' + data.claro.email + '/', function(data) {
                html = data;
                $('form#forgotpassword').fadeOut( function() {
                    $(this).html(html).fadeIn('slow');
                });
            });

        }

    }

};


var qmodal                 = {

    init        : function() {

        if (this.check())  this.core();

    },

    check       : function() {

        return ( typeof jQuery.fn.jqm == 'function' ) ? true : false;

    },

    core        : function() {

        var html    = '<div class="jqmWindow password" id="modalpassword"></div>';
        $('body').append(html);

        var myLoad  = function() { validateforgotpassword.init(); captcha.init(); };

        $('#modalpassword').jqm({
            ajax        : def.project.path + 'get_esqueci_senha',
            trigger     : 'a.forgotpassword',
            onLoad      : myLoad
        });

    }
};


_main.init();


//]]>