
function userheaderHidePrefOptions ()
{
    if ($('header_userpref_link')) {
        $('header_userpref_link').removeClassName('userpref_link_bg');
        $('header_userpref_link').addClassName('dotted');
    }
    if ($('prefoptions')) {
        $('prefoptions').setStyle({display: 'none'});
    }
}

function userheaderHideLoginOptions ()
{
    if ($('header_login_link')) {
        $('header_login_link').removeClassName('login_link_bg');
        $('header_login_link').addClassName('dotted');
    }
    if ($('loginoptions')) {
        $('loginoptions').setStyle({display: 'none'});
    }
}

document.observe('dom:loaded', function (e) {

    // Open the User Prefs popup and close the login popup
    if ($('header_userpref_link')) {

        $('header_userpref_link').observe('click', function () {
            if ($('prefoptions')) {
                if ($('pref_content')) {

                    // Get just the path part of the current request,
                    // in case we have to redirect if the language changes:
                    var len = ALBase.make_uri('/').length;
                    var currentPath = currentRequestURL.substr(len);

                    var url = ALBase.make_uri('/userprefs/'
                                            + AtomicUserId
                                            + '/edit')
                                            + '?current_request_path='
                                            + escape(currentPath);

                    new Ajax.Updater('pref_content', url,
                                     { method: 'post', evalScripts: 'true' }
                    );
                }
                // Delay a bit to let the AJAX update start happening
                // before we pop up the box:
                setTimeout("$('prefoptions').setStyle({display: 'block'})",
                           500);
            }
            $(this).addClassName('userpref_link_bg').removeClassName('dotted');
            userheaderHideLoginOptions();
        });
    }

    // If no cookie is set because it is the first time visiting the page,
    // display the login box for 3 seconds and then fade. This is to show
    // users where it is so they are not confused.
    //
    displaylogin = getCookie('displaylogin');
    if (!(displaylogin != null && displaylogin != "")) {

        if ($('loginoptions')) {
            var ef = Effect.Fade($('loginoptions').setStyle({display: 'block'}), {delay: 3});
            $('loginoptions').observe('click',
                      function (e) {
                        if (ef) ef.cancel();
                        $('loginoptions').setStyle({opacity: 1});
                      }
            );
        }
        setCookie('displaylogin', 'set', 1);
    }
    else {
        if ($('loginoptions')) {
            $('loginoptions').setStyle({display: 'none'});
        }
    }

    // Closes the popups
    $$('div.header_popup div.header_popup_close_button').each(
            function (popup) {
                $(popup).observe('click', function () {
                        $(popup).parentNode.setStyle({display: 'none'});
                    }
                );
            }
    );

    // Login with the enter key
    if ($('user_login_form')) {
        $('user_login_form').observe('keypress', function (event) {
                if (event.keyCode == Event.KEY_RETURN) {
                    $('user_login_form').submit();
                }
            }
        );
    }

    // Open the login popup and close the user prefs popup
    if ($('header_login_link')) {
        $('header_login_link').observe('click', function () {
                userheaderHidePrefOptions();
                if ($('loginoptions')) {
                    $('loginoptions').setStyle({display: 'block'});
                }
                if ($('popupusername')) {
                    $('popupusername').focus();
                }
                $(this).addClassName('login_link_bg').removeClassName('dotted');
            }
        );
    }
});

//----------------------------------------------------------

function userprefsSubmit (my_form)
{
    makeAjaxRequest(
        {
            url: '/userprefs/' + AtomicUserId + '/ajaxsave',
            parameters: $(my_form).serialize(true),
            onSuccess: function (json) {
                if (json.new_url) {
                    document.location.href = json.new_url;
                }
                else {
                    userheaderHidePrefOptions();
                }
            }
        }
    );
}

//----------------------------------------------------------

function userprefsChangePasswordSubmit (form)
{
    var pass1 = $(form.pass1).getValue().strip();
    var pass2 = $(form.pass2).getValue().strip();
    if (pass1.length == 0 || pass2.length == 0)
    {
        alert('Please enter your new password in both fields');
        return false;
    }
    if (pass1 != pass2) {
        alert('New password entries do not match');
        return false;
    }

    makeAjaxRequest(
        {
            url: '/user/' + AtomicUserId + '/ajaxpasswordchange',
            parameters: $(form).serialize(true),
            onSuccess: function (json) {
                al_alert('Your password has been changed.');
            },
            failureMessage: 'The attempt to change your password failed'
        }
    );
}

