var httpKeyword = jsRemote();
var keyword_is_updated = 1;
var click_enable = true;
var curr = 0;
var ie_start = false;

function showKeywords() {
	if (!keyword_is_updated) return;
		keyword_is_updated = 0;
		
	if(!httpKeyword) {
		//alert("Your browser doesn't support Ajax.");
		return;
	} else {
		alt = document.getElementById('keyword');
		var txt = alt.value;
		if(txt.length > 3) {
			httpKeyword.open("GET","norw/keyword_search.php?random="+Math.random()+"&search=" + txt,true);
			httpKeyword.onreadystatechange = jsChangeKeywordState;
			httpKeyword.send(null);
		} else {
			callVisualClose();
			keyword_is_updated = 1;	
		}
		return false;
	}
}

function jsChangeKeywordState() {
	if (httpKeyword.readyState !=4) return;
	else {
		callVisual(httpKeyword.responseText);
		keyword_is_updated = 1;
	}
}

function callVisual(txt) {
	curr = 0;
	ie_start = false;
	if(txt.length) {
		obj = document.getElementById('keywordIn');
		obj.style.position = "absolute";
		obj.style.display = "block";
		var e = new Array();
		eval(txt);
		callPaint(e);
		alt = document.getElementById('keyword');
		alt.onclick = function () {
			click_enable = false	
		}

		document.body.onclick = function () {
			curr = 0;
			ie_start = false;
			if(!click_enable) {
				click_enable = true;
				return false;
			}
			callVisualClose();
		}
		
		document.body.onkeypress = function (anEvent) {
			if(navigator.appName.indexOf("Microsoft") != -1) {
				anEvent = window.event;
				anEvent.which = anEvent.keyCode;
			}
			if(anEvent.keyCode) {
				if (anEvent.keyCode==38 || anEvent.keyCode==40) return false;
			}
		}
		
		document.body.onkeydown = function (anEvent) {
			if(navigator.appName.indexOf("Microsoft") != -1) {
				anEvent = window.event;
				anEvent.which = anEvent.keyCode;
			}
			if(navigator.appName.indexOf("Microsoft") != -1) {
				if(anEvent.which == 27) {
					callVisualClose();
					return false;
				}
				if(anEvent.which==38 || anEvent.which==40 || anEvent.which==13) {
					if(ie_start) {
						if(anEvent.which == 40) {
							curr+= 1;
						} else if(anEvent.which == 38) {
							curr-= 1;
						} else if(anEvent.which == 13) {
							temp = document.getElementById('kwrd_'+curr);
							document.getElementById('keyword').value = temp.innerHTML;
							curr = 0;
							callVisualClose();
							return false;
						}
					}
					ie_start = true;
					callNavigate();
					return false;
				}
			} else {
				if(anEvent.which==38 || anEvent.which==40) {
					return false;
				}
				
			}
		}
		
		alt.onkeyup = function(anEvent) {
			if(navigator.appName.indexOf("Microsoft") != -1) {
				anEvent = window.event;
				anEvent.which = anEvent.keyCode;
			}
			if(anEvent.which == 40) {
				alt.blur();
				if(temp = document.getElementById('kwrd_0')) temp.style.backgroundColor = '#9FBFF9';
				document.getElementById('kwrd_fcs').focus();
				startNavigate();
			} else {
				showKeywords();
			}
		}
		
	} else {
		callVisualClose();
	}
}

function callVisualClose() {
	alt = document.getElementById('keyword');
	alt.onkeyup = showKeywords;
	alt.focus();
	obj = document.getElementById('keywordIn');
	obj.innerHTML = "";
	obj.style.display = "none";
}

function callValue(obj) {
	alt = document.getElementById('keyword');
	alt.value = obj.innerHTML;
}

function startNavigate() {
	obj = document.getElementById('kwrd_fcs');
	var i = 0;
	while(temp = document.getElementById('kwrd_'+i)) {
		i++;
	}
	obj.onkeyup = function(anEvent) {
			if(navigator.appName.indexOf("Microsoft") != -1) {
				anEvent = window.event;
				anEvent.which = anEvent.keyCode;
			}
			if(anEvent.which == 40) {
				curr+= 1;
			} else if(anEvent.which == 38) {
				curr-= 1;
			} else if(anEvent.which == 13) {
				temp = document.getElementById('kwrd_'+curr);
				document.getElementById('keyword').value = temp.innerHTML;
				curr = 0;
				callVisualClose();
				return false;
			} else if(anEvent.which == 27) {
				callVisualClose();
				return false;
			}
			callNavigate();
		}
}

function callNavigate() {
	if(curr > 4 ) {
		curr = 4;
	}
	if(curr < 0) {
		if(temp = document.getElementById('kwrd_0')) temp.style.backgroundColor = '#FAFBFF';
		document.getElementById('kwrd_fcs').blur();
		document.getElementById('keyword').focus();
		curr = 0;
		ie_start = false;
		return;
	}
	if(temp = document.getElementById('kwrd_'+(curr-1))) temp.style.backgroundColor = '#FAFBFF';
	if(temp = document.getElementById('kwrd_'+(curr+1))) temp.style.backgroundColor = '#FAFBFF';
	document.getElementById('kwrd_'+curr).style.backgroundColor = '#9FBFF9';
}

function _f(txt,e) {
	var i = new Object();
	i.txt = txt;
	e.push(i);
}

function callPaint(e) {
	obj = document.getElementById('keywordIn');
	obj.innerHTML = "";
	obj.style.display = 'block';
	var m = document.createElement("div");
	m.style.padding = '5px';
	m.innerHTML = "<a src='i/i.gif' width='1px' height='1px' id='kwrd_fcs'></a>";
	obj.appendChild(m);
	var cnt = 0;
	for (k in e){
	 	i = e[k];
		var n = document.createElement("div");
		n.id = 'kwrd_' + cnt;
		n.style.padding = '2px';
		n.style.cursor = 'pointer';
		n.onmouseover = function () {
			this.style.backgroundColor='#9FBFF9';
			
		}
		n.onmouseout = function () {
			this.style.backgroundColor='#FAFBFF';
		}
		n.onclick = function () {
			callValue(this);
		}
		n.innerHTML = i.txt;
		m.appendChild(n);
		cnt++;
	}
	
}
