function suggest(properties)
{
	this.properties = properties;
	
	if(typeof this.properties.show == 'undefined')
	{
		this.properties.show = 10;	
	}
	
	var that = this;
	
	this.current = null;
	this.result = 0;
	
	this.input = document.getElementById(this.properties.id);
	
	if(!this.input || this.input.tagName.toLowerCase() != 'input')
		return false;
	
	this.suggest = document.createElement('div');
	this.suggest.className = 'suggest';
	this.suggest.style.display = 'none';
	this.suggest.style.position = 'absolute';
	this.suggest.style.overflow = 'hidden';
	
	this.optionMouseOver = function()
	{
		if(that.current !== null && that.current !== false)
		{
			that.suggest.childNodes[that.current].className = 'suggest_option';		
		}
		
		that.current = this.title;
		that.suggest.childNodes[that.current].className = 'suggest_option_current';	
	}
	
	this.optionClick = function()
	{
		that.keyup({keyCode:13});
	}
	
	for(var s=0;s<this.properties.show;s++)
	{
		var option = document.createElement('div');
		option.className = 'suggest_option';
		option.innerHTML = s;
		option.onmouseover = that.optionMouseOver;
		option.onclick = that.optionClick;
		option.title = s;
		this.suggest.appendChild(option);
	}
	
	this.input.parentNode.insertBefore(this.suggest, this.input);
	
	/* Callback */
	
	this.ajaxCallback = function(response)
	{
		that.input.style.backgroundColor = '#FFFFFF';
		
		if(response.xml == null)
		{
		  that.current = null;
			that.input.style.backgroundColor = '#990000';
			return;			
		}
		
		var elements = response.xml.getElementsByTagName("elements")[0].getElementsByTagName('element');
		
		if(elements.length == 0)
		{
		  that.current = null;
			that.input.style.backgroundColor = '#990000';
			return;		
		}
		
		that.suggest.style.display = 'block';
		
		var top = 0;
		var left = 0;
		var parent = that.input;
		do
		{
			top += parent.offsetTop;
			left += parent.offsetLeft;
			parent = parent.offsetParent;
		}
		while(parent && parent.tagName && parent.tagName.toLowerCase() != 'body');
		
		that.suggest.style.width = that.input.offsetWidth + 'px';
		that.suggest.style.left = left + 'px';
		that.suggest.style.top = top + that.input.offsetHeight + 'px';
		
		that.result = elements.length;
		
		that.current = false;
		/* that.suggest.childNodes[0].className = 'suggest_option_current'; */
		
		that.suggest.style.backgroundColor = '#FFFFFF';
		
		for(s=0;s<that.properties.show;s++)
		{
			if(s >= elements.length)
			{
				that.suggest.childNodes[s].style.display = 'none';
				continue;			
			}
			
			that.suggest.childNodes[s].style.display = 'block';
			
			that.suggest.childNodes[s].innerHTML = elements[s].firstChild.data;
			that.suggest.childNodes[s].tag = elements[s].getAttribute("key");
			
			if(elements[s].getAttribute("proper") == '1')
			{
			  that.suggest.childNodes[s].className = 'suggest_option_current';
			  that.current = s;
			}
			else
			{
			  if(that.current === s)
			    that.current = false;
			  that.suggest.childNodes[s].className = 'suggest_option';
			}
			//  elements[s].attributes[0].firstChild.data
		}
		
		that.input.focus();
	}
	
	this.keyup = function(event)
	{
		var event = event || window.event;
		
		switch(event.keyCode)
		{
			case 38 : // arrow up
				if(that.current === null)
					break;
				
				if(that.current === false)
				  break;
				
				if(that.current > 0)
				{
					that.suggest.childNodes[that.current].className = 'suggest_option';
					that.current--;
					that.suggest.childNodes[that.current].className = 'suggest_option_current';	
				}
			break;
			case 40 : // arrow down
				if(that.current === null)
					break;
				
				if(that.current === false)
				{
				  that.current = 0;
				  that.suggest.childNodes[that.current].className = 'suggest_option_current';	
				  break;
				}
				
				if(that.current < that.result - 1)
				{
					that.suggest.childNodes[that.current].className = 'suggest_option';
					that.current++;
					that.suggest.childNodes[that.current].className = 'suggest_option_current';	
				}
			break;
			case 13 : // ENTER
			 // typeof event.mouseDown == 'undefined' ||
				if((that.current === null || that.current === false) && event.mouseDown == true)
				{
					if(that.input.value.length >= 2)
					{
					  var request = new window.ajaxRequest({url:that.properties.ajaxURL + '?add=' + encodeURIComponent(that.input.value) + (typeof that.properties.moduleId == 'undefined' ? '' : '&module_id=' + that.properties.moduleId),type:'GET',callback:that.ajaxCallback});
					  request.startRequest();
					}
					
					break;
				}
				
				if(that.current === null)
				  break;
				
				if(that.current === false)
				  that.current = 0;
				
				that.suggest.childNodes[that.current].className = 'suggest_option';
				that.suggest.style.display = 'none'; 
				
				if(typeof that.properties.onReturn == 'function' && typeof that.input.tag == 'undefined')
				{
					that.properties.onReturn(that.suggest.childNodes[that.current].tag, that.suggest.childNodes[that.current].firstChild.data);
				}
				
				if(typeof event.type != 'undefined')
					that.current = null;
			break;			
			default : 
				if(that.current != null && that.current != false)
				{
					that.suggest.childNodes[that.current].className = 'suggest_option';
					that.current = null;
				}
					
				that.suggest.style.display = 'none';
				
				if(that.input.value.length < 2)
				{
				  this.current = null;
					that.input.style.backgroundColor = '#FFFFFF';
					break;
				}
				
				var request = new window.ajaxRequest({url:that.properties.ajaxURL + '?input=' + encodeURIComponent(that.input.value) + (typeof that.properties.moduleId == 'undefined' ? '' : '&module_id=' + that.properties.moduleId),type:'GET',callback:that.ajaxCallback});
		    request.startRequest();
			break;
		}
		
		return false;
	}
	
	this.input.onkeydown = function(event) { var event = event || window.event; if(event.keyCode == 13) { that.keyup({keyCode:13,mouseDown:true}); return false; } }
	this.input.onkeyup = this.keyup;
}
