var currentSuggestion=0;
var noOfSuggestions=0;
var suggestions=null;

var searching=null;
var theForm=null;
var theObject=null;

var lastRequest=0;

function initialise(f,w,s,o) {

	//make sure suggestions is set
	if (suggestions==null)
		suggestions=document.getElementById("suggestions");

	theForm=f;
	searching=s;
	theObject=o;
	currentSuggestion=0;
	
	//set the width
	suggestions.style.width=""+w+"px";
	
}

function clearSuggestions() {

	currentSuggestion=0;
	noOfSuggestions=0;

	suggestions.style.display="none";

}

function clickSuggestion(no) {

	//suggestion selected
	if (currentSuggestion>0) {

		//blank the previous one
		document.getElementById("suggestion"+currentSuggestion).style.backgroundColor="#ffffff";
		document.getElementById("suggestion"+currentSuggestion).style.color="#000000";

	}

	//new suggestion selected
	if (no>0) {

		//set value
		theObject.value=removeTags(document.getElementById("suggestion"+no+"text").innerHTML);
		
	}

	//clear suggestions
	clearSuggestions();

	//simple search
	if (theForm=="simpleSearch") {
	
		//submit the form
		document.getElementById(theForm).submit();
	
	}

}

function doRequest() {

	var now=new Date().getTime();
	
	//more than 50 millis since last request
	if ((now-lastRequest)>=50) {

		var url="suggestions.jsp?searching="+searching+"&name="+encodeURIComponent(theObject.value)+"&time="+now;

		//proper browser
		if (window.XMLHttpRequest)
			req=new XMLHttpRequest();

		//internet explorer 6 or earlier
		else if (window.ActiveXObject)
			req=new ActiveXObject("Microsoft.XMLHTTP");

		req.onreadystatechange=processRequest;
		req.open("GET",url,true);
		req.send(null);
		
		//remember when we last requested
		lastRequest=now;

	}

}

function highlightSuggestions() {

	for (var s=1; s<=noOfSuggestions; s++) {

		//highlight this one
		if (currentSuggestion==s) {

			document.getElementById("suggestion"+s).style.backgroundColor="#316ac5";
			document.getElementById("suggestion"+s).style.color="#ffffff";

		//blank this one
		} else {

			document.getElementById("suggestion"+s).style.backgroundColor="#ffffff";
			document.getElementById("suggestion"+s).style.color="#000000";

		}

	}

}

function keyDown(e) {

	//tab key
	if (e.keyCode==9) {
	
		//something selected
		if (currentSuggestion>=1)
			clickSuggestion(currentSuggestion);
		
		return true;
		
	//escape
	} else if (e.keyCode==27) {
	
		//remove text
		theObject.value="";
		
		//clear suggestions
		clearSuggestions();
		
		return false;
	
	//up
	} else if (e.keyCode==38) {
	
		//there are some suggestions
		if (noOfSuggestions>0) {

			//more above
			if (currentSuggestion>=1) {

				currentSuggestion--;
				highlightSuggestions();

			}

		}
		
		return false;

	//down
	} else if (e.keyCode==40) {
	
		//there are some suggestions
		if (noOfSuggestions>0) {

			//more below
			if ((currentSuggestion+1)<=noOfSuggestions) {

				currentSuggestion++;
				highlightSuggestions();

			}

		//there is some input
		} else if (theObject.value!="")
			doRequest();

		//there is no input
		else
			clearSuggestions();
			
		return false;

	//enter
	} else if (e.keyCode==13) {
	
		//suggestion highlighted
		if (currentSuggestion>0) {
		
			//click suggestion
			clickSuggestion(currentSuggestion);
			
		}
		
		return true;
		
	}

}

function keyUp(e) {

	//not shift, up or down
	if ((e.keyCode!=16)&&(e.keyCode!=38)&&(e.keyCode!=40)) {

		currentSuggestion=0;

		//enter/tab
		if ((e.keyCode==9)||(e.keyCode==13)) {
		
			clearSuggestions();
			
			//enter and simple search or no suggestion selected
			return ((e.keyCode==13)&&((theForm=="simpleSearch")||(currentSuggestion==0)));

		//there is some input
		} else if (theObject.value!="") {
		
			//get suggestions
			doRequest();

		//there is no input
		} else {
		
			//clear suggestions
			clearSuggestions();
			
		}

	}

}

