function addListener(el,ev,fun){
	if(el.attachEvent){
		el.attachEvent("on"+ev,fun);
	}else if(el.addEventListener){
		el.addEventListener(ev,fun,false);
	}
}

var KSSearchPrompt={
	inputEl:null,//输入框
	app:null,//页面
	serverUrl:'',//服务器地址,外部系统调用请用http://ks.pconline.com.cn
	useJs:false,
	floatFrame:null,//漂浮的层
	promptUl:null,//提示列表
	isShowing:false,//当前正在显示提示
	close:true,//当前是否应该显示
	userClose:false,//用户是否关闭了提示
	currentApp:null,//当前的app
	currentXHQ:null,//当前的xmlHttpRequest
	previousXHQ:null,//前一次请求
	currentValue:null,//当前的xmlHttpRequest对应的值
	currentIndex:-1,//当前获取到hover的那条提示
	
	play:function(){
		if(!this.userClose){
			this.close=false;
			var value=this.inputEl.value;
			if(/^(\s*$|请输入)/.test(value)){
				this.hide();
				return;
			}
			if(this.currentValue!=value||this.currentApp!=this.app){
				this.currentValue=value;
				this.currentApp=this.app;
				var f=this.useJs?this.sendJsRequest:this.sendAjaxRequest;
				f.call(this,value,this.app,this.check);
			}else{
				this.check();
			}
		}
	},
	
	stop:function(){
		this.close=true;
		this.hide();
	},
	
	slide:function(obj,time){
		if (time<10){ 
			var currentclip=(time/10*parseInt(obj.offsetHeight))+"px"
			obj.style.clip= "rect(0 auto "+currentclip+" 0)" ;
			setTimeout(function(){KSSearchPrompt.slide(obj,time+1)}, 10)
		}
		else{ 
			obj.style.clip="rect(0 auto auto 0)"
		}
	},
	
	setMenuPosition:function() {
		var obj=this.inputEl;
		var left_offset = obj.offsetLeft;
		var top_offset = obj.offsetTop;
		while((obj = obj.offsetParent) != null) {
			left_offset += obj.offsetLeft;
			top_offset += obj.offsetTop;
		}
		obj=this.floatFrame;
		while((obj = obj.offsetParent) != null) {
			left_offset -= obj.offsetLeft;
			top_offset -= obj.offsetTop;
		}
		this.floatFrame.style.left  = left_offset + 'px';
		this.floatFrame.style.top   = (top_offset + this.inputEl.offsetHeight) + 'px';
		this.floatFrame.style.width = 380 + 'px';//this.floatFrame.style.width = this.inputEl.offsetWidth + 'px';
	},
	
	check:function(){
		if(this.promptUl.childNodes.length!=0){
			this.show();
		}else{
			this.hide();
		}
	},
	
	show:function(){
		if((!this.userClose)&&(!this.close)&&!this.isShowing){
			this.isShowing=true;
			this.setMenuPosition();
			this.slide(this.floatFrame,0);
			this.floatFrame.style.display="block";
			this.floatFrame.style.visibility="visible";
		}
	},
	
	hide:function(){
		if(!this.isShowing) return;
		this.isShowing=false;
		this.floatFrame.style.display="none";
	},
	
	hover:function(index,select){
		var el;
		var nodeList=this.promptUl.childNodes;
		if(typeof index == 'object'){
			el=index;
			for(var i=0;i<nodeList.length;i++){
				if(nodeList[i]==el){
					index=i;
					break;
				}
			}
		}else{
			if(index>-1&&index<nodeList.length){
		    el=nodeList[index];
		  }
		}
		if(index==this.currentIndex||!this.isShowing) return;
		if(el){
		   var el=nodeList[index];
		   if(select) this.inputEl.value=nodeList[index].firstChild.innerHTML;
			 el.className="hover";
			 if(this.currentIndex!=-1){
			 	  try{
						var el=nodeList[this.currentIndex];
						if(el) el.className="";
					}catch(e){}
			 }
			 this.currentIndex=index;
		}
	},
	
	addInput2Listen:function(inputElement,app,serverUrl,useJs){
		if(typeof inputElement == 'object'){
			var inputEl =  inputElement;
		}else{
			var inputEl =  document.getElementById(inputElement);
		}
		this.init(inputEl,app);
		addListener(inputEl,"focus",function(){
			KSSearchPrompt.init(inputEl,app,serverUrl,useJs);
		});
	},
	
	init:function(inputElement,app,serverUrl,useJs){
		this.app=app||'zh';
		this.serverUrl=serverUrl||'';
		this.useJs=!!useJs;
		
		if(!this.floatFrame){
			var floatFrame=this.floatFrame = document.createElement("DIV");
			floatFrame.className = "ajaxsearch";
			floatFrame.style.display="none";
			floatFrame.style.position="absolute";
			//floatFrame.style.border ="1px solid red";
		  document.body.insertBefore(floatFrame,document.body.firstChild);
		  
		  var promptUl=this.promptUl = document.createElement("UL");
		  floatFrame.appendChild(promptUl);
		  
		  var closeDiv = document.createElement("DIV");
		  closeDiv.className="close";
		  floatFrame.appendChild(closeDiv);
		  var closeSpan = document.createElement("span");
		  closeSpan.innerHTML="关闭";
		  closeDiv.appendChild(closeSpan);
		  closeSpan.onclick=function(){KSSearchPrompt.userClose=true;KSSearchPrompt.stop();return false;};
		  
		  //var frame0 = document.createElement("frame");
		  //frame0.frameBorder = "0";
		  //floatFrame.appendChild(frame0);
		}
	  
	  if(typeof inputElement == 'object'){
			var inputEl =  inputElement;
		}else{
			var inputEl =  document.getElementById(inputElement);
		}
		if(inputEl!=this.inputEl){
			this.inputEl=inputEl;
		  inputEl.onclick=function(){
		  	if(KSSearchPrompt.inputEl!=this)
		  	  return true;
		  	KSSearchPrompt.play();
		  	return false;
		  };
		  inputEl.onkeyup=function(e){
		  	if(KSSearchPrompt.inputEl!=this)
		  	  return true;
		  	var key = window.event ? window.event.keyCode : e.which; 
		  	if(key == 13) {// enter
		  		KSSearchPrompt.stop();
		  		return true;
		  	}else if(key == 38) {// ↑
		  		KSSearchPrompt.hover(KSSearchPrompt.currentIndex-1,true);
		  	}else if(key == 40) { // ↓
		  		KSSearchPrompt.hover(KSSearchPrompt.currentIndex+1,true);
		  	}else if(key == 27) { // Esc
		  		KSSearchPrompt.userClose=true;
		  		KSSearchPrompt.stop();
		  	}else{
			  	KSSearchPrompt.play();
			  }
		  	return false;
		  };
		  inputEl.onblur=function(){
		  	if(KSSearchPrompt.inputEl!=this)
		  	  return true;
		  	setTimeout(function(){KSSearchPrompt.stop()}, 200);
		  	return false;
		  };
		}
	},
	cancelRequest:function(){
		this.previousXHQ.abort();
	},
	sendAjaxRequest:function(value, app, callback) {
		this.previousXHQ = this.currentXHQ;
		if(this.previousXHQ){
			this.previousXHQ.onreadystatechange=function(){};
			setTimeout("KSSearchPrompt.cancelRequest()",0);//后台处理
		}
		var url=encodeURI( encodeURI(	this.serverUrl+"/SearchPrompt.jsp?q="+value+"&app="+(app||"zh") ) );
		var xmlHttpRequest;
		if (window.ActiveXObject) {
			xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} else if (window.XMLHttpRequest) {
			xmlHttpRequest = new XMLHttpRequest();
		} else {
			return;
		}
		this.currentXHQ=xmlHttpRequest;
		xmlHttpRequest.open("GET", url, true);
		var f=function(){
			if (xmlHttpRequest.readyState == 4) {
				if (xmlHttpRequest.status == 200) {
					var responseText = xmlHttpRequest.responseText;				
					KSSearchPrompt.requestCallback(responseText,callback);
				}
			}
		};
		var isFirefox=navigator&&navigator.userAgent&&navigator.userAgent.indexOf("Firefox")>0;
		//if(!isFirefox){
			xmlHttpRequest.onreadystatechange =f;
		//}
		xmlHttpRequest.send(null);
		//if(isFirefox){  //firefox烦死人了 
        //f();
		//}
	},
	
	sendJsRequest:function(value, app, callback) {	
		var uri=encodeURI( encodeURI(	this.serverUrl+"/SearchPrompt.jsp?q="+value+"&app="+(app||"zh")+"&jsCallBack=KSSearchPromptJsCallBack" ) );
		var scriptElement=document.createElement('script');
	  document.body.insertBefore(scriptElement,document.body.firstChild);
	  window.KSSearchPromptJsCallBack=function(app,v,data){
	  	if(KSSearchPrompt.currentApp==app&&KSSearchPrompt.currentValue==v){
	  		KSSearchPrompt.requestCallback(data.replace(/\t{3,3}/mg,"\n"),callback);
	  	}
	  }
    setTimeout(function(){
    	scriptElement.src=uri;
    }, 1);
	},
	
	go2Search:function(kw){
		this.inputEl.value=kw;
		var f=this.inputEl;
		while(f){
			if(f.tagName == 'FORM'){
				f.submit();
			}
			f = f.parentNode;
		}
	},
	
  requestCallback:function(responseText,callback) {
				try {
					if(/^(\s|\n|\t)*$/.test(responseText)){
						this.promptUl.innerHTML="";
					}else{
						this.promptUl.innerHTML=("<li id='ac_li_0' onmousemove='KSSearchPrompt.hover(this)' onmousedown='KSSearchPrompt.go2Search(this.firstChild.innerHTML)'><i id='ac_item_4' class='resulttitle'>"+responseText.replace(/^(\n|\t)+|(\n|\t)+$/mg,"").replace(/\n/mg,"</i>结果</li><li id='ac_li_1' onmousemove='KSSearchPrompt.hover(this)' onmousedown='KSSearchPrompt.go2Search(this.firstChild.innerHTML)'><i id='ac_item_4' class='resulttitle'>").replace(/\t+/mg,"</i><i class='num'>")+"</i>结果</li>");
					}
					if(callback) callback.call(this);
				  this.currentIndex=-1;
				} catch(exception) {}
	}
}

