function short_mixture_search_init(){
	var all_input=document.getElementsByTagName("input");
	var already = new Array();
	for(var i=0;i<all_input.length;i++){
		if(all_input[i].className=='shms_inp' && !already[all_input[i]]){
			already[all_input[i]] = 1;
			short_mixture_search(all_input[i]);
		}
	}
	delete all_input;
}

addEvent(window,"load",short_mixture_search_init,false);

short_mixture_search = function (obj){
	
	var self;
	
	var tcons = function (obj){
		
		self=this;
		
		this.input = obj;
		this.container = this.tag('div','');
		this.width = this.input.clientWidth;
		this.left = this.input.left;
		this.script = obj.getAttribute('script');
		this.action = this.input.form.action;
		this.input.onblur = this.on_blur;
		this.input.onfocus = this.on_focus;
		
		this.input.form.onsubmit = this.on_choose;
		
		this.data = new Array();
		this.prev = '';
		
		this.container.className = 'popup_container';
		this.container.style.display = 'none';
		this.container.style.overflow = 'hidden';
		this.container.style.position = 'absolute';
		this.container.style.zIndex = '10001';
		this.container.style.zoom = '1';
		
		var l = 0;
		var t = 0;
		var elem = this.input;
		while (elem){
			l += elem.offsetLeft;
			t += elem.offsetTop;
			elem = elem.offsetParent;
		}


		this.container.style.width = this.width+'px';
		this.container.style.left = l+'px';
		this.container.style.top = t+this.input.offsetHeight+'px';
		
		this.current = 0;
		
		this.input.parentNode.insertBefore(this.container,this.input.nextSibling);
	
		this.input.onkeyup = this.on_keyup;
		
		if(!JSON){
			alert('can\'t find JSON object');
			return false;			
		}

		this.init();
	}
	
	tcons.prototype={
		get_object : function(e){
			if (!e){e = window.event;}
			return window.event ? e.srcElement : e.currentTarget;
		},
		tag : function (type,classname){var el = document.createElement(type); el.className = classname; return el;},
		init : function (){
					
		},
		hide : function(){
			self.container.style.display = 'none';
		},
		show : function(){
			if(self.data.length){
				self.container.style.display = 'block';
			}
		},
		clear_list : function(){
			var i;

			// удаляем содержимое
			if(self.data.length){
				for(i in self.data){
					self.container.removeChild(self.data[i]);
				}
			}	
			
			self.data = [];
		},
		make_data : function (data){
			var i;
			var temp;
			
			self.clear_list();

			self.current = 0;
			var k = 0;
			// заполняем полученным содержимым
			for(i in data){
				self.data[k] = self.tag('div','');
				self.data[k].className = 'popup_inactive';
				self.data[k].elid = i;
				self.data[k].elpos = k;
				self.data[k].onmousemove = self.on_mousemove;
				self.data[k].onmousedown = self.on_click;
				self.data[k].innerHTML = data[i].val;
				self.container.appendChild(self.data[k]);
				k++;
			}
			
			self.set_active(0,false);
			self.show();
		},
		on_choose : function(){
			self.set_action();
			document.location = self.input.form.action;
			return false;
		},
		on_focus : function(){
			self.input.select();
			self.key_process(0);
		},
		on_blur : function (){
			self.clear_list();
			self.hide();
		},
		on_click : function (e){
			self.on_choose();
		},
		on_mousemove : function (e){
			var temp = self.get_object(e);
			
			if(self.current != temp.elpos){
				self.set_active(temp.elpos,true);
			}
		},
		on_keyup : function(e){
			
			if (!e) var e=window.event;
			self.key_process(e.keyCode);

		},
		key_process : function(code){
			switch(code){
				case 38:// нажата клавиша вверх
					if(self.data.length == 0){
						return;
					}
					if(self.current-1 < 0){
						self.set_active(self.data.length-1,true);
					}else{
						self.set_active(self.current-1,true);
					}
				break;
				case 40:// нажата клавиша вниз
					if(self.data.length == 0){
						return;
					}
					if(self.current+1 >= self.data.length){
						self.set_active(0,true);
					}else{
						self.set_active(self.current+1,true);
					}
				break;
				case 27:// нажата клавиша ESC
					self.hide();
				break;		
				case 13:// нажата клавиша ENTER
					self.on_choose();
				break;	
				case 46:// нажата клавиша DEL
				break;	
				default:// нажаты другие клавиши
					if(self.input.value.length >= 3){
						if(self.input.value == self.prev){
							return;
						}
						self.prev = self.input.value;
					  	self.request(1,self.input.value);
					}else{
						self.clear_list();
						self.hide();
					}
			}			
		},
		set_action : function (){
			if(self.data[self.current]){
				self.input.form.setAttribute('action',self.action + '?id=' + self.data[self.current].elid);
				self.input.form.action = self.action + '?id=' + self.data[self.current].elid;
			}
		},
		set_active : function (num,to_input){
			
			if(self.data.length == 0){
				return;
			}
			
			self.data[self.current].className = 'popup_inactive';
			self.data[num].className = 'popup_active';
			if(to_input){
				self.input.value = self.data[num].innerHTML;
			}
			self.current = num;
			self.set_action();
		},
		request : function(command_,data_){
			self.ask(
				JSON.stringify(
					{ 
						command : command_, 
						data : encodeURI(data_)
					}
				)
			);
			
		},
		getXmlHttp : function (){
			var xmlhttp;
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
					try{
						xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
					}catch(e1){xmlhttp = false;}
			}
			if (!xmlhttp && typeof XMLHttpRequest!="undefined"){xmlhttp = new XMLHttpRequest();}
			return xmlhttp;
		},
		ask : function (json) {
			var req = self.getXmlHttp();
			req.abort();
			req.open('GET',self.script+'?json='+json, true);
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					if(req.status == 200) {
						self.answer(req.responseText);
					}
				}
			}
			req.send(null);
		},		
		answer : function(json){
			var obj = JSON.parse(json);
		
			switch(obj.command){
				case 1 : self.make_data(obj.data); break;
			}
		}
	}
	
	new tcons(obj);
}

