$(function () {
    function activateAccountSettingButton(button)
    {
        var url = "/ajax/accounts/settings/"
        var box = $('#modal .modal_content')
        
        function submit()
        {
            var form = box.find("form")
            
            $.post(url, form.serialize(), function(data, status) {
                if (data=="")
                {
                    hideModal()
                }
                else
                {
                    box.html(data)
                    box.find("button").click(submit)
                }
            })
            
            return false
        }
        
        button.click(function() {
            if (settings.AUTHENTICATED)
            {
                showModal(url, function() {
                    box.find("button").click(submit)
                })
            }
            else
            {
                document.location = "/accounts/login/?next=" + document.location
            }
            
            return false
        })
    }
    
    function activateAccountSettingButtons(root)
    {
        var buttons = root.find(".account_settings")
        buttons.each(function () {
            activateAccountSettingButton($(this))
        })
    }
    
    activateAccountSettingButtons($(document))
    activateFunctions.push(activateAccountSettingButtons)
})
