function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};

var pos = 0;
var count = 0;
var minlength = 2;

function noenter(key) {
	suggcont = document.getElementById("suggcontainer");
	if (suggcont.style.display == "block") {
		if (key == 13) {
			choiceclick(document.getElementById(pos));
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

document.onclick = function () { closechoices(); }

function suggest(key,query) {
	if (key == 38) {
		goPrev();
	} else if (key == 40) {
		goNext();
	} else if (key != 13) {
		if (query.length > minlength) {
			t = new Date();
			advAJAX.get({
    				url: x_root_url + 'out.php?' + x_module_access +"=out&" + x_action_access + "=suggest&cmd=uname&query=" +query+ "&ncache="+t.getTime(),
    				onSuccess : function(obj) { update(obj.responseText); },
    				onError : function(obj) { alert("Error: " + obj.status); }
			});
		} else {
			update('');
		}
	}
}

function suggestTag(key,query) {
	if (key == 38) {
		goPrev();
	} else if (key == 40) {
		goNext();
	} else if (key != 13) {
		if (query.length > minlength) {
			t = new Date();
			advAJAX.get({
    				url: x_root_url + 'out.php?' + x_module_access +"=out&" + x_action_access + "=suggest&cmd=tag&query=" +query+ "&ncache="+t.getTime(),
    				onSuccess : function(obj) { update(obj.responseText); },
    				onError : function(obj) { alert("Error: " + obj.status); }
			});
		} else {
			update('');
		}
	}
}

function update(result) {
	arr = new Array();
	arr = result.split('\r\n');

	if (arr.length > 10) {
		count = 10;
	} else {
		count = arr.length;
	}

	suggdiv = document.getElementById("suggestions");
	suggcont = document.getElementById("suggcontainer");
	if (arr[0].length > 0) {
		suggcont.style.display = "block";
		suggdiv.innerHTML = '';
		suggdiv.style.height = count * 20;
	
		for (i = 1; i <= count; i++) {
			novo = document.createElement("div");
			suggdiv.appendChild(novo);
			novo.id = i;
			novo.className = 'x_suggestUnSelected';
			novo.onmouseover = function() { select(this,true); }
			novo.onmouseout = function() { unselect(this,true); }
			novo.onclick = function() { choiceclick(this); }
			opt = arr[i-1].split('\t');
			novo.ref = opt[0];
			/*if(opt[0] != '' && opt[0] != opt[1]) 
				novo.innerHTML = opt[1] + ' (' + opt[0] + ')';
			else
				novo.innerHTML = opt[0];*/
			if(opt[0] != '' && opt[0] != opt[1]) 
				novo.innerHTML = opt[1];
			else
				novo.innerHTML = opt[0];
		}
	} else {
		suggcont.style.display = "none";
		count = 0;
	}
}

function select(obj,mouse) {
	obj.className = 'x_suggestSelected';
	if (mouse) {
		pos = obj.id;
		unselectAllOther(pos);
	}
}

function unselect(obj,mouse) {
	obj.className = 'x_suggestUnSelected';
	if (mouse) {
		pos = 0;
	}
}

function goNext() {
	if (pos <= count && count > 0) {
		if (document.getElementById(pos)) {
			unselect(document.getElementById(pos));
		}
		pos++;
		if (document.getElementById(pos)) {
			select(document.getElementById(pos));
		} else {
			pos = 0;
		}
	}
}

function goPrev() {
	if (count > 0) {
		if (document.getElementById(pos)) {
			unselect(document.getElementById(pos));
			pos--;
			if (document.getElementById(pos)) {
				select(document.getElementById(pos));
			} else {
				pos = 0;
			}
		} else {
			pos = count;
			select(document.getElementById(count));
		}
	}
}

function choiceclick(obj) {
	document.getElementById("searchinput").value = obj.ref;
	count = 0;
	pos = 0;
	suggcont = document.getElementById("suggcontainer");
	suggcont.style.display = "none";
	document.getElementById("searchinput").focus();
}

function closechoices() {
	if(!document.getElementById("suggcontainer")) 
		return; 

	suggcont = document.getElementById("suggcontainer");
	if (suggcont.style.display == "block") {
		count = 0;
		pos = 0;
		suggcont.style.display = "none";
	}
}

function unselectAllOther(id) {
	for (i = 1; i <= count; i++) {
		if (i != id) {
			document.getElementById(i).className = 'x_suggestUnSelected';
		}
	}
}
