/* ---------------------------- */
/* XMLHTTPRequest Enable 		*/
/* ---------------------------- */
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
	request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_type = new XMLHttpRequest();
	}
		return request_type;
}

var http = createObject();
// global variables
var acListCurrent = -1;


/* -------------------------- */
/* SEARCH					 */
/* -------------------------- */
function autosuggest(e) {
		
		// check for enter key presses
		if(window.event) // IE
		{
			keynum = e.keyCode;
		}
		else if(e.which) // Netscape/Firefox/Opera
		{
			keynum = e.which;
		} 
		
		switch(keynum)
		{
			case 27: clearAutoComplete(); break;
			case 40: updownArrow(keynum); break;
			case 38: updownArrow(keynum); break;	
			case 13: GotoSelected(); break;
			default: 
				acListCurrent = -1;			
				q = document.getElementById('search-q').value;
				// Set te random number to add to URL request
				nocache = Math.random();
				http.open('get', '../ajax/search.php?q='+q+'&nocache = '+nocache);
				http.onreadystatechange = autosuggestReply;
				http.send(null);			
			
			break;
		}

}

function GotoSelected()
{
	if (acListCurrent != -1)
	{
		aSearchItem = resElement.childNodes[0].childNodes[acListCurrent];
		location.href = aSearchItem.firstChild.href;
		return false;
	}
	else	
		return true;

/*		{
		searchTerm = document.getElementById('search-q').value;
		location.href = 'archive.php?search='+searchTerm;
	}*/
}

function autosuggestReply() {
	if(http.readyState == 4){
		var response = http.responseText;
		e = document.getElementById('results');
		if(response!=""){
			e.innerHTML=response;
			e.style.display="block";
		} else {
			e.style.display="none";
		}
	}
}

// clear auto complete box
function clearAutoComplete()
	{
		acListCurrent = -1;
		document.getElementById('search-q').value = '';
		document.getElementById('results').style.display="none";
	}
	
	
	
// treat up and down key strokes defining the next selected element
function updownArrow(keyCode) {
	if(keyCode == 40 || keyCode == 38){

		resElement = document.getElementById('results');
		iSeachItemCount = resElement.childNodes[0].childNodes.length;
		
		if(keyCode == 38){ // keyUp
			if(acListCurrent == 0 || acListCurrent == -1){
				acListCurrent = iSeachItemCount-1;
			}else{
				acListCurrent--;
			}
		} else { // keyDown
			if(acListCurrent == iSeachItemCount-1){
				acListCurrent = 0;
			}else {
				acListCurrent++;
			}
		}

		// loop through each result div applying the correct style
		
		for(i=0;i<resElement.childNodes[0].childNodes.length;i++)
		{
			aSearchItem = resElement.childNodes[0].childNodes[i];
			if(i == acListCurrent)			
				aSearchItem.className = "selected";
			else
				aSearchItem.className = "unselected";		
		}
		

		return true;
	} else {
		// reset
		acListCurrent = -1;
		return false;
	}
}
