

function UserAuth(options)
{
	this.options = {
		dialog_container: "#auth-dialog"
	};

	if (options) {
		$.extend(this.options, options);
	}

	var self = this;
	$(this.options.dialog_container + " .auth-dialog-close").click(function(){
		$(self.options.dialog_container).dialog("destroy");
	});

	this.ShowAuthWindow = function()
	{
		var dialog_params =
		{
			width: 500,
			height: 400,
			modal: true,
			dialogClass: "auth-dialog-container"
		};
		$(this.options.dialog_container).dialog(dialog_params);
		$(this.options.dialog_container).dialog("open");
	};

	this.CloseAuthWindow = function()
	{
		
	};
}

function UserChooser()
{
	this.actions_path = "/common/actions/users.php";
	this.cache = {};
	this.min_length = 2;
	
	this.Init = function(container_name)
	{
		var obj = this;
		$(container_name).bind( "keydown", function( event ) {
			if ( event.keyCode === $.ui.keyCode.TAB &&
					$( this ).data( "autocomplete" ).menu.active ) {
				event.preventDefault();
			}
		})
		.autocomplete({
			minLength: obj.min_length,
			source: function( request, response ) {				
				var term = request.term;
				if ( term in obj.cache ) {
					response( obj.cache[ term ] );
					return;
				}

				$.ajax({
					url: obj.actions_path,
					type: "POST",
					dataType: "json",
					data: {
						action: "user-search",
						hash: global_hash,
						login: request.term
					},
					success: function(data) {
						obj.cache[ term ] = data.Users;
						response( data.Users );
					}
				});
			},
			focus: function( event, ui ) {
				return false;
			},
			select: function( event, ui ) {
				$(container_name).val(ui.item.login);
				return false;
			}
		}).data("autocomplete")._renderItem = function( ul, item ) {
			return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a>" + item.login + "</a>" )
			.appendTo( ul );
		};
	};
	
}
