﻿Autocompleter.MultiTagSearch=new Class({
	Extends:Autocompleter.Ajax.Json,
/*
	query:function(){
		var data=$extend({},this.options.postData);
		
		var q=this._getLastTagInList();
		if (!q.length) return;
		
		data[this.options.postVar]=q;
		//console.log('askin "'+q+'"');
		this.fireEvent("onRequest",[this.element,this.ajax]);
		this.ajax.send({data:data});
	},

	_getLastTagInList:function () {
		var value=this.element.value;
		var q=value.substring(value.lastIndexOf(" ")+1,value.length);
		return q;
	},

	updateChoices:function(choices) {
		this.queryValue=this._getLastTagInList();
	
		this.choices.empty();
		this.selected=null;
		if (!choices || !choices.length) return;
		if (this.options.maxChoices<choices.length) choices.length=this.options.maxChoices;
		choices.each(this.options.injectChoice || function(choice,i){
			var el=new Element("li").set("html",this.markQueryValue(choice));
			el.inputValue=choice;
			this.addChoiceEvents(el).inject(this.choices);
		},this);
		this.showChoices();
	},

	choiceSelect:function (el) {
		var value=this.element.value;
		// current value not including the last word entered
		var currValue=value.substring(0,value.lastIndexOf(" "));
		// new value contains the last word as selected from tags
		var newValue=(currValue ? currValue+" " : "")+el.inputValue;

		this.observer.value=this.element.value=newValue+" ";
		this.hideChoices();
		this.fireEvent("onSelect",[this.element],20);
	},*/
	showChoices:function() {
		arguments.callee.parent();
		this.resetSelected();
	},

	resetSelected:function () {
		if (this.selected) this.selected.removeClass("autocompleter-selected");
		this.selected=false;
	},

	onCommand:function(e,mouse) {
		if (!this.selected && (e.key=="up" || e.key=="down")) {
			this.choiceOver(e.key=="up" ? this.choices.getLast() : this.choices.getFirst());
			e.stop();
		}
		else if (this.selected && e.key=="up" && !this.selected.getPrevious()) {
			this.resetSelected();
			e.stop();
		}
		else if (this.selected && e.key=="down" && !this.selected.getNext()) {
			this.resetSelected();
			e.stop();
		}
		else arguments.callee.parent(e,mouse);
	}
});

