var searchTimeout = null;
var timerDelay = 400;

var animateTimeout = null;
var msPerMovement = 1;
var absPixels = 5;
var open = false;

function searchFromNav() {
	var query = document.getElementById("search_from_nav");
	location.href = "/dictionary/search/"+query.value;
}

function processTextInNavSearch(e) {
	var query = document.getElementById("search_from_nav");
	var inBox = query.value;
	var divObj = document.getElementById("search_div");
	if (!open) {
		divObj.style.visibility = "hidden";
		divObj.style.display = "none";
		divObj.style.left = "5px";
	}
	if (inBox.length == 0) {
		window.clearTimeout(searchTimeout);
		window.clearTimeout(animateTimeout);
		divObj.style.visibility = "hidden";
		divObj.style.display = "none";
		divObj.style.left = "5px";
		open = false;
		return false;
	}
	if (e.keyCode == 13) {
		searchFromNav();
		return false;
	}
	
	window.clearTimeout(searchTimeout);
	searchTimeout = window.setTimeout(requestSearch, timerDelay);
}

function moveWindow() {
	var divObj = document.getElementById("search_div");
	divObj.style.left = absPixels+"px";
	absPixels =  absPixels * 1 + 10;

	if (absPixels < 125) {
		animateTimeout = window.setTimeout(moveWindow, msPerMovement);
	} else {
		absPixels = 5;
		open = true;
	}
	
}

function requestSearch() {
	var xmlhttp = getXmlHttp();
	
	var query = document.getElementById("search_from_nav");
	
	var page = "/dictionary/search/"+query.value+"/ajax=true";
	var divObj = document.getElementById("search_div");
	divObj.innerHTML = "<img id=\"curve_search_upper\" src=\"/images/curve-bl.gif\" alt=\"\" /><div id=\"search_div_text\" onclick=\"clickedDiv();\">Searching for \""+query.value+"\" ...</div><img id=\"curve_search_bottom\" src=\"/images/curve-ul.gif\" alt=\"\" />";
	divObj.style.display ="block";
	divObj.style.visibility = "visible";
	if (!open) {
		animateTimeout = window.setTimeout(moveWindow, msPerMovement);
	}
	xmlhttp.open("GET", page);
	xmlhttp.onreadystatechange = function () {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

				if (xmlhttp.responseText.match(/^([^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\n)+$/)) {
					var words = xmlhttp.responseText.split("\n");
					
					var output = "";
					for (var i = 0; i < (words.length-1); i++) {
						var word = words[i].split("\t");
						output += "<a href=\"/dictionary/search/"+word[0]+"/strict=true\">"+word[0]+"</a> - /<b>"+word[1]+"</b>/ : "+word[2]+" <i><b>"+word[3]+"</b></i><br />\n";
					}
					divObj.innerHTML = "<img id=\"curve_search_upper\" src=\"/images/curve-bl.gif\" alt=\"\" /><div id=\"search_div_text\" onclick=\"clickedDiv();\">"+output+"</div><img id=\"curve_search_bottom\" src=\"/images/curve-ul.gif\" alt=\"\" />";
				} else {
					divObj.innerHTML = "<img id=\"curve_search_upper\" src=\"/images/curve-bl.gif\" alt=\"\" /><div id=\"search_div_text\" onclick=\"clickedDiv();\">No results found.</div><img id=\"curve_search_bottom\" src=\"/images/curve-ul.gif\" alt=\"\" />";
				}
			}
	}
	
	xmlhttp.send(null);
	
}

var divClicked = false;

function clickedDiv() {
	divClicked = true;
}
function clickedBody() {
	if (divClicked) {
		divClicked = false;
		return false;
	}
	var divObj = document.getElementById("search_div");
	divObj.style.visibility = "hidden";
	divObj.style.display = "none";
	divObj.style.left = "5px";
	open = false;
	
}