var ContactSearch = Class.create({	
	
	initialize: function(section, args) 
	{
		this.contacts = [];
		this.remove = [];
		
		args = args || {};
		this.callback = args.callback;
		
		if (!section) return;

		try
		{
			this.section = $(section);		
			this.contactList = this.section.select('.autocomplete_list').first();
			this.field = this.section.select('input').first();		
			this.chooser = $('contactsChooser');
		} catch(e) { return; }

		this.contactList.select('.user').each(function(user) {
			var id = user.id.substr(this.section.id.length);
			if (this.contacts.indexOf(id) != -1) return;

			this.contacts.push(id);					
			this.remove = this.remove.without(id);
			this.setupContact(user, id);
		}.bind(this));

		this.field.onkeydown = function(e) {
			this.selectPrevious(e);
		}.bindAsEventListener(this);
		
		this.field.onkeypress = function(e) {
			this.selectPrevious(e);
		}.bindAsEventListener(this);
		
		Event.observe(document, 'keydown', this.deleteSelected.bindAsEventListener(this));
		Event.observe(document, 'keypress', this.deleteSelected.bindAsEventListener(this));
		Event.observe(document, 'click', this.clearSelected.bindAsEventListener(this));
				
		this.section.select('.field').first().onclick = function(e) {
			if (!Event.element(e).hasClassName('user'))
				this.field.focus();
		}.bindAsEventListener(this);
				
		var callback = function(x, entry) 
		{ 
			var params = { ids: this.contacts.toString() };
			return entry + '&' + $H(params).toQueryString();
		}.bind(this);
		
		new Ajax.Autocompleter(this.field, this.chooser, '/users/', { 
			paramName: 'user',
			callback: callback,
			parameters: $H({ excludeUser: args.excludeUser }).toQueryString(),
			updateElement: function(li) { this.addContact(li) }.bind(this)
		});		
	},
	
	empty: function()
	{
		return (this.contacts == 0);
	},
	
	setupContact: function(user, id)
	{
		if (!user) return;
		
		var del = user.select('a.delete').first();
		if (del) del.onclick = this.deleteCurrent.bindAsEventListener(this, id);
		user.onclick = this.selectCurrent.bindAsEventListener(this);		
	},
	
	addContact: function(li)
	{
		var id = li.id.substr(6);
		if (this.contacts.indexOf(id) != -1) return;
		
		var name = li.select('.name').first();
		this.contacts.push(id);
		this.remove = this.remove.without(id);		
		this.contactList.insert('<div id="'+this.section.id+id+'" class="user">'+name.innerHTML.stripTags()+' <a class="delete" href="#">X</a></div>');

		var user = this.contactList.select('.user').last();
		this.setupContact(user, id);
		
		if (this.callback) this.callback(li);
		
		this.field.value = '';
		this.field.focus();		
	},
	
	selectCurrent: function(e)
	{
		if (this.selected)
			this.selected.removeClassName('selected');

		var user = Event.element(e);
		this.selected = user;
		
		user.addClassName('selected');
		this.field.blur();		
	},
	
	deleteCurrent: function(e, id)
	{
		Event.stop(e);
		this.contacts = this.contacts.without(id);
		if (this.remove.indexOf(id) == -1) this.remove.push(id);
		Event.element(e).parentNode.remove();
		this.field.focus();		
	},
	
	selectPrevious: function(e)
	{
		var keycode = e.charCode || e.keyCode;
		var input = Event.element(e);

		if (keycode && keycode == 8 && input.value == '')
		{
			var user = this.contactList.select('div.user').last();
			if (user)
			{
				this.selected = user;
				
				user.addClassName('selected');
				this.field.blur();				
			}
			Event.stop(e);
		}
	},
	
	deleteSelected: function(e)
	{
		var keycode = e.charCode || e.keyCode;

		if (keycode && (keycode == 8) && !(Event.element(e) && Event.element(e).value))
		{
			Event.stop(e);

			if (this.selected) {
				if (!this.selected.select('a.delete').first()) return;

				var id = this.selected.id.substr(this.section.id.length);						
				this.contacts = this.contacts.without(id);
				if (this.remove.indexOf(id) == -1) this.remove.push(id);

				this.selected.remove();						
				delete this.selected;
				this.field.focus();
			}			
		}			
	},
	
	clearSelected: function(e)
	{
		if (this.selected && Event.element(e) != this.selected)
		{
			this.selected.removeClassName('selected');
			delete this.selected;
		}
	}	
});