var Search = Class.create();
Search.prototype = {
	initilize:function(txtID){
		this._txt = $ID(txtID);
		
		this._oldValue = "";
		this._index = null;
		this._length = 0;
		
		this._d = document.createElement("div");
		this._d.className = "relating";
		this._d.id = "divSearch";
		this._d.style.display = "none";
		this._d.style.width = this._txt.offsetWidth + "px";
		this._d.style.left = getNodePosition(this._txt,"left") + "px";
		this._d.style.top = getNodePosition(this._txt,"top") + this._txt.offsetHeight + "px";
		document.body.appendChild(this._d);
		
		this._fnKeydown = BindAsEventListener(this,this.Keydown);
		addEventHandler(this._txt,"keydown",this._fnKeydown);
		this._fnKeyup = BindAsEventListener(this,this.Keyup);
		addEventHandler(this._txt,"keyup",this._fnKeyup);

		this._fnMouseOver = BindAsEventListener(this,this.MouseOver);
		this._fnMouseUp = BindAsEventListener(this,this.MouseUp);
		this._fnBodyMouseDown = BindAsEventListener(this,this.BodyMouseDown);
		addEventHandler(document.body,"mousedown",this._fnBodyMouseDown);
		
		this._xmlHttp = null;
		if(window.ActiveXObject){
			this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}else if(window.XMLHttpRequest){
			this._xmlHttp = new XMLHttpRequest();
		}
		
		Bind(this,this.ShowD);
		Bind(this,this.HideD);
	},
	Keydown:function(event){
		if(event.keyCode==40){
			if(this._index == null){
				this._index = 0;
				this._d.childNodes[this._index].style.cssText = "background-color:#3c89d7;color:#fff";
				this._txt.value = this._d.childNodes[this._index].childNodes[0].nodeValue;
			}else{
				this._index++;
				this._d.childNodes[this._index-1].style.cssText = "";
				if(this._index>=this._length){
					this._index = null;
					this._txt.value = this._oldValue;
				}else{
					this._d.childNodes[this._index].style.cssText = "background-color:#3c89d7;color:#fff";
					this._txt.value = this._d.childNodes[this._index].childNodes[0].nodeValue;
				}
			}
		}else if(event.keyCode==38){//up
			if(this._index==null){
				this._index = this._length-1;
				this._d.childNodes[this._index].style.cssText = "background-color:#3c89d7;color:#fff";
			}else{
				this._index--;
				this._d.childNodes[this._index+1].style.cssText = "";
				if(this._index<0){
					this._index = null;
					this._txt.value = this._oldValue;
				}else{
					this._d.childNodes[this._index].style.cssText = "background-color:#3c89d7;color:#fff";
					this._txt.value = this._d.childNodes[this._index].childNodes[0].nodeValue;
				}
			}
		}else if(event.keyCode==13){
			if(this._index!=null){
				this._txt.value = this._d.childNodes[this._index].childNodes[0].nodeValue;
				removeEventHandler(this._txt,"keyup",this._fnKeyup);
				$ID("form_search").submit();
			}
		}
	},
	Keyup:function(event){
		if(event.keyCode!=40&&event.keyCode!=38){
			this._oldValue = this._txt.value;
			this._xmlHttp.open("GET","/ajax/search_product.php?cID="+ $ID("cID").value +"&str=" + encodeURIComponent(this._txt.value.trim()) + "&timeStamp=" + new Date().getTime(),true);
			this._xmlHttp.send(null);
			this._fnStatechange = Bind(this,this.Statechange);
			addEventHandler(this._xmlHttp,"readystatechange",this._fnStatechange);
		}
	},
	Statechange:function(){
		if(this._xmlHttp.readyState==4&&this._xmlHttp.status==200){
			this._d.innerHTML = "";
			this._length = 0;
			var str = this._xmlHttp.responseText;
			if(str==""){
				this._d.style.display = "none";
			}else{
				var arr = str.split(",");
				var temp = document.createDocumentFragment();
				this._length = arr.length;
				for(var i=0;i<this._length;i++){
					var b = document.createElement("b");
					b.innerHTML = arr[i];
					b.id = "bbb_" + i;
					
					addEventHandler(b,"mouseover",this._fnMouseOver);
					addEventHandler(b,"mouseup",this._fnMouseUp);
					
					temp.appendChild(b);
				}
				this._d.appendChild(temp);
				this.ShowD();
				this._index = null;//init
			}
		}
	},
	MouseOver:function(event){
		if(isIE){
			var me = event.srcElement;
			event.cancelBubble = true;
		}else{
			var me = event.target;
			event.stopPropagation();
		}
		
		for(var i=0;i<this._d.childNodes.length;i++){
			this._d.childNodes[i].style.cssText = "";
		}
		
		if(me.nodeName=="B"){
			var cur = me.id.split("_")[1];
			this._d.childNodes[cur].style.cssText = "background-color:#3c89d7;color:#fff";
			this._index = cur;
		}
	},
	MouseUp:function(event){
		if(isIE){
			var me = event.srcElement;
			event.cancelBubble = true;
		}else{
			var me = event.target;
			event.stopPropagation();
		}
		if(me.nodeName=="B"){
			this._txt.value = me.childNodes[0].nodeValue;
			$ID("form_search").submit();
			this.HideD();
		}
	},
	BodyMouseDown:function(event){
		if(isIE){
			var me = event.srcElement;
			event.cancelBubble = true;
		}else{
			var me = event.target;
			event.stopPropagation();
		}
		
		var sign = 0;
		while(me!=document.body){
			if(me.id=="divSearch"){
				sign = 1;
				break;
			}
			me = me.parentNode;
		}
		if(sign==0){
			this.HideD();
		}
	},
	ShowD:function(){
		//为了兼容ie6，遮住sortBy下拉列表
		/*if(this._length>=5){//结果大于5的
			if($ID("sortBy")){
				$ID("sortBy").style.display = "none";
			}
		}*/
		this._d.style.display = "block";
	},
	HideD:function(){
		this._d.style.display = "none";
		/*if(this._length>=5){//结果大于5的
			if($ID("sortBy")){
				$ID("sortBy").style.display = "block";
			}
		}*/
	}
}

$(document).ready(function(){
	new Search("search");
});

