function Ajx(){
	this.httpRequest=false;
	if(window.XMLHttpRequest)this.httpRequest=new XMLHttpRequest();
	else if(window.ActiveXObject){
		try{this.httpRequest=new ActiveXObject("Msxml2.XMLhttpRequest");}catch(e){
			try{this.httpRequest=new ActiveXObject("Microsoft.XMLHTTP");}catch(er){}
		}
	}
	if(!this.httpRequest)alert('Ajax >> Невозможно создать экземпляр класса XMLhttpRequest ');
};
Ajx.prototype.request=function(url,targetObject){
	if(!this.httpRequest) return;
	this.httpRequest.abort();
	this.targetObject=targetObject;
	this.httpRequest.onreadystatechange=function(ajax){return function(){ajax.processStateChange();}}(this);
	if(this.GET){
		url+='?';
		for(var v in this.GET)url+=v+'='+encodeURIComponent(this.GET[v])+'&';
	}
	if(this.POST){
		this.httpRequest.open('POST',url,true);
		var post='';
		for(var v in this.POST)post+=v+'='+encodeURIComponent(this.POST[v])+'&';
		this.httpRequest.setRequestHeader('Content-Type', "application/x-www-form-urlencoded");
		this.httpRequest.setRequestHeader('Content-Length', post.length);
	}else this.httpRequest.open('GET',url,true);
	this.httpRequest.send(post);
	this.GET=this.POST=null;
};
Ajx.prototype.processStateChange=function(){
	if(this.httpRequest.readyState==4){
		if(this.httpRequest.status==200){
			this.targetObject.processResult(this.httpRequest.responseText,this.httpRequest.responseXML);
		}else alert('Ajax >> С запросом возникла проблема.');
	}
};
Ajx.prototype.setGET=function(vname,value){
	if(!this.GET)this.GET=new Object();
	this.GET[vname]=value;
};
Ajx.prototype.setPOST=function(vname,value){
	if(!this.POST)this.POST=new Object();
	this.POST[vname]=value;
};


function searchPrompt(id){
	this.c=todo.create('ul',null,null,{'background':'#fff',
		'border':'1px solid #ccc',
		'listStyle':'none',
		'padding':'5px 10px',
		'margin':'0px',
		'position':'absolute',
		'top':'8px',
		'left':'40px',
		'visibility':'hidden',
		'minWidth':'280px',
		'minHeight':'20px',
		'zIndex':'1000'});
	//this.field=todo.get(id);
	//this.c=this.field.parentNode.insertBefore(this.c,this.field.parentNode.firstChild);
	//this.c=this.field.parentNode.insertBefore(this.c,this.field);
	this.c=todo.get('search_prompt').appendChild(this.c);
	this.cCloseLi=this.c.appendChild(todo.create('li',{'class':'searchCloseButton'},null,{'textAlign':'right'}));
	this.cClose=this.cCloseLi.appendChild(todo.create('span',null,'закрыть',{'fontSize':'9px','cursor':'pointer','color':'#555'}));
	this.cClose._sp=this;
	this.cClose.onclick=function(){this._sp.close();};
	this.close=function(){this.c.style.visibility='hidden';};
	this.open=function(){this.c.style.visibility='visible';};
	this.clear=function(){
		while(this.c.firstChild){
			if(this.c.firstChild.className=='searchCloseButton')break;
			else this.c.removeChild(this.c.firstChild);
		}
	};
	this.wait=function(state){
		this.clear();
		if(state)this.c.style.background='#fff url(images/ajax-loader.gif) no-repeat 2px 1px';
		else this.c.style.background='#fff';
	};
	this.addItem=function(text,url){
		var li=this.c.insertBefore(todo.create('li',{'class':'result_item'},url?null:text,{'textAlign':'left','padding':'2px 0 1px 10px','whiteSpace':'nowrap'}),this.cCloseLi);
		if(url){
			li.appendChild(todo.create('a',{'href':url},text,{'color':'#555','whiteSpace':'nowrap','display':'block','textDecoration':'none'}));
			li.onmouseover=function(){this.style.background='#ccc';};
			li.onmouseout=function(){this.style.background='';};
		}
	};
	this.addSection=function(text){
		var li=this.c.insertBefore(todo.create('li',null,text,{'textAlign':'left','fontWeight':'bold','color':'#555','padding':'2px 0 1px 0','whiteSpace':'nowrap'}),this.cCloseLi);
	};
	this.processResult=function(text,xml){
		this.wait(false);
		if(!xml){alert(text);return;};
		var res=xml.getElementsByTagName('mod'),
			res2=xml.getElementsByTagName('cat'),
			cParent = '';
		for(var i=0,l=res.length;i<l;i++) {
			if (cParent!=res[i].getAttribute('parent')) {
				cParent=res[i].getAttribute('parent');
				this.addSection(res[i].getAttribute('parent'));
			}
			if(res[i].getAttribute('chair'))
				this.addItem(res[i].getAttribute('name'),'/more_info/'+res[i].getAttribute('id')+'/');
			else this.addItem(res[i].getAttribute('name'),'/cart/'+res[i].getAttribute('id')+'/');
		}
		for(var i=0,l=res2.length;i<l;i++) {
			if (cParent!=res2[i].getAttribute('parent')) {
				cParent=res2[i].getAttribute('parent');
				this.addSection(res2[i].getAttribute('parent'));
			}
		
			this.addItem(res2[i].getAttribute('name'),res2[i].getAttribute('url'));
		}
		if(!res.length && !res2.length)this.addItem('по данному запросу ничего не найдено',null);
		else $("li.result_item").highlight($('#search_string').val(),{'className':'clY'});
		$('.clY').css({'color':'#900','font-style':'italic'});
	};
	this.search=function(subj){
		this.open();
		this.wait(true);
		var ajx=new Ajx,
			d=new Date;
		ajx.setPOST('request',subj);
		ajx.request('/search_new.php?x='+d.getSeconds()+d.getMinutes(),this);
	}
};

//todo.onload(function(){try{
$(document).ready(function() {
	var ss=todo.get('search_string');
	ss.onkeyup=function(){
		if(this.value.length>2 && this._lastQuery!=this.value){
			if(this._requestDelay)window.clearInterval(this._requestDelay);
			this._requestDelay=window.setInterval(function(o){return function(){o._searchRequest()}}(this),70);
		}
	};
	ss._searchRequest=function(){
		if(this._requestDelay){
			window.clearInterval(this._requestDelay);
			this._requestDelay=null;
		}
		if(!this._sp)this._sp=new searchPrompt(this.id);
		this._lastQuery=this.value;
		this._sp.search(this.value);
	}
	ss.onfocus=function(){this.style.backgroundImage='none';};
	ss.onblur=function(){if(this.value.length)this.style.background='';else this.style.background='#fff url(/images/home_search_bg_new.gif) no-repeat left center';};
	ss.onblur();
});
//}catch(er){}});
