var IS_IE = true;
try
{
    window.attachEvent = window.HTMLDocument.prototype.attachEvent = window.HTMLElement.prototype.attachEvent = function (type, callback) {this.addEventListener(type.substring(2), callback, false);}
    window.detachEvent = window.HTMLDocument.prototype.detachEvent = window.HTMLElement.prototype.detachEvent = function (type, callback) {this.removeEventListener(type.substring(2),callback,false);}

    CSSStyleDeclaration.prototype.__defineGetter__('pixelLeft',function() {return parseInt(this.left) || 0;});
    CSSStyleDeclaration.prototype.__defineSetter__('pixelLeft',function(i) {this.left = i + "px";});
    CSSStyleDeclaration.prototype.__defineGetter__('pixelTop',function() {return parseInt(this.top) || 0;});
    CSSStyleDeclaration.prototype.__defineSetter__('pixelTop',function(i) {this.top = i + "px";});

    IS_IE = false;
}
catch(e) { }

AutoComplete = function(HTML_AJAX , input, suggestions)
{
	var timer;

	input.attachEvent("onkeyup", handleKeyUp);
	input.attachEvent("onkeydown", handleArrowKeys);

	function handleKeyUp(event)
	{
		if (event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13)
		{
			if (input.value.length == 0)
				suggestions.innerHTML = "";

			if (timer)
				clearTimeout(timer);

			if (input.value.length > 0)
				timer = setTimeout(reload, 200);
		}
		else if (event.keyCode == 13)
		{
		    clearTimeout(timer);
			var cursor = getCursor();
			var parent = suggestions.firstChild;


            // if there are no suggestions yet, i.e. the user types series id and hits enter before suggestions are loaded
            if(cursor == -1)
            {
                input.blur();
                return false;
            }
            // there is only one suggestion, such as when user enters series_id but they hit enter without moving down to select it
            else if(cursor != -1 && parent.childNodes.length ==1)
            {
		        input.value = parent.childNodes[0].childNodes[0].value;
				suggestions.innerHTML = "";
                input.blur();
                return false;
            }
            // an item in the suggestions box has been selected
			else if (cursor != -1 && cursor < parent.childNodes.length)
			{
		        input.value = parent.childNodes[cursor].childNodes[0].value;
				suggestions.innerHTML = "";
                input.blur();
                return false;
			}
		}
	}

	function reload()
	{
		//suggestions.reload({ prefix : input.value });
        	//HTML_AJAX.replace('suggestions', '/fga_link.php?action=findseries&value='+input.value+'&elem='+input.id);
		HTML_AJAX.grab('fga_link.php?action=findseries&value='+input.value+'&elem='+input.id,function(result) { if(input.value) suggestions.innerHTML = result; });

	}

	function handleArrowKeys(event)
	{
		try
		{
			var cursor = getCursor();
			var parent = suggestions.firstChild;

			if (cursor != -1 && (event.keyCode == 40 || event.keyCode == 38))
			{
                // down arrow
				if (event.keyCode == 40)
				{
					if (cursor == parent.childNodes.length)
						parent.childNodes[0].style.backgroundColor = "#a3ceff";
					else if (cursor < parent.childNodes.length - 1)
					{
						parent.childNodes[cursor].style.backgroundColor = "";
						parent.childNodes[cursor + 1].style.backgroundColor = "#a3ceff";
					}
				}
                // up arrow
				else
				{
					if (cursor > 0)
					{
						parent.childNodes[cursor].style.backgroundColor = "";
						parent.childNodes[cursor - 1].style.backgroundColor = "#a3ceff";
					}
				}
			}
		}
		catch (e) { }
	}

	function getCursor()
	{
		if (suggestions.innerHTML.length == 0)
			return -1;

		var parent = suggestions.firstChild;

		for (var i = 0; i < parent.childNodes.length; i++)
			if (
					parent.childNodes[i].style.backgroundColor == "#a3ceff" ||
					parent.childNodes[i].style.backgroundColor == "rgb(163, 206, 255)"
			   )
				return i;

		return parent.childNodes.length;
	}
}
