/**
 * @author panLiangHu
 * @copyright 2008 DiLingLing.com
 * 嘀铃铃前台搜索suggestion
 */
var suggestionCount=0;
var currentSuggestionLine=0;
var currentValue="";
var xmlHttp;
var xmlHttpSearchSuggestion;
var xmlHttpSuggestionIndex;
var WAITING_TIME = 150;
var timeoutHandle=null;
var dx = 0;//值越大越靠右
var dy = 23;//值越大越靠下
var divWidth = 272;//显示框的宽度

function dllSearchSubmit(id){
	var theKey=document.getElementById(id).value;
	if(jstrim(theKey)=="") return false;
	document.SEARCH_FORM_HEAD.submit();
}
function timeout(evt,type,width,x,y){
	if(typeof(width) != 'undefined') divWidth = width;
	if(typeof(x) != 'undefined') dx = x;
	if(typeof(y) != 'undefined') dy = y;
	if(jstrim(document.getElementById("q").value) == "") fun_clearSuggestion();
	if(evt.keyCode==38||evt.keyCode==40) return;
	evt=evt?evt:(window.event?window.event:null);
	if(timeoutHandle!=null) window.clearTimeout(timeoutHandle);
	timeoutHandle=window.setTimeout("fun_onkeyup('"+type+"')", WAITING_TIME);
}
function search(key,type){
	if(key != ""){
		var url="/ajax/suggestion.php?type="+type+"&key="+encodeURIComponent(key)+"&num=10";
		xmlHttpSearchSuggestion=GetXmlHttpObject(searchCallback);
		xmlHttpSearchSuggestion.open("GET",url,true);
		xmlHttpSearchSuggestion.send(null);
	} else {
		fun_clearSuggestion();
		return;
	}
}
function searchCallback(){
	if (xmlHttpSearchSuggestion.readyState==4 && xmlHttpSearchSuggestion.status == 200){
		 ParseReback(xmlHttpSearchSuggestion.responseXML);
	}
}
function fun_suggestionOnClick(key){
	document.getElementById("q").value=key;
	dllSearchSubmit('q');
	fun_clearSuggestion();
}
function onTopKeyDown(evt){
  	evt=evt?evt:(window.event?window.event:null);
  	//if (evt.keyCode==13) dllSearchSubmit('q');
    try{
    	var div=document.getElementById("window01");
	    if(jstrim(div.innerHTML)!=""){
	    	if(evt.keyCode==38){
	    		// delete highlight in currentline
	    		var cl=currentSuggestionLine;
	    		if(cl!=0){
	    			document.getElementById("suggestionLine"+cl).className='bg2';
	    		}    			
	    		// highlight nextline and put the nextline text into the input element
	    		var nextLineNumber;
	    		if(cl==0){	
	    			nextLineNumber=suggestionCount;
	    			document.getElementById("suggestionLine"+nextLineNumber).className='bg';
	    			document.getElementById("q").value=document.getElementById("suggestionLine"+nextLineNumber).childNodes[1].nodeValue;
	    		}else if(cl==1){    		
	    			nextLineNumber=0;
	    			document.getElementById("q").value=currentValue;
	    		}else{
	    			nextLineNumber=currentSuggestionLine-1;
	    			document.getElementById("suggestionLine"+nextLineNumber).className='bg';
	    			document.getElementById("q").value=document.getElementById("suggestionLine"+nextLineNumber).childNodes[1].nodeValue;
	    		}	
	    		// change the current suggenstion line
	    		currentSuggestionLine=nextLineNumber;
	    	}
	    
	    	if(evt.keyCode==40){	
				// delete highlight in currentline
	    		var cl=currentSuggestionLine;
	    		if(cl!=0){
	    			document.getElementById("suggestionLine"+cl).className='bg2';
	    		}  
	    		// highlight nextline and put the nextline text into the input element
	    		var nextLineNumber;
	    		if(cl==0){	
	    			nextLineNumber=1;
	    			document.getElementById("suggestionLine"+nextLineNumber).className='bg';
	    			document.getElementById("q").value=document.getElementById("suggestionLine"+nextLineNumber).childNodes[1].nodeValue;
	    		}else if(cl==suggestionCount){		
	    			nextLineNumber=0;
	    			document.getElementById("q").value=currentValue;
	    		}else{
	    			nextLineNumber=currentSuggestionLine+1;
	    			document.getElementById("suggestionLine"+nextLineNumber).className='bg';
	    			document.getElementById("q").value=document.getElementById("suggestionLine"+nextLineNumber).childNodes[1].nodeValue;
	    		}
	    		// change the current suggenstion line
	    		currentSuggestionLine=nextLineNumber;
	    	}
	    }
    } catch(e){
    	suggestion_ini();
    }
}
function fun_onkeyup(type){
	var value=document.getElementById("q").value;
	var text=jstrim(value);
	if(value.substring(value.length-1,value.length)==" ") text=text+" ";
	//if(text==currentValue) return;
	currentValue=text;
	search(currentValue,type);
}
function fun_clearSuggestion(){
	//if(document.getElementById("window01")) document.getElementById("window01").innerHTML = "";
	document.getElementById("dll_suggestion").style.display="none";
}
function ParseReback(xmldoc){
	if(xmldoc == null) {
		fun_clearSuggestion();
		return;
	}
	var items = xmldoc.getElementsByTagName("item");
	var html="";
	var count=0;

	if(items.length>0){		
		html=html+"<ul>";		
		for (var loop=0;loop<items.length;loop++){		
			var item=items[loop];
			var itemKey = unescape(item.getElementsByTagName("item-key")[0].childNodes[0].nodeValue);
			
			if(loop==0){
				html = html + "<li id=\"suggestionLine"+(loop+1)+"\" name=\"suggestionLine"+(loop+1)+"\" style=\"border-top:solid 1px #ccc;\" onClick=\"fun_suggestionOnClick('"+itemKey+"');\" onMouseOver=\"fun_onMouseOverSuggestion("+(loop+1)+");\" onMouseOut=\"document.getElementById('q').onblur=fun_clearSuggestion;this.className='bg2'\" ><span class=\"windowgreen2\" ></span>"+itemKey+"</li>";
			}else{
				html = html + "<li id=\"suggestionLine"+(loop+1)+"\" name=\"suggestionLine"+(loop+1)+"\" onClick=\"fun_suggestionOnClick('"+itemKey+"');\" onMouseOver=\"fun_onMouseOverSuggestion("+(loop+1)+");\" onMouseOut=\"document.getElementById('q').onblur=fun_clearSuggestion;this.className='bg2'\" ><span class=\"windowgreen2\" ></span>"+itemKey+"</li>";
			}
			count++;
    	}
    	suggestionCount=count;
    	html=html+"</ul>";
    	html=html+"<ul style=\"text-align:right\"><a href=\"javascript:fun_clearSuggestion();\" class=\"blue\">关闭</a></li>";
    	html=html+"</ul>";	
  		try{
  			document.getElementById("window01").innerHTML = html;
			fun_showSuggestion('q','suggestiondiv');
  		} catch(e){
  			suggestion_ini();
  		}
	} else {
		fun_clearSuggestion();
	}
}
function suggestion_ini(){
	if(document.getElementById("suggestiondiv") == null || document.getElementById("window01") == null){
		document.getElementById("dll_suggestion").innerHTML = '<div id="suggestiondiv"><div id="window01"></div></div>';
	}
}
function fun_showSuggestion(postionID,suggestionID){
	var point=document.getElementById(postionID);
	var pos = getPostion(point);
	try{
		if(document.getElementById("dll_suggestion").style.display == "none"){
			document.getElementById(suggestionID).style.position = "absolute";
			document.getElementById(suggestionID).style.zIndex = "9999";
			document.getElementById(suggestionID).style.left= (pos.x + dx) + "px";
			document.getElementById(suggestionID).style.top= (pos.y + dy) + "px";
			document.getElementById(suggestionID).style.background = "";
			document.getElementById(suggestionID).style.border = "0px solid #860001";
			document.getElementById(suggestionID).style.padding = "0px";
			document.getElementById("suggestiondiv").style.width = divWidth + "px";
			document.getElementById("dll_suggestion").style.display = "";
		}
	}catch(e){
		suggestion_ini();
	}
}
function getPostion(e){
	var l=e.offsetLeft;
	var t=e.offsetTop;
	while(e=e.offsetParent){
		l+=e.offsetLeft;
		t+=e.offsetTop;
	}
	var pos=new Object();
	pos.x=l;
	pos.y=t;
	return pos;
}
function fun_onMouseOverSuggestion(num){
	
	var obj=document.getElementById("suggestionLine"+num);
	if(currentSuggestionLine>0){
		document.getElementById("suggestionLine"+currentSuggestionLine).className="bg2";
	}
	currentSuggestionLine=num;
	document.getElementById("q").onblur=null;
	obj.className="bg";
}
function GetXmlHttpObject(handler){ 
	var objXmlHttp=null;
	if (navigator.userAgent.indexOf("MSIE")>=0){ 
	
		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
			strName="Microsoft.XMLHTTP";
		}
		try{ 
	
			objXmlHttp=new ActiveXObject(strName);
			objXmlHttp.onreadystatechange=handler; 
			return objXmlHttp;
		}catch(e){ 
			return;
		} 
	}else{
		
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler; 
		
		return objXmlHttp;
	}
}
function jstrim(str){
	if(str==null){
		return null;
	}
	while (str.charAt(0)==" "){
		str=str.substr(1);
	}      
    while (str.charAt(str.length-1)==" "){
    	str=str.substr(0,str.length-1);
    }
    return(str);
}