SITE.login = function() {

    var $container = null,
        $form = null,
        valid = true;

    /**
     * Checks whether or not the passed input is empty
     * 
     * @author gabrielh
     * @param {Object} $input The input you want to have checked if empty
     * @returns {Boolean} A boolean where true equals an empty input, false equals an input with a value
     */
    var isEmpty = function( $input ) {
        return ( '' === $input.val() ) ? true : false;
        //return false;
    };

    /**
     * Stores associated objects and data to the form object and checks form validity
     * 
     * @author gabrielh
     * @returns {Boolean} A true boolean value, if form is valid, false boolean value if form is invalid
     */
    var prePost = function() {

        valid = true;

        //-- Attach the fields to the form object
        $form.$username = $form.find('input[name=username]');
        $form.$password = $form.find('input[name=password]');
        $form.$error = $form.find('div.error');
        $form.$header = $form.siblings('h1');

        //-- Store data to associated objects
        $form.$header.data('headerText', $form.$header.html());
        $form.$header.data('loadingText', CONFIG.loginProcessingText);
        $form.$password.data('errorWrapper', $('<div class="error"></div>').hide());
        $form.$password.data('errorText', CONFIG.loginInvalidNotificationText);

        //-- Create the hide and show methods on the header
        $form.$header.slideOut = function( callback ) {
            $form.$header.hide('slide', {
                direction : CONFIG.loginValidSlideOutDirection
            }, CONFIG.loginValidSlideTime, function() {
                if ( 'function' === typeof(callback) ) {
                    callback();
                }
            });
        };
        $form.$header.slideIn = function( data, callback ) {

            if ( data.redirect ) {

                SITE.login.redirect(data.url);
            
            } else {
                if($.browser.msie){SITE.login.redirect('/')}
                else{
                //-- Update the container content
                $container.html(data.html);
                

                //-- Setup the placeholders if any
                SITE.placehold();

                //-- Display the returned HTML
                $container.show('slide', {
                    direction : CONFIG.loginValidSlideInDirection
                }, CONFIG.loginValidSlideTime, function() {
                    if ( 'function' === typeof(callback) ) {
                        callback();
                    }
                });
                }

            }

        };

        //-- Validate form
        if ( isEmpty( $form.$username ) || isEmpty( $form.$password ) ) {
            valid = SITE.login.displayError();
        }

        return valid;

    };

    /**
     * Determines what to do after a login
     * 
     * @author gabrielh
     */
    var handleLogin = function() {
        //-- Reinitiate if the login form is found again
        //$container = $('div#dvPageHeaderLogin');
        if ( $container.find('#' + $form.attr('id')).length ) {
            SITE.login.init();
            
        //-- Assume valid login and redirect
        } else {
            SITE.login.redirect(CONFIG.loginValidForwardUrl);
        }
    };

    return {

        /**
         * This will either append an error or animate the existing error for a visual call to action
         * 
         * @author gabrielh
         * @returns {Boolean} A false boolean value
         */
        displayError : function() {

            //-- Shake the error to call the users attention to the notification
            if ( $form.$error.length ) {
                $form.$error
                    .html($form.$password.data('errorText'))
                    .effect('shake', {
                        times : CONFIG.loginInvalidShakeRepeat,
                        distance : CONFIG.loginInvalidShakeDistance
                    }, CONFIG.loginInvalidShakeTime);

            //-- Append the error to the DOM if not displayed already
            } else {
                $form.append(
                    $form.$password.data('errorWrapper')
                        .html($form.$password.data('errorText'))
                        .fadeIn(CONFIG.loginInvalidFadeTime)
                );
            }

            return false;

        },

        /**
         * Redirects the user after a valid login
         * 
         * @author gabrielh
         */
        redirect : function( url ) {
            window.location.href = url;
        },

        /**
         * Posts the login form and checks for a valid login
         * 
         * @author gabrielh
         * @context The form that is being submitted
         * @returns {Boolean} A false boolean value
         */
        login : function() {

            if ( prePost() ) {

                //-- Remove the error
                $form.$error.remove();

                //-- Hide the inputs and animate to loading
                $form.find('input').hide();
                $form.$header
                    .html($form.$header.data('loadingText'))
                    .toggleClass('loading', CONFIG.loginAnimateTime,
                        function() {
                            $.ajax({
                                type : $form.attr('method'),
                                url : $form.attr('action'),
                                cache: false,
                                data : $form.serialize(),
                                dataType : 'json',
                                //contentType: "application/json",
                                success : function( data, other, other2) {
                                    $form.$header.slideOut( $form.$header.slideIn( data, handleLogin ) );
                                },
                                error : function(){}
                            });
                        }
                    );

            }

            return false;

        },

        /**
         * Sets up the login form behavior
         * 
         * @author gabrielh
         */
        init : function() {

            //-- Store the private variables
            $container = $('div#dvPageHeaderLogin');
            $form = $container.find('form#frmLogin');

            insteadOfThis = this;
            //-- On form submit
            $form.submit(function(evt){
                evt.preventDefault();
                insteadOfThis.login();
            });

        }

    };

} ();
$(document).ready(
    function() {
        SITE.login.init();
    }
);
