
var Completer = new Class({
	
	obj: null,
	obid: null,
	suggerimenti: null,
	timer: null,
	choosing: false,
	foc: null,
	
	statusSuggerimenti: function(status){
		if(status){
			this.suggerimenti.setStyle('display','block'); 
		}else{
			this.suggerimenti.empty();
			this.suggerimenti.setStyle('display','none');
			this.foc = null;
		}
		
	},
	
	setFocus: function(newfoc){

		if(!newfoc) return;


		if(!this.foc){
			this.foc = newfoc;
		}else{
//			$('body').appendText('|'+this.foc.getText());
			this.foc.removeClass('sel');
			this.foc = newfoc;
		}
		
		this.foc.addClass('sel');
		
		suggh = this.suggerimenti.getCoordinates();
		focpos = this.foc.getCoordinates();
		 //focpos.bottom; //(focpos.top-suggh.top);
		if((focpos.bottom-this.suggerimenti.scrollTop) > suggh.bottom){
			this.suggerimenti.scrollTop += focpos.height;	
		}
		
		if(focpos.top-this.suggerimenti.scrollTop < suggh.top){
			this.suggerimenti.scrollTop -= focpos.height;	
		}
	},
	
	initialize: function(id){
		
		this.obj = $(id);
		this.obid = id.match(/tappa_(.*)_comune/)[1];
		this.suggerimenti = $('sugg_'+this.obid);
		
		var parentCompleter = this;

		this.obj.addEvent('keydown',function(e){
			var e = new Event(e);
			
			if(e.key.test(/[0-9]+/)){
				e.stop();
				return;
			}
			
			if(e.key == 'enter'){
				e.stop();
				parentCompleter.foc.fireEvent('mousedown');
			}
		});
		
		this.obj.addEvent('keyup',function(e){
			
			var e = new Event(e);
			
			switch(e.key){
				
				case 'esc':
					parentCompleter.statusSuggerimenti(false);
				break;
				
				case 'up':
					parentCompleter.setFocus(parentCompleter.foc.getPrevious());
				break;
				case 'down':
					parentCompleter.setFocus(parentCompleter.foc.getNext());
				break;
				
				case 'enter':
					parentCompleter.foc.fireEvent('mousedown');
				break;
				
				default:
					
					obval = this.getValue();
					
					var fx = function(){						
						if(obval.length > 2){
							new Ajax('index_ajax.php?do=esccomune2&search='+obval,{
								method: 'get',
								update: parentCompleter.suggerimenti,
								onComplete: function(){
									parentCompleter.statusSuggerimenti(true);
									
									parentCompleter.foc = null;
									parentCompleter.setFocus(parentCompleter.suggerimenti.getFirst());
									
									parentCompleter.suggerimenti.getChildren().addEvent('mouseenter',function(){
										parentCompleter.setFocus(this);
									});
									
									parentCompleter.suggerimenti.getChildren().addEvent('mousedown',function(){
										
										if(this.getTag() != 'a') return;
										
										var cap = $('tappa_'+parentCompleter.obid+'_cap'),
											capid = $('tappa_'+parentCompleter.obid+'_idcap'),
											mappid = $('tappa_'+parentCompleter.obid+'_mappa');
//document.title += this.id+'|';
										parentCompleter.obj.value = this.getText().substring(0,this.getText().length-8);
										
										var mc = this.getProperty('rel').match(/(.*)\|(.*)/);

										cap.value = mc[1];
										capid.value = this.id;
										mappid.value = mc[2];
										
										parentCompleter.statusSuggerimenti(false);
										
										parentCompleter.obj.fireEvent('blur');
										cap.fireEvent('blur');
									});
								}
							}).request();
						}
					}
					
					$clear(parentCompleter.timer);
					parentCompleter.timer = fx.delay(400);
				break;
			}
		});
		
		this.obj.addEvent('blur',function(){
			if(!parentCompleter.choosing)
				parentCompleter.statusSuggerimenti(false);
		});
		
		this.suggerimenti.addEvent('mouseenter',function(){
			parentCompleter.choosing = true;
		});
		
		this.suggerimenti.addEvent('mouseleave',function(){
			parentCompleter.choosing = false;
		});
		
		
	}
						  
						  
});