var wv = {User: {}};

(function($) {

	wv.User.initAutocomplete = function(element) {
		element = $(element);

		element.autocomplete({
			url:            'index.php',
			paramName:      'name',
			extraParams:    {page: 'userautocomplete', func: 'autocomplete'},
			maxCacheLength: 50,
			matchContains:  true,
			resultsClass:   'devutils-ac-results',
			onItemSelect:   function(item) {
				element.prev().val(item.data[0]);
			},
			useCache:       false
		});
	};

	$(function() {
		$('input.user_autocomplete').each(function() {
			wv.User.initAutocomplete(this);
		});

		$('form:has(input.user_autocomplete)').submit(function() {
			$('input.user_autocomplete', this).each(function() {
				var input = $(this);
				if (!input.val().length) {
					input.prev().val('');
				}
			});
		});
	});

	$('.form-multiple-row').live('rowAdded', function() {
		wv.User.initAutocomplete($('input.user_autocomplete', this));
	});

})(jQuery);
