//云端搜索提示类
var search_suggest={
	globalVar: {"defaultValue":["",false],"inputIsFocus":false,"input_Id":"","onkey_is_up":false,"selectedIndex":-1,"URL":"http://baoku.yunduan.cn","arrList":[],"firstKey":"","closeTipsTimer":null,"tipsIsClose":false,"suggestDivIsShow":false,"searchtnum":"","siteClass":"d"},
	$: function(ele){
		if(typeof(ele)=="object"){
			return ele;
		}else if(typeof(ele)=="string"||typeof(ele)=="number"){
			return document.getElementById(ele.toString());
		}else{
			return null;
		}
	},
	ajax: function(){
		var ajax = new Object();
		ajax.resultHandle = null;
		ajax.sendString = '';
		ajax.targetUrl = '';
		ajax.createXMLHttpRequest = function(){
			var request = false;
			if(window.XMLHttpRequest){
				request = new XMLHttpRequest();
				if(request.overrideMimeType){
					request.overrideMimeType('text/xml');
				}
			}else if(window.ActiveXObject){
				var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
				for(var i=0; i<versions.length; i++){
					try{
						request = new ActiveXObject(versions[i]);
						if(request){
							return request;
						}
					}catch(e){}
				}
			}
			return request;
		}
		ajax.XMLHttpRequest = ajax.createXMLHttpRequest();
		ajax.processHandle = function() {
			if(ajax.XMLHttpRequest.readyState == 4 && ajax.XMLHttpRequest.status == 200) {
				ajax.resultHandle(ajax.XMLHttpRequest.responseText, ajax);
			}
		}
		ajax.get = function(targetUrl, resultHandle) {
			ajax.targetUrl = targetUrl;
			ajax.XMLHttpRequest.onreadystatechange = ajax.processHandle;
			ajax.resultHandle = resultHandle;		
			if(window.XMLHttpRequest) {
				ajax.XMLHttpRequest.open('GET', ajax.targetUrl);
				ajax.XMLHttpRequest.send(null);
			} else {
				ajax.XMLHttpRequest.open('GET', ajax.targetUrl,true);
				ajax.XMLHttpRequest.send();
			}
		}
		ajax.post = function(targetUrl, sendString, resultHandle) {
			ajax.targetUrl = targetUrl;
			ajax.sendString = sendString;
			ajax.XMLHttpRequest.onreadystatechange = ajax.processHandle;
			ajax.resultHandle = resultHandle;
			ajax.XMLHttpRequest.open('POST', targetUrl);
			ajax.XMLHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			ajax.XMLHttpRequest.send(ajax.sendString);
		}
		return ajax;
	},
	keyCode: function(e){
		return (window.event ? e.keyCode : e.which);
	},
	inputFocus: function(ele){
		var isThis=this;
		if(!isThis.globalVar.inputIsFocus){isThis.checkDefaultValue(ele);isThis.globalVar.inputIsFocus=true;}
		clearTimeout(isThis.globalVar.closeTipsTimer);
		if(!isThis.$("suggest")){
			var createDiv = document.createElement("div");
			createDiv.style.cssText="height:auto;width:200px;border:1px #000 solid;font-size:12px;";
			createDiv.id = "suggest";
			document.body.appendChild(createDiv);
		}
		ele.onkeydown=function(event){isThis.globalVar.onkey_is_up=false;isThis.getKeySuggestText(event,this);}
		ele.onkeyup=function(){
			isThis.globalVar.onkey_is_up=true;
			if(this.value==""){isThis.globalVar.tipsIsClose=false;isThis.closeSuggestTips(0);isThis.inputFocus(this);}
		}
		ele.onblur=function(){
			isThis.checkDefaultValue(this);
			isThis.globalVar.inputIsFocus=false;
			isThis.globalVar.closeTipsTimer=setTimeout("search_suggest.closeSuggestTips(0);",200);
		}
		if(/msie/i.test(navigator.userAgent)){
				ele.onpropertychange=function(){
				if(!isThis.globalVar.tipsIsClose){
					if(this.value!=isThis.globalVar.firstKey){
						isThis.showSuggestTips(this);
					}
				}
			}
		}else{
			ele.addEventListener("input",function(){if(!isThis.globalVar.tipsIsClose){if(ele.value!=isThis.globalVar.firstKey){isThis.showSuggestTips(this);}}},false);
		}
	},
	checkDefaultValue: function(ele){
		if(!this.globalVar.defaultValue[1]){
			this.globalVar.defaultValue[0]=ele.value;
			this.globalVar.defaultValue[1]=true;
		}
		var dv= this.globalVar.defaultValue[0];
		if(ele.value!=""){
			if(ele.value==dv){
				ele.value="";
			}
		}else{
			ele.value=dv;
		}
	},
	getKeySuggestText: function(e,d){
		e = e || window.event;
		this.globalVar.input_Id = d;
		var keyCode=this.keyCode(e);
		if(keyCode==40 || keyCode==38){//↑键 ↓键
			this.globalVar.onkey_is_up=false;
			this.$(d).onpropertychange=function(){};
			var isUp=true;
			if(keyCode==38){isUp=false;}
			if(!this.globalVar.onkey_is_up){
				if(this.$('suggest').innerHTML!=""){
					this.list_changeSelection(isUp);
				}
			}
		}else if(keyCode==13){//回车键
			this.list_outSelection(this.globalVar.selectedIndex);
		}else if(keyCode==8){//退格键
			var isThis=this;
			this.$(d).onpropertychange=function(){
				if(!isThis.globalVar.tipsIsClose){
					if(this.value!=isThis.globalVar.firstKey){
						isThis.showSuggestTips(this);
					}
				}
			}
		}else{
			if(keyCode!=9 && !(keyCode>=16 && keyCode<=20) && keyCode!=27 && !(keyCode>= 33 && keyCode<=39) && keyCode!=44 && !(keyCode>=112 && keyCode<=123)){
				if(this.globalVar.onkey_is_up){
					this.globalVar.onkey_is_up=false;
					this.$(d).onpropertychange=function(){search_suggest.showSuggestTips(this)};
				}
				return;
			}
		}
	},
	showSuggestTips: function(ele){
		var value = this.$(ele).value;
		if(value!="" || value!=undefined){
			this.suggestlist(ele);
			this.globalVar.selectedIndex=-1;
			this.globalVar.firstKey=value;
			if(!this.globalVar.suggestDivIsShow){this.divPosition(ele);this.globalVar.suggestDivIsShow=true;}
		}else{
			this.globalVar.firstKey="";
			search_suggest.closeSuggestTips(0);
			return;
		}
	},
	suggestlist: function(ele){
		var value = this.$(ele).value;
		var postSugInfoUrl=this.globalVar.URL.replace('ydsearch://','http://');
		var getUrl = postSugInfoUrl+'/suggest/?ajax=1&key=' + encodeURIComponent(value);
		var isThis=this;
		(new this.ajax).get(getUrl,function(r){//生成列表
			isThis.globalVar.arrList= r = eval(r);
			var list_Html='<ul>';
			var rLen = r.length;
			if(rLen>0){
				for(var i=0;i<rLen;i++){
					list_Html += "<li onmouseover=\"search_suggest.mouseOverList(this);\" onmousedown=\"search_suggest.mouseClick("+ i +")\">" + r[i].key.replace("'","’").replace('"','”') + "</li>";
				}
				list_Html +="<div class='closeSugest'><a href='javascript:;' target='_parent' ";
				if(/opera/i.test(navigator.userAgent)){list_Html +="onmousedown='";}
				else{list_Html +="onclick='";}
				list_Html +="search_suggest.closeSuggestTips(1);'>关闭</a></div></ul>";
				isThis.$("suggest").innerHTML=list_Html;
				if(isThis.$(ele).value!=""){isThis.$("suggest").style.display = "block";};
			}else{
				isThis.closeSuggestTips(0);
			}
		});
	},
	list_changeSelection: function(isUp){
		var input_id = this.globalVar.input_Id.id;
		if(isUp){
			this.globalVar.selectedIndex++;
		}else{
			this.globalVar.selectedIndex--;
		}
		var maxIndex = this.globalVar.arrList.length-1;
		if(this.globalVar.selectedIndex<0){this.globalVar.selectedIndex=maxIndex;}
		if(this.globalVar.selectedIndex>maxIndex+1){this.globalVar.selectedIndex=0;}
		if(this.globalVar.selectedIndex==maxIndex+1){
			this.$(input_id).value=this.globalVar.firstKey;
		}
		for(var i=0;i<=maxIndex;i++){
			if(i==this.globalVar.selectedIndex){
				this.$('suggest').getElementsByTagName("ul")[0].childNodes[i].className="selectedStyle";
				this.$(input_id).value=this.globalVar.arrList[i].key.replace("&amp;","&");
			}else{
				this.$('suggest').getElementsByTagName("ul")[0].childNodes[i].className="";
			}
		}
	},
	list_outSelection: function(n){
		if(n==-1){
			if(this.$(this.globalVar.input_Id.id).value==""){return;}
		}else{
			this.$(this.globalVar.input_Id.id).onpropertychange=function(){};
			this.$(this.globalVar.input_Id.id).value = this.globalVar.arrList[n].key.replace("&amp;","&");
		}
		this.closeSuggestTips(0);
		if(this.$("suggestIe6Frm")){this.$("suggestIe6Frm").style.display="none";}
		window.location.href=this.globalVar.URL+"/"+this.globalVar.siteClass+"/search/"+ encodeURIComponent(this.$(this.globalVar.input_Id.id).value)+"/?recorde=1&t="+this.globalVar.searchtnum;
	},
	mouseClick: function(n){
		this.$(this.globalVar.input_Id.id).onpropertychange=function(){};
		this.$(this.globalVar.input_Id.id).value=this.globalVar.arrList[n].key.replace("&amp;","&");
		window.location.href=this.globalVar.URL+"/"+this.globalVar.siteClass+"/search/"+ encodeURIComponent(this.$(this.globalVar.input_Id.id).value)+"/?recorde=1&t="+this.globalVar.searchtnum;
	},
	mouseOverList: function(o){
		var getThisList=o.parentNode.getElementsByTagName("li");
		var thisListLen=getThisList.length;
		for(var i=0;i<thisListLen;i++){
			getThisList[i].className='';
			if(getThisList[i]==o){this.globalVar.selectedIndex=i};
		}
		o.className='selectedStyle';
	},
	closeSuggestTips: function(n){
		var inputObj = this.$(this.globalVar.input_Id.id);
		if(inputObj!=null){if(this.globalVar.firstKey!=inputObj.value){this.globalVar.firstKey=inputObj.value;}}
		if(n==1){
			this.globalVar.tipsIsClose=true;
			clearTimeout(this.globalVar.closeTipsTimer);
			this.globalVar.closeTipsTimer=null;
			inputObj.value=this.globalVar.firstKey;
			if(inputObj.createTextRange){
				var range = inputObj.createTextRange();
				range.collapse(false);
				range.moveStart('character',inputObj.value.length);
				range.select();
			}else if(inputObj.setSelectionRange){
				inputObj.setSelectionRange(inputObj.value.length, inputObj.value.length);
				inputObj.focus();
			}
			this.$('suggest').style.display ='none';
			//inputObj.onblur=function(){this.focus();};
			return;
		}else{
			this.$('suggest').style.display ='none';
		}
		this.$('suggest').innerHTML="";
		clearTimeout(this.globalVar.closeTipsTimer);
	},
	divPosition: function(ele){
		var e = this.$(ele);
		if(/msie/i.test(navigator.userAgent)){
			var top = 0;
			var left = -1;
		}else if(/opera/i.test(navigator.userAgent)){
			var top = 0;
			var left = -8;
		}else{
			var top = 1;
			var left = -5;
		}
		while(e.offsetParent){
			left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
			top += e.offsetTop + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
			e = e.offsetParent;
		}
		left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		top += e.offsetTop + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		//this.$('suggest').style.width= e.clientWidth + 19 + "px";
		//this.$('suggest').style.top = (top + e.clientHeight) + "px";
		//this.$('suggest').style.left = left + "px"; 
	}
};
Number.prototype.NaN0 = function(){return isNaN(this)?0:this;}

