var KeysProfileUI = Class.create();

KeysProfileUI.prototype = {
    initialize: function(user_id, send_revoke, sr_init, grant_deny, request, has_private) {
		this.user_id = user_id;
		this.has_private = has_private;

		if (send_revoke || grant_deny) {
			if (sr_init) {
				if (this.has_private) {
					$('send_key').hide();
				}
			}
			else {
				$('revoke_key').hide();
			}
			this.initRevokeKey();
			if (this.has_private) {
				this.initSendKey();
			}
		}

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

		if (request) {
			Event.observe(window, 'load',
				(function (e) {
					this.initRequestKey();
				}).bind(this)
			);
		}
	},

	initRequestKey: function() {
		$$('.request_key').each(
			(function(element) {
				Event.observe(element, 'click', (function(e) {
					Event.stop(e);

					show_hide(new Array('top_err_not_message'), 1);
					show_hide(new Array('loading_top'), 0);

					new Ajax.Request('/index.php?c=keys&a=request&user_id=' + this.user_id + '&ajax=1&ts=' + new Date().getTime(), {
						method:'get',
						onSuccess: (function(transport) {
							var ajax_response = ajax_response_msg(transport.responseText);
						}).bind(this)
					});
				}).bind(this));
			}).bind(this)
		);
	},

	initSendKey: function() {
		Event.observe('send_key', 'click', (function(e) {
			Event.stop(e);
			show_hide(new Array('top_err_not_message'),1);
			show_hide(new Array('loading_top'),0);
			new Ajax.Request('/index.php?c=keys&a=grant&user_id=' + this.user_id + '&ajax=1&ts=' + new Date().getTime(), {
				method:'get',
				onSuccess: (function(transport) {
					var ajax_response = ajax_response_msg(transport.responseText);
					if (ajax_response != "0") {
						$('send_key').hide();
						$('revoke_key').show();
						if ($('grant_deny')) {
							$('grant_deny').remove();
							$('key_request_count').innerHTML = (parseInt($('key_request_count').innerHTML)-1);
						}
					}
				}).bind(this)
			});
		}).bind(this), false);
	},

	initRevokeKey: function() {
		Event.observe('revoke_key', 'click', (function(e) {
			Event.stop(e);
			show_hide(new Array('top_err_not_message'),1);
			show_hide(new Array('loading_top'),0);
			new Ajax.Request('/index.php?c=keys&a=revoke&user_id=' + this.user_id + '&ajax=1&ts=' + new Date().getTime(), {
				method:'get',
				onSuccess: (function(transport) {
					var ajax_response = ajax_response_msg(transport.responseText);
					if (ajax_response != "0") {
						$('revoke_key').hide();
						if (this.has_private) {
							$('send_key').show();
						}
					}
				}).bind(this)
			});
		}).bind(this), false);
	},

	initGrantRequest: function() {
		Event.observe('grant_key', 'click', (function(e) {
			Event.stop(e);
			show_hide(new Array('top_err_not_message'),1);
			show_hide(new Array('loading_top'),0);
			new Ajax.Request('/index.php?c=keys&a=grant&user_id=' + this.user_id + '&ajax=1&ts=' + new Date().getTime(), {
				method:'get',
				onSuccess: (function(transport) {
					var ajax_response = ajax_response_msg(transport.responseText);
					if (ajax_response != "0") {
						$('revoke_key').show();
						$('send_key').hide();
						$('grant_deny').remove();
						$('key_request_count').innerHTML = (parseInt($('key_request_count').innerHTML)-1);
					}
				}).bind(this)
			});
		}).bind(this), false);
	},

	initDenyRequest: function() {
		Event.observe('deny_key', 'click', (function(e) {
			Event.stop(e);
			show_hide(new Array('top_err_not_message'),1);
			show_hide(new Array('loading_top'),0);
			new Ajax.Request('/index.php?c=keys&a=deny&user_id=' + this.user_id + '&ajax=1&ts=' + new Date().getTime(), {
				method:'get',
				onSuccess: (function(transport) {
					var ajax_response = ajax_response_msg(transport.responseText);
					if (ajax_response != "0") {
						$('revoke_key').hide();
						if (!this.has_private) {
							$('send_key').hide();
						}
						$('grant_deny').remove();
						$('key_request_count').innerHTML = (parseInt($('key_request_count').innerHTML)-1);
					}
				}).bind(this)
			});
		}).bind(this), false);
	}
};
