/**
 * File:    SFC.Login.js
 * Author:  RP =)
 */

/**
 * SFC.Guestbook class handles all stuff regarding the guestbook.
 */
SFC.Login = (function() {
    return {

        /**
         * Login entry point called to setup the guestbook page.
         * @param options
         */
        setup: function(options) {
            var that = this;     /* {SFC.Login} 'this' reference for inside of inner functions */

            /* Setup our form submit. We'll submit the form thru ajax.
             * util.guestbook.php returns the list of guestbook entries via json.
             * We can use this data to re-display our guestbook list. Form is validated
             * before submitted. Validations are done in jquery.validate.js
             */
            $("#loginForm").validate({
                submitHandler: function(form) {
                    $("#loginForm").ajaxSubmit({
                        url: "lib/util.checklogin.php",
                        data: {
                            func: "login"
                        },
                        beforeSubmit: function() {
                            SFC.Container.toggleFormElements($("#loginForm"), true);
                        },
                        success: function(res) {
                            $("#loginForm").resetForm();
                            SFC.Container.toggleFormElements($("#loginForm"), false);
                            window.location.href = "home.php";
                        }
                    });
                    return false;
                }
            });
        }

    };
}());