
var lastindex=-1;
var flag=false;//标记
var listlength=0;//查询词长度
var kkk = "k";
function StringBuffer(){this.data=[];} 
StringBuffer.prototype.append=function(){this.data.push(arguments[0]);return this;} 
StringBuffer.prototype.tostring=function(){return this.data.join("");} 
String.prototype.Trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}

//默认设置
function hiddensearch()
{
 document.getElementById("rlist"+kkk).style.display="none";
 document.getElementById("rFrame"+kkk).style.display="none";
}

//获得坐标
function getposition(element,offset)
{
    var c=0;
    while(element)
    {
        c+=element[offset];
        element=element.offsetParent
    }
    return c;
}
//创建下拉框
function createlist(ci)
{
	var listDiv=document.createElement("div");
	listDiv.id="rlist"+ci;        
	listDiv.style.zIndex="2";
	listDiv.style.position="absolute"; 
	listDiv.style.border="solid 1px #000000";
	listDiv.style.backgroundColor="#FFFFFF";
	listDiv.style.display="none";	
	listDiv.style.width=document.getElementById(ci).clientWidth+ 4 +"px";
	listDiv.style.left=getposition(document.getElementById(ci),'offsetLeft')+"px";
	listDiv.style.top =(getposition(document.getElementById(ci),'offsetTop')+document.getElementById(ci).clientHeight +3)+"px";
	
	var listFrame=document.createElement("iframe");
	listFrame.id="rFrame"+ci;
	listFrame.style.zIndex="1";
	listFrame.style.position="absolute";
	listFrame.style.border="0";
	listFrame.style.display="none";	
	listFrame.style.width=document.getElementById(ci).clientWidth+ 4 +"px";
	listFrame.style.left=getposition(document.getElementById(ci),'offsetLeft')+"px";
	listFrame.style.top =(getposition(document.getElementById(ci),'offsetTop')+document.getElementById(ci).clientHeight +3)+"px";
	
	document.body.appendChild(listDiv);       
	document.body.appendChild(listFrame);
}

function showsearch(num)
{
	if(kkk=='keyword'){
		document.getElementById("rlistkeyword").style.display='inline';
 	 	document.getElementById("rFramekeyword").style.display='inline';
	}else{
		document.getElementById("rlistkeyword").style.display='none';
  		document.getElementById("rFramekeyword").style.display='none';
	}
  document.getElementById("rlist"+kkk).style.height=num*20+num+'px';
  document.getElementById("rFrame"+kkk).style.height=num*20+num+'px';
}
//鼠标在下拉词上
function focusitem(index)
{
  var itemXL = document.getElementById(kkk+'item'+lastindex);
  var itemXF = document.getElementById(kkk+'item'+index);
  if(itemXL!=null)setstyle(itemXL,'d');
  if(itemXF!=null)
  {
   setstyle(itemXF, 'm');
   lastindex=index;
  }
  else document.getElementById(kkk).focus();
}
//选择下拉词
function searchclick(index)
{
  document.getElementById(kkk).value=document.getElementById(kkk+'title'+index).innerHTML;
  flag=true;
}
//设置样式
function setstyle(element,classname)
{
	switch (classname)
    {
     case 'm':
	      element.style.fontSize="12px";
		  element.style.fontFamily="arial,sans-serif";
		  element.style.backgroundColor="#3366cc";
		  element.style.color="black";
		  element.style.width=document.getElementById(kkk).clientWidth-2+"px";
		  element.style.height="20px";
          element.style.padding="1px 0px 0px 2px";
          if(element.displaySpan)element.displaySpan.style.color="white"
		  break;
     case 'd':
	      element.style.fontSize="12px";
		  element.style.fontFamily="arial,sans-serif";
		  element.style.backgroundColor="white";
		  element.style.color="black";
		  element.style.width=document.getElementById(kkk).clientWidth-2+"px";
		  element.style.height="20px";
          element.style.padding="1px 0px 0px 2px";
          if(element.displaySpan)element.displaySpan.style.color="green"
		  break;
	 case 't':
	      element.style.width="80%";
		  if(window.navigator.userAgent.toLowerCase().indexOf("firefox")!=-1)element.style.cssFloat="left";
		  else element.style.styleFloat="left";
		  element.style.whiteSpace="nowrap";
		  element.style.overflow="hidden";
		  element.style.textOverflow="ellipsis";
		  element.style.fontSize="12px";
		  element.style.textAlign="left";
		  break;
	 case 'h':
	      element.style.width="20%";
		  if(window.navigator.userAgent.toLowerCase().indexOf("firefox")!=-1)element.style.cssFloat="right";
		  else element.style.styleFloat="right";
		  element.style.textAlign="right";
		  element.style.color="green";
		  break;
    }
}