function processRequest() {

	//loaded
	if (req.readyState==4) {

		//ok
		if (req.status==200) {

			var response=unescape(req.responseText);
			var typed=escape(theObject.value);

			//no response
			if (response=="")
				clearSuggestions();

			//got response
			else {

				var rows=response.split("*");
				
				var theName;
				var theSize;

				noOfSuggestions=0;

				suggestions.style.display="";
				suggestions.style.left=""+findPosX(theObject)+"px";
				suggestions.style.top=""+(findPosY(theObject)+(theForm=="simpleSearch"?20:19))+"px";

				var r=1;
				
				while (document.getElementById("suggestion"+r)!=null) {
				
					//got a suggestion
					if (r<=rows.length) {

						theName=rows[r-1].substring(0,rows[r-1].indexOf("#"));
						theSize=parseInt(rows[r-1].substring(rows[r-1].indexOf("#")+1));
						
						//convert all + to space
						while (theName.indexOf("+")>=0)
							theName=theName.replace("+"," ");

						//show this row
						document.getElementById("suggestion"+r).style.display="";

						//update the text
						document.getElementById("suggestion"+r+"text").innerHTML="<span style=\"float:right;font-size:76%;padding-top:2px\">"+theSize+" result"+(theSize==1?"":"s")+"</span>"+highlightText(theName,unescape(typed));

						noOfSuggestions++;

					//no suggestion
					} else {

						//hide this row
						document.getElementById("suggestion"+r).style.display="none";

						//remove the text
						document.getElementById("suggestion"+r+"text").innerHTML="";

					}
					
					r++;

				}

				highlightSuggestions();

				suggestions.style.visibility="visible";

			}

		}

	}

}

function highlightText(text,typed) {

	var lastChar="";
	var started=false;
	var finished=false;

	var ret="";

	for (var c=0; c<text.length; c++) {
	
		//not started
		if (started!=true) {
		
			//first character of a word
			if ((c==0)||(lastChar==" ")) {
			
				//word starts with typed
				if (text.toLowerCase().substring(c,c+typed.length)==typed.toLowerCase()) {
				
					ret+="<span class=\"highlight\">";
					started=true;
					
				}
				
			}
			
		}
		
		ret+=text.charAt(c);
		
		//not finished
		if (finished!=true) {
		
			//ends with typed
			if ((ret.length>=typed.length)&&(ret.substring(ret.length-typed.length).toLowerCase()==typed.toLowerCase())) {
			
				ret+="</span>";
				finished=true;
				
			}
			
		}
		
		//remember last character
		lastChar=text.charAt(c);
		
	}
	
	return ret;

}

function removeTags(text) {

	var inTag=false;
	var ret="";
	
	var tagsEnded=0;
	
	for (var c=0; c<text.length; c++) {
	
		//start of tag
		if (text.charAt(c)=='<')
			inTag=true;
			
		//end of tag
		if (text.charAt(c)=='>') {
		
			inTag=false;
			tagsEnded++;
			
		}
			
		//not in tag or first piece of text
		else if ((inTag!=true)&&(tagsEnded>=2))
			ret+=text.charAt(c);
			
	}

	while (ret.indexOf("&amp;")>=0)
		ret=ret.replace("&amp;","&");

	return ret;

}

//from http://www.quirksmode.org/js/findpos.html
function findPosX(obj) {

	var curleft=0;

	if (obj.offsetParent) {

		while(1) {

			curleft+=obj.offsetLeft;

			if (!obj.offsetParent)
				break;

			obj=obj.offsetParent;

		}

	} else if (obj.x)
		curleft+=obj.x;

	return curleft;

}

function findPosY(obj) {

	var curtop=0;

	//browser supports offsetParent
	if (obj.offsetParent) {

		//count each parent in turn
		while (obj.offsetParent) {

			curtop+=obj.offsetTop;
			obj=obj.offsetParent;

		}

	//netscape 4
	} else if (obj.y)
		curtop+=obj.y;

	return curtop;

}