function KeysProfileUI(user_id, send_revoke, sr_init, grant_deny, has_private) {
    this.user_id = user_id;
    this.has_private = has_private;

    if (send_revoke || grant_deny) {
        if (sr_init) {
            if (this.has_private) {
                jQuery('#send_key').hide();
            }
        }
        else {
            jQuery('#revoke_key').hide();
        }

        this.initRevokeKey();

        if (this.has_private) {
            this.initSendKey();
        }
    }

    if (grant_deny && jQuery('#grant_deny').length) {
        this.initGrantRequest();
        this.initDenyRequest();
        this.grant_deny = true;
    }
    else {
        this.grant_deny = false;
    }

    jQuery(jQuery.proxy(this.initRequestKey, this));
};

KeysProfileUI.prototype = {
    initRequestKey: function() {
        jQuery('.request_key').click(jQuery.proxy(function() {
            show_hide('#top_err_not_message', true);
            show_hide('#loading_top', false);

            jQuery.ajax({
                url: '/keys/request?method=ajax',
                data: { user_id: this.user_id, ajax: 1, ts: (new Date()).getTime() },
                dataType: 'json',
                success: function(data) {
                    if (data.content) {
                        jQuery('#top_err_not_message').html(data.content);
                    }
                }
            });

            return false;
        }, this));
    },

    initSendKey: function() {
        jQuery('#send_key').click(jQuery.proxy(function() {
            show_hide('#top_err_not_message', true);
            show_hide('#loading_top', false);

            jQuery.ajax({
                url: '/keys/grant?method=ajax',
                data: { user_id: this.user_id, ajax: 1, ts: (new Date()).getTime() },
                dataType: 'json',
                success: function(data) {
                    if (data.content) {
                        jQuery('#top_err_not_message').html(data.content);
                        jQuery('#send_key').hide();
                        jQuery('#revoke_key').show();
                        if (jQuery('#grant_deny').length) {
                            jQuery('#grant_deny').remove();
                            jQuery('#key_request_count').html(parseInt(jQuery('#key_request_count').text(), 10) - 1);
                        }
                    }
                }
            });

            return false;
        }, this));
    },

    initRevokeKey: function() {
        jQuery('#revoke_key').click(jQuery.proxy(function() {
            show_hide('#top_err_not_message', true);
            show_hide('#loading_top', false);

            var successHandler = jQuery.proxy(
                function(data) {
                    if (data.content) {
                        jQuery('#top_err_not_message').html(data.content);
                        jQuery('#revoke_key').hide();
                        if (this.has_private) {
                            jQuery('#send_key').show();
                        }
                    }
                }, this);

            jQuery.ajax({
                url: '/keys/revoke?method=ajax',
                data: { user_id: this.user_id, ajax: 1, ts: (new Date()).getTime() },
                dataType: 'json',
                success: successHandler
            });

            return false;
        }, this));
    },

    initGrantRequest: function() {
        jQuery('#grant_key').click(jQuery.proxy(function(){
            show_hide('#top_err_not_message', true);
            show_hide('#loading_top', false);

            jQuery.ajax({
                url: '/keys/grant?method=ajax',
                data: { user_id: this.user_id, ajax: 1, ts: (new Date()).getTime() },
                dataType: 'json',
                success: function(data) {
                    if (data.content) {
                        jQuery('#top_err_not_message').html(data.content);
                        jQuery('#revoke_key').show();
                        jQuery('#send_key').hide();
                        jQuery('#grant_deny').remove();
                        jQuery('#key_request_count').html(parseInt(jQuery('#key_request_count').text(), 10) - 1);
                    }
                }
            });

            return false;
        }, this));
    },

    initDenyRequest: function() {
        jQuery('#deny_key').click(jQuery.proxy(function() {
            show_hide('#top_err_not_message', true);
            show_hide('#loading_top', false);

            var successHandler = jQuery.proxy(
                function(data) {
                    jQuery('#top_err_not_message').html(data);
                    jQuery('#revoke_key').hide();
                    if (!this.has_private) {
                        jQuery('#send_key').hide();
                    }
                    jQuery('#grant_deny').remove();
                    jQuery('#key_request_count').html(parseInt(jQuery('#key_request_count').text(), 10) - 1);
                }, this);

            jQuery.ajax({
                url: '/keys/deny?method=ajax',
                data: { user_id: this.user_id, ajax: 1, ts: (new Date()).getTime() },
                dataType: 'json',
                success: successHandler
            });

            return false;
        }, this));
    }
};