function searchkeydown(e)
{
 if(document.getElementById("rlist"+kkk).innerHTML=='')return;
  var keycode=(window.navigator.appName=="Microsoft Internet Explorer")?event.keyCode:e.which;
  //down
  if(keycode==40)
  {
	if(lastindex==-1||lastindex==listlength-1)
	{
	 focusitem(0);
	 searchclick(0);
	}
	else{
	 focusitem(lastindex+1);
	 searchclick(lastindex+1);
	}
  }
 if(keycode==38)
 {
	if(lastindex==-1)
	{
	 focusitem(0);
	 searchclick(0);
	}
	else{
	 focusitem(lastindex-1);
	 searchclick(lastindex-1);
	}
 }
 var kw = document.getElementById(kkk);
 if(keycode==13)
 {
  focusitem(lastindex);
  kw.value=document.getElementById(kkk+'title'+lastindex).innerText;
 }	   
 if(keycode==46||keycode==8){flag=false;ajaxsearch(kw.value.substring(0,kw.value.length-1).Trim());}
}
 var http_request;
function ajaxsearch(value)
{
	if (window.XMLHttpRequest) { // Mozilla, Safari, ... 
		http_request = new XMLHttpRequest(); 
	} else if (window.ActiveXObject) { // IE 
		http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
	}
	
	// 设置回调方法
	http_request.onreadystatechange = showresult; 
	// 确定发送请求的方式和 URL 以及是否异步执行下段代码 
	
	var url = "/so/search.do?m=search_kw&kw="+encodeURI(value)+"&t="+document.getElementById("t").value;
	http_request.open("post", url , true); 
	// 设置表头
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	// 发送信息
	http_request.send(null); 
}
function showresult(xmlhttp)
{
	if (http_request.readyState == 4) { // 判断对象状态 
		if (http_request.status == 200) { // 信息已经成功返回，开始处理信息 
			var result=http_request.responseText;
			if(result!=''){
			   var resultstring=new StringBuffer();
		       var title=result;
		      // var hits=result.split('$')[1];
			   for(var i=0;i<title.split('|').length;i++)
			   {
				  resultstring.append('<div id="'+kkk+'item'+i+'" onmousemove="focusitem('+i+')" onmousedown="searchclick('+i+')">');
				  resultstring.append('<span id='+kkk+'title'+i+'>');
				  resultstring.append(title.split('|')[i]);
				  resultstring.append('</span>');
				 // resultstring.append('<span id=hits'+i+'>');
				  //resultstring.append(hits.split('|')[i]);
				 // resultstring.append('</span>');
				  resultstring.append('</div>');
			   }
			   document.getElementById("rlist"+kkk).innerHTML=resultstring.tostring();
			   for(var j=0;j<title.split('|').length;j++)
			   {
			    setstyle(document.getElementById(kkk+'item'+j),'d');
				document.getElementById(kkk+'item'+j).displaySpan=document.getElementById('hits'+j);
			    setstyle(document.getElementById(kkk+'title'+j),'t');
			    //setstyle(document.getElementById('hits'+j),'h');
			   }
			   showsearch(title.split('|').length);
			   listlength=title.split('|').length;
			   lastindex=-1;
			}else{
				hiddensearch();
			}
		} else { 
			
		} 
	} 
	
}

// 处理返回信息的函数 
function main(){
 var kw = document.getElementById(kkk);
	if(kw.value.Trim()=='')hiddensearch();
	else
	{
      if(kw.value!=''&&flag==false)ajaxsearch(kw.value.Trim());
	  if(listlength!=0)
	  {
	  	kw.onkeydown=searchkeydown;
	  }else {
	  
	  	hiddensearch();
	  }
	}
}
function setnk(kn)
{
	kkk = kn;
}
//载入函数
function oninit()
{
	var kw = document.getElementById("keyword");
	kw.autocomplete="off";
	kw.onfocus=main;
	kw.onkeyup=main;
	kw.onblur=hiddensearch; 
	
	createlist("keyword");	
} 
