/**
 * @fileoverview
 * A couple of utility functions to allow user-specific information to be
 * persisted across browser sessions.
 */

/**
 * Loads the current profile data if we don't already have it cached. This
 * shouldn't need to be called outside of this file, but it's available just in
 * case.
 */
jQuery.loadProfile = function() {
    if (!$.profileData) {
        try {
            $.profileData = $.secureEvalJSON($.cookie("profile"));
        }
        catch (e) {
            // That's OK, let's just wipe out the profile data and start
            // afresh.
            $.profileData = {};
            $.cookie("profile", $.toJSON($.profileData), { expires: 365, path: "/" });
        }
    }
};

/**
 * Gets or sets profile data.
 *
 * @param {String} name The name of the profile record to retrieve or set.
 * @param value If set, the new value of the profile record.
 * @return If value is omitted, the current value of the requested profile
 * record.
 */
jQuery.profile = function(name, value) {
    $.loadProfile();
    if (value) {
        $.profileData[name] = value;
        $.cookie("profile", $.toJSON($.profileData), { expires: 365, path: "/" });
    }
    else {
        return $.profileData[name];
    }
};

/* Grab the profile data if it exists and cache it, thereby avoiding
 * unnecessary JSON evals. */
$($.loadProfile);

// vim: set et ts=4 sw=4 cin nopaste:
