/**
 * Отвечает за приглашение пользователей в чат
 */
inviteToChatEventsObserver = Class.create({
	initialize: function(observer) {
		this.inviteButtonId = 'show_users_you_can_invite'; 
		this.listContainerId = 'users_you_can_invite_list';
		this.observer = observer;
		this.init();
	},

	init: function() {
		this.observer.on('settings.onSuccess', function(c) {
			$(this.inviteButtonId).observe('click', this.toggleUsersList.bind(this));
			this.listContainer = $(this.listContainerId); 
		}.bind(this));
	},

	toggleUsersList: function (e) {
		if (e) {
			e.preventDefault();
		}
		if (this.listContainer.offsetHeight == 0) {
			new Ajax.Request('/jabber/getUsersYouCanInvite/', {
				onComplete: function(response) {
					var text = response.responseText;
					this.listContainer.update(text);
					$A(this.listContainer.getElementsByClassName("chat-invite-link")).each(function(el) {
						//el.observe('click', this.inviteUser.bind(this, el.previous().getValue()));
						el.observe('click', function (e){
							this.inviteUser(el.previous().getValue());
							Event.stop(e);
						}.bind(this));
					}.bind(this));
					this.listContainer.show();
				}.bind(this)
			});
		} else {
			this.listContainer.hide();
		}
	},

	inviteUser: function (jid) {
		this.observer.fire('inviteToChat.invite', jid);
	}
});
