﻿// ----------------------------------------------
// File:		TypeAheadWidget.js
// Author:		Nathan Derksen
// ----------------------------------------------

// ----------------------------------------------
// Function:	TypeAheadWidget()
// Author:		Nathan Derksen
// Description:	Base class
// Inputs:		<None>
// Returns:		<Nothing>
// ----------------------------------------------
function TypeAheadWidget()
{
	this.pInstance = null;
	this.pHandle = null;
	this.pRelativeElement = null;
	this.pShowIntervalId = 0;
	this.pHideIntervalId = 0;
	this.pShowDelay = 400;
	this.pHideDelay = 400;
	this.pContentArray = [];
	this.pHilightedRow = -1;
	this.isVisible = false;
	this.lastX = -1;
	this.lastY = -1;
	this.hilightedRowClass = "typeAheadRowHilighted";
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.assignDiv = function(elementId)
{
	this.pHandle = document.getElementById(elementId);
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.positionRelativeTo = function(elementId)
{
	this.pRelativeElement = document.getElementById(elementId);
	
//	var position = BrowserUtils.getPosition(this.pRelativeElement);
	var width = Number(this.pRelativeElement.style.width.split("px").join(""));
	var height = Number(this.pRelativeElement.style.height.split("px").join(""));
//	var top = this.pRelativeElement.offsetTop;
//	var left = this.pRelativeElement.offsetLeft;
//	var top = position.top;
//	var left = position.left;

	this.pHandle.style.top = String(height + 3) + "px";
	this.pHandle.style.left = "0px";
	this.pHandle.style.width = width + "px";
};

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.getItem = function(indexNum)
{
	if (indexNum < this.pContentArray.length && indexNum >= 0)
	{
		return this.pContentArray[indexNum];
	}
	return null;
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.setContents = function(contents)
{
	this.pContentArray = contents;
	
	this.draw();
	
	if (contents != null && contents.length > 0)
	{
		this.showWidget();
	}
	else
	{
		this.hideWidgetNow();
	}
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.draw = function()
{
	var html = "<ul>";
	
	for (var i=0; i < this.pContentArray.length; i++)
	{
		if (i == this.pHilightedRow)
		{
			html += '<li class="' + this.hilightedRowClass + '"><a href="javascript:;" onclick="TypeAheadWidget_handleClick(' + this.pContentArray[i].id + ',\'' + escape(this.pContentArray[i].label) + '\');">' + this.pContentArray[i].label + '</a></li>';
		}
		else
		{
			html += '<li><a href="javascript:;" onclick="TypeAheadWidget_handleClick(' + this.pContentArray[i].id + ',\'' + escape(this.pContentArray[i].label) + '\');">' + this.pContentArray[i].label + '</a></li>';
		}
	}
	html += "</ul>";
	this.pHandle.innerHTML = html;
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.getContents = function()
{
	return this.pContentArray;
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.getItem = function(id)
{
	return this.pContentArray[id];
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.showWidget = function(contents)
{
	this.pHandle.style.display = "block";
	this.pHilightedRow = -1;
	this.isVisible = true;
	clearTimeout(this.pHideIntervalId);
};


// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.hideWidget = function()
{
	this.isVisible = false;
	clearTimeout(this.pShowIntervalId);
	clearTimeout(this.pHideIntervalId);
	this.pHideIntervalId = setTimeout("hideWidgetFollowup()", this.pHideDelay);
};

// ----------------------------------------------
// ----------------------------------------------
function hideWidgetFollowup()
{
	TypeAheadWidget_instance.hideWidgetNow();
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.hideWidgetNow = function()
{
	clearTimeout(this.pShowIntervalId);
	clearTimeout(this.pHideIntervalId);
	this.pHandle.style.display = "none";
	this.isVisible = false;
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.setHighlightedRowNum = function(rowNum)
{
	if (rowNum < this.pContentArray.length)
	{
		this.pHilightedRow = rowNum;
	}
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.getHighlightedRowNum = function()
{
	return this.pHilightedRow;
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.getHighlightedRow = function()
{
	if (this.pHilightedRow > -1 && this.pHilightedRow < this.pContentArray.length)
	{
		return this.pContentArray[this.pHilightedRow];
	}
	return null;
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.moveHilightUp = function()
{
	if (this.pHilightedRow == -1 && this.pContentArray.length > 0)
	{
		this.pHilightedRow = this.pContentArray.length - 1;
		this.draw();
	}
	else if (this.pHilightedRow > 0)
	{
		this.pHilightedRow--;
		this.draw();
	}
	this.draw();
}

// ----------------------------------------------
// ----------------------------------------------
TypeAheadWidget.prototype.moveHilightDown = function()
{
	if (this.pHilightedRow == -1 && this.pContentArray.length > 0)
	{
		this.pHilightedRow = 0;
		this.draw();
	}
	else if (this.pHilightedRow < this.pContentArray.length - 1)
	{
		this.pHilightedRow++;
		this.draw();
	}
}

// ----------------------------------------------
// ----------------------------------------------
function TypeAheadWidget_handleClick(id, label)
{
	label = unescape(label);
	label = label.split("<b>").join("");
	label = label.split("</b>").join("");
	label = label.split("<B>").join("");
	label = label.split("</B>").join("");
	generateEvent("onLookaheadItemSelected", id, label);
	
	TypeAheadWidget_instance.hideWidget();
}

var TypeAheadWidget_instance = new TypeAheadWidget();