var NameBrandSearchDisplay = {
	"currenttextobj": null,
	"selectcolor": "rgb(245,143,31)",
	"backgroundcolor": "white",
	"lastquerytime": new Date().getTime(),
	"listselectindex": -1,//-1: list not exist,0:no one is selected,1:selected row 1,
	"ajaxurl": "nameorbrand.show.do",
	"resultlist":new Array(),
	"namebrandlistdiv":document.getElementById("namebrandlistdiv"),
	textFieldEnter: 
			function(ev,obj){
				this.currenttextobj = obj;
				this.namebrandlistdiv = document.getElementById("namebrandlistdiv");
				switch(ev.keyCode) {
					case 38: // up
							this.move(-1);
							break;
					case 40: // down
							this.move(1);
							break;
					case 13: // enter
							this.selectCurrent(-1);
							break;
					case 37://left
							break;
					case 39://right;
							break;
					case 32://space;
							break;		
					default:
							this.ajaxQuery();
							break;
				  }
			},
	move:
		function(offset){
			if(offset<0 && this.listselectindex==0){
				this.listselectindex = this.resultlist.length;
			}else{
				this.listselectindex+=offset;
				if(this.listselectindex>this.resultlist.length){
					this.listselectindex = 1;
				}
				if(offset<0 && this.listselectindex==0){
					this.listselectindex = this.resultlist.length;
				}
			}
			this.clearAllBGColor();
			this.setSelectBGColor();
		},
	selectCurrent:
		function(index){//index -1, select current item
			if(index > 0){
				this.listselectindex = index;
			}
			var selectobj = this.resultlist[this.listselectindex-1];
			this.setInputValue(selectobj);
			this.closeDisplayList();
		},
	closeDisplayList:
		function(){
			this.namebrandlistdiv.style.display = "none";
		},
	mouseover:
		function(index){
			this.listselectindex = index;
			this.clearAllBGColor();
			this.setSelectBGColor();
		},
	mouseout:
		function(index){
			this.listselectindex = index;
			this.clearAllBGColor();
			this.setSelectBGColor();
		},
	generateListUI:
		function(){
			//if(branddiv.style.opacity)branddiv.style.opacity="0.5";//transparent
			//if(branddiv.style.filter)branddiv.style.filter="alpha(opacity=50)";
			$("#liid1").each(function(){
				$(this).parent().remove();
			});
			var str="<ul style='background-color:white;color:black;border:1px solid #8F8F8F;list-style-type:none;text-align:left;margin:0px;padding:0px;font-weight:bold;'>";
			for(var i=1; i<=this.resultlist.length; i++){
				str+="<li style='cursor:pointer;padding:5px;' onmouseover=\"javascript:NameBrandSearchDisplay.mouseover("+i+");\""
				+" id='liid"+i+"'"
				+" onmouseout=\"NameBrandSearchDisplay.mouseout("+i+");\""
				+" onclick=\"javascript:NameBrandSearchDisplay.selectCurrent("+i+")\">"
				+this.resultlist[i-1]+"</li>";
			}
			if(this.resultlist.length>0){
				str+="<li id='closelink' style='color:#0E4A8E;cursor:pointer;padding: 10px;text-align:right;' onclick=\"javascript:NameBrandSearchDisplay.closeDisplayList();\">"
				 +"Close</li>";
			}
			//var divobj = document.getElementById("namebrandlistdiv");
			this.namebrandlistdiv.innerHTML=str+"</ul>";
			var pos = GetObjPos(this.currenttextobj);
			this.namebrandlistdiv.style.position="absolute";
			this.namebrandlistdiv.style.left=(pos.x)+"px";
			this.namebrandlistdiv.style.top=(pos.y+this.currenttextobj.offsetHeight)+"px";
			this.namebrandlistdiv.style.display="";
		},
	ajaxQuery:
		function(){
			var text = this.currenttextobj.value;
			if(text==""){
				return;
			}
			var curtime = new Date().getTime();
			if((curtime - this.lastquerytime)<600){
				return;
			}
			$.post(this.ajaxurl,{"ajax":1,"field(name)":text},
				function (res){
					try{
						NameBrandSearchDisplay.resultlist = eval("(" + res + ")");
					}catch(e){
						NameBrandSearchDisplay.closeDisplayList();
						return;
					}
					NameBrandSearchDisplay.lastquerytime = new Date().getTime();
					if(NameBrandSearchDisplay.resultlist.length>0){
						NameBrandSearchDisplay.listselectindex=0;
						NameBrandSearchDisplay.generateListUI();
					}else{
						NameBrandSearchDisplay.listselectindex=-1;
						NameBrandSearchDisplay.closeDisplayList();
					}
				}
			);
		},
	clearAllBGColor:
		function(){
			for(var i=1; i<=this.resultlist.length; i++){
				document.getElementById("liid"+i).style.backgroundColor = this.backgroundcolor;
			}
		},
	setSelectBGColor:
		function(){
			document.getElementById("liid"+this.listselectindex).style.backgroundColor=this.selectcolor;
		},
	setInputValue:
		function(value){
			if(value){
				$("#keyword")[0].value=value;
			}
		}	
}
