(function ($) {
    var vougeAuthValidation = {
        init: function () {
            $('body').on('click', '.vouge-pro-login-link', function (e) {
                e.preventDefault();

                $.ajax({
                    type: "POST",
                    url: vouge_urls.ajaxurl,
                    data: {
                        action: 'vouge_pro_show_login_form_popup',
                    },
                    success: function (response) {
                        $('body').find('.vouge-pro-login-form-container, .vouge-pro-login-form-overlay').remove();
                        $('body').append(response);
                        $('#user_login').focus();
                        vougeAuthValidation.addPlaceholder();
                    }
                });
            });

            $('body').on('click', '.vouge-pro-login-form-overlay', function (e) {
                e.preventDefault();
                $('body').find('.vouge-pro-login-form-container, .vouge-pro-login-form-overlay').fadeOut();
            });

            jQuery(document).on('click', '#vouge-custom-auth-register-button', function (e) {
                e.preventDefault();

                var first_name = $('#first_name').val();
                var last_name = $('#last_name').val();
                var user_name = $('#user_name').val();
                var password = $('#password').val();
                var cpassword = $('#cpassword').val();
                var user_email = $('#user_email').val();

                $.ajax({
                    type: "POST",
                    url: vouge_urls.ajaxurl,
                    data: {
                        action: 'vouge_pro_register_user_front_end',
                        first_name: first_name,
                        last_name: last_name,
                        user_name: user_name,
                        password: password,
                        cpassword: cpassword,
                        user_email: user_email
                    },
                    success: function (results) {
                        $('.vouge-custom-auth-register-alert').text(results).show();
                    },
                    error: function (results) {
                        // Handle error
                    }
                });
            });
        },

        addPlaceholder: function () {
            // Login Form Scripts
            $('#loginform input[id="user_login"]').attr('placeholder', 'Username');
            $('#loginform input[id="user_pass"]').attr('placeholder', 'Password');

            $('#loginform label[for="user_login"]').contents().filter(function () {
                return this.nodeType === 3;
            }).remove();
            $('#loginform label[for="user_pass"]').contents().filter(function () {
                return this.nodeType === 3;
            }).remove();

            $('input[type="checkbox"]').on('click', function () {
                $(this).parent('label').css("background-position", this.checked ? "0px -20px" : "0px 0px");
            });
        }
    }

    "use strict";
    $(document).ready(function () {
        vougeAuthValidation.init();

        // Custom register page
        if ($('#signup-content').length) {
            $('body').addClass('wdt-custom-auth-form');
            $('.wrapper').addClass('wdt-custom-auth-form');
        }
    });
})(jQuery);
