﻿/// <reference path="../spider/spider.validation.js" />

site.account = function (settings) {
    var _settings = {
        logInFormId: 'loginform',
        createAccountFormId: 'createaccountform',
        forgotPasswordFormId: 'forgotpasswordform'
    };

    if (settings) {
        $.extend(_settings, settings);
    }

    return {
        initialize: function () {
            this.bindLogInForm();
            this.bindCreateAccountForm();
            this.bindLogOut();
            this.bindInitializeToggle();
            this.bindForgotPassword();
        },

        bindInitializeToggle: function () {
            $('[data-toggle]').each(this.toggleDivs);
        },

        toggleDivs: function () {
            var t = $(this);
            var o = '#' + t.attr('data-toggle');
            $(o).css('display', 'none');
            $(this).click(function () {
                $(o).fadeToggle('fast');
                return false;
            });
        },

        bindLogOut: function () {
            $('a.logout').click(function (e) {
                e.preventDefault();
                $.post('/account/logout', function (d) {
                    document.location = document.location;
                })
            });
        },

        bindLogInForm: function () {
            $('#' + _settings.logInFormId).submit(function (e) {
                var validate = validation({ formId: _settings.logInFormId });
                if (validate.checkRequired()) {
                    $.post('/account/login', $('#' + _settings.logInFormId).serialize(), function (d) {
                        document.location = '/account';
                    })
                }
                return false;
            });
        },

        bindForgotPassword: function () {
            $('#' + _settings.forgotPasswordFormId).submit(function () {
                var validate = validation({ formId: _settings.forgotPasswordFormId });
                if (validate.checkRequired()) {
                    $.post('/account/forgotuserpassword', $('#' + _settings.forgotPasswordFormId).serialize(), function (d) {
                        if (d.Success) {
                            $('#forgotpasswordform').remove();
                        }
                        $('#forgotpassword-message').html(d.Message);
                    })
                }
                return false;
            });
        },

        bindCreateAccountForm: function () {
            $('#' + _settings.createAccountFormId).submit(function (e) {
                e.preventDefault();
                var validate = validation({ formId: _settings.createAccountFormId });
                if (validate.checkRequired()) {
                    $.post('/account/createuseraccount', $('#' + _settings.createAccountFormId).serialize(), function (d) {
                        if (d.Success) {
                            document.location = '/account/index';
                        } else {
                            var messageString = '';
                            $(d.Messages).each(function (i, e) {
                                if (!e.Success) {
                                    messageString += e.Message + '<br />';
                                }
                            })
                            $('#createaccount-message').html(messageString);
                        }
                    })   
                }
                return false;
            });
        }
    }
}

$(document).ready(function () {
    var account = site.account().initialize();
})
