
function AutoSuggest(id, suggestId, displayNum, submitForm, file, settings)
{
	var defaults = {
		lang: null,
		useSE: null,
		delegator: null,
		dividerText: null,
		dividerType: null,
		textColor: '#000000',
		highlightTextColor: '#000000',
		backgroundColor: 'transparent',
		highlightColor: '#C4C4C4'
	};

	var settings = jQuery.extend(defaults, settings);

	this.settings = settings;
	this.source = new Array();
	this.file = file;
	this.id = id;
	this.inputNode = document.getElementById(id);
	this.submitForm = submitForm;
	this.suggestId = suggestId;
	this.suggestNode = document.getElementById(suggestId);
	this.displayNum = displayNum;
	this.field = (id.indexOf('What') > -1) ? 'WHAT' : 'WHERE';
	this.useSE = settings.useSE;
	this.lang = settings.lang;
	this.delegator = settings.delegator;
	this.suggestionIndex = -1;
	this.suggestedItems = null;

	this.inputNode.onkeydown = this.keydown;
	this.inputNode.onkeyup = this.keyup;
	this.inputNode.suggest = this; //pointer to AutoSuggest Object
	this.inputNode.suggestbox = this.suggestNode;
	this.inputNode.onblur = function() { this.suggest.hide(); };
	this.suggestNode.onmousedown = this.mousedown;
	this.suggestNode.inbox = this.inputNode;
	this.suggestNode.suggestbox = this;

//	this.inputNode.value = '';
	this.suggestNode.value = '';
	this.inprogress = false; // ready for autocomplete query the first time
	this.navigating = false;
}

AutoSuggest.prototype.getdatasource = function()
{
	var thisAS = this; // save away this AutoSuggest object as the meaning of "this" inside Ajax.Request object changes to point to the Ajax.Request object

	var userInput = this.inputNode.value;
	if (userInput.length == 0) { // don't make an ajax request when no userinput
		this.source = new Array();
		this.hide();
		return;
	}

	if (this.inprogress) return;
	if (this.inputNode == null) return;

	//var url = '/tools/autosuggestlist.html?f=suggest-where.tab&t=';
	var url = '/tools/autosuggestlist.html';
	this.inprogress = true;

	jQuery.ajax ({
		url: url,
		dataType: "json",
		data: {f: this.file, t: userInput, c: thisAS.inputNode.name, lang: this.lang, fld: this.field, SE: this.useSE},
		success: function(result) {
			// Should use JSON parsing methods from prototype instead of eval'ing the results 
			thisAS.source = result;
			thisAS.inprogress = false;
			thisAS.suggest();
		}
	});
}

AutoSuggest.prototype.keyup = function(event)
{
	event = event || window.event;
	var keyValue = (event.keyCode) ? event.keyCode : (event.which) ? event.which : null;
	switch(keyValue)
	{
		case 38: //up arrow
				this.suggest.moveup();
				break;
		case 40: //down arrow
				this.suggest.movedown(event);
				break;
		default:
				if (keyValue != 13) {		
					// NEW APPROACH: run getdatasource which runs ajax call and let onSuccess callback run AutoSuggest.suggest() instead of here in keyup
					this.suggest.getdatasource();
					//this.suggest.suggest();
				}
				break;
	}
}

AutoSuggest.prototype.keydown = function(event)
{
	event = event || window.event;
	var keyValue = (event.keyCode) ? event.keyCode : (event.which) ? event.which : null;

	if (13 == keyValue) {
		this.suggest.hide();
		wasNavigating = this.suggest.navigating;
		this.suggest.navigating = false;
		return !wasNavigating;
	}
}

AutoSuggest.prototype.moveup = function()
{
	this.navigating = true;
	if (this.suggestionIndex > 0)
	{
		this.suggestedItems.eq(this.suggestionIndex).css('background-color',this.settings.backgroundColor);
		this.suggestedItems.eq(this.suggestionIndex).css('color',this.settings.textColor);
		this.suggestionIndex--;

		this.suggestedItems.eq(this.suggestionIndex).css('background-color',this.settings.highlightColor);
		this.suggestedItems.eq(this.suggestionIndex).css('color',this.settings.highlightTextColor);
		this.inputNode.value = this.suggestedItems.eq(this.suggestionIndex).text();
	}
}

AutoSuggest.prototype.movedown = function()
{
	this.navigating = true;
	if (this.suggestionIndex < this.suggestedItems.size() - 1)
	{
		if (this.suggestionIndex >= 0) {
			this.suggestedItems.eq(this.suggestionIndex).css('background-color',this.settings.backgroundColor);
			this.suggestedItems.eq(this.suggestionIndex).css('color',this.settings.textColor);
		}

		this.suggestionIndex++;
		this.suggestedItems.eq(this.suggestionIndex).css('background-color',this.settings.highlightColor);
		this.suggestedItems.eq(this.suggestionIndex).css('color',this.settings.highlightTextColor);
		this.inputNode.value = this.suggestedItems.eq(this.suggestionIndex).text();
	}
}

AutoSuggest.prototype.mousedown = function(event)
{
	event = event || window.event;
	var clickedItem = (event.srcElement) ? event.srcElement : event.target;

	if (clickedItem.className == 'asItem') {
		var text = clickedItem.firstChild.nodeValue;
		this.inbox.value = text;
		this.suggestbox.hide();
		this.inbox.focus();
	}
	else
		return false;
}

// Modified because data no longer in "this.source" property of AutoSuggest.
AutoSuggest.prototype.suggest = function() {
	//if (this.source == null) this.source = dataSource; // Ajax call may not have been returned on initialization
	//if (this.source == null) return;

	var displayDividerAdded = false;
	var input = this.inputNode.value;
	if (!input || input == "") {
		this.hide();
	} else {
		if (this.source.length == 0) {
			this.hide();
		} else {
			var suggestions = "";
			for (i = 0; i < this.source.length; i++) {
				if (i >= this.displayNum) break;
				if (this.settings.dividerText != null && this.settings.dividerText != null && !displayDividerAdded &&
				    this.settings.dividerType != null && this.source[i].type == this.settings.dividerType) {
					suggestions += '<div class="asCachedSuggest"><div class="asDividerMarker">' + this.settings.dividerText + "</div>";
					displayDividerAdded = true;
				}
				suggestions += '<div class="asItem">' + this.source[i].value + '</div>';
			}
			if (displayDividerAdded)
				suggestions += "</div>";

			this.show(suggestions);
			this.suggestedItems = jQuery('#' + this.suggestId + ' .asItem');// update suggested list
		}
	}
}

AutoSuggest.prototype.hide = function()
{
	this.suggestNode.style.display='none';
	this.suggestionIndex = -1;
}

AutoSuggest.prototype.show = function(suggestions)
{
	this.suggestNode.style.display = 'block';
	this.suggestNode.innerHTML = suggestions;
}

