/*
	This is the JavaScript file for the AJAX Suggest Tutorial

	You may use this code in your own projects as long as this 
	copyright is left	in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.DynamicAJAX.com
	
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	

*/
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser is too old for some of the functions of this site.\n\nTry upgrading or switching browser.");
	}
}

//document.getElementById('gwbody').onKeyUp = function () { alert('here'); };

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();
var highlighted = -1;
var maxvalue = -1;

function checksearch() {
	
	if(highlighted > -1) {
		//alert("highlighted is: "+highlighted);
		window.location = document.getElementById('innerlink_'+highlighted).href;
	}
	
	return false;
}

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest(e) {
	
	var unicode=e.keyCode? e.keyCode : e.charCode
	//alert(unicode);
	
	//alert(highlighted+" - "+maxvalue);
	
    if(unicode == 40)
    {
        if(highlighted < maxvalue-1 && highlighted > -2) {
			if(highlighted > -1) {
				document.getElementById('link_'+highlighted).className = 'suggest_link';
			}
			highlighted++;
			document.getElementById('link_'+highlighted).className = 'suggest_link_over';
			//document.links['innerlink_'+highlighted].focus();
			//document.getElementById('link_'+highlighted).style.background = '#9999FF';
			//document.getElementById('link_'+highlighted).focus();
		}
    }

    // When the up arrow key is pressed

    else if(unicode == 38)
    {
         if(highlighted > -1 && highlighted < maxvalue) {
			document.getElementById('link_'+highlighted).className = 'suggest_link';
			highlighted--;
			
			if(highlighted > -1) {
				document.getElementById('link_'+highlighted).className = 'suggest_link_over';
				//document.links['innerlink_'+highlighted].focus();	
			} else {
				//element.focus();
			}
			//document.getElementById('link_'+highlighted).focus();
		}
    } else if(unicode == 13) {
		
		// enter is pressed!
		//window.location = document.links['innerlink_'+highlighted].href;
		//alert("gone?");
		e.stop();
		
		
	} else {
		if (searchReq.readyState == 4 || searchReq.readyState == 0) {
			var str = escape(document.getElementById('txtSearch').value);
			searchReq.open("GET", '/ajax/searchSuggest.php?search=' + str, true);
			searchReq.onreadystatechange = handleSearchSuggest; 
			searchReq.send(null);
		}	
	}
	
	return false;
}

function unsearch() {
	setTimeout('hidesearch()',500);
}

function hidesearch() {
	var ss = document.getElementById('search_suggest');
	ss.style.display = 'none';
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		//alert("handling return..");
		var ss = document.getElementById('search_suggest')
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		//alert(str.length);
		//alert(searchReq.responseText);
		maxvalue = str.length-1;
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var content = str[i].split("||");
			var suggest = '<div ';
				suggest += 'onmouseover="javascript:suggestOver(this,'+i+');" ';
				suggest += 'onmouseout="javascript:suggestOut(this);" ';
				suggest += 'onclick="javascript:window.location = \''+content[2]+'\';" ';
				//alert('onclick="javascript:setSearch('+type[1]+');" ');
				suggest += 'class="suggest_link" id="link_'+i+'"><a href="'+content[2]+'" name="innerlink_'+i+'" id="innerlink_'+i+'" onkeyup="searchSuggest(event);">' + content[1] + '</a><br /><div class="light">' + content[0] +'</div></div>';
				ss.innerHTML += suggest;
			//ss.innerHTML += "Link<br />";
		}
		if(str.length > 1) {
			//alert("String length: "+str.length);
			
			highlighted = 0;
			document.getElementById('link_'+0).className = 'suggest_link';
			document.getElementById('link_'+0).className = 'suggest_link_over';
			
			ss.style.display = 'block';
		} else {
			highlighted = -1;
			ss.style.display = 'none';
		}
	}
}

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}
//Click function
function setSearch(value) {
	document.getElementById('txtSearch').value = value;
	document.getElementById('search_suggest').innerHTML = '';
	document.getElementById('search_suggest').style.display = 'none';
	
}