/**
 * @author Reto Bisang
 */
function PLiveSearch(inputBoxId, inputBoxWidth, searchUrl, handlerUrl, resultContainerId)
{
	var me=this;
	this._maxResults = 10;
	this._ajaxLiveSearch = new PAjax();
	this._searchUrl = searchUrl;
	this._handlerUrl = handlerUrl;
	this._currentIndex = -1;
	this._inputBox = pge(inputBoxId);
	this._inputBoxWidth = inputBoxWidth;

	this.inputBoxKeyPressEvent = function(e)
	{
		var e = e || event;

		if ((e.keyCode||e.which) == 13) { // Enter (submit)
			if (!me._searchUrl) {
				me.delayedSearch();
				return;
			}
			
			if (me._currentIndex == -1) {
				if (me._inputBox.value == '') {
					return;
				}
				window.location.href = me._searchUrl+me._inputBox.value;
			} else {
				// find url
				currentElement = pge('lsrurl-'+me._currentIndex);
				me.navigate(currentElement.href);
			}
		} else if ((e.keyCode||e.which) == 38) { // Up
			index = me._currentIndex;
			
			if (index == -1) {
				index = 1;
			} else {
				index--;
			}
			
			me.refreshResultSelection(index);
		} else if ((e.keyCode||e.which) == 40) { // Down
			index = me._currentIndex;
			
			if (index == -1) {
				index = 1;
			} else {
				index++;
			}
			
			me.refreshResultSelection(index);
		} else { // Search
			me.delayedSearch();
		}
	};

	this.searchResult = function(responseText)
	{
		me._resultContainer.innerHTML = responseText;
		me._resultContainer.style.display = '';
	};
	
	this.registerInputBox();
	
	if (!resultContainerId) {
		this.createResultContainer();
	} else {
		this._resultContainer = pge(resultContainerId);
	}
}

PLiveSearch.prototype.changeSelection=function changeSelection(element, hover)
{
	if (hover) {
		element.className = "live-search-row-on";
	} else {
		if (element.id != ('lsr-'+this._currentIndex)) {
			element.className = "live-search-row-off";
		}
	}
};

PLiveSearch.prototype.navigate=function navigate(url)
{
	this._resultContainer.style.display = 'none';
	window.location.href=url;
};

PLiveSearch.prototype.refreshResultSelection=function refreshResultSelection(index)
{
	currentElement = pge('lsr-'+index);
	
	if (currentElement) {
		if (this._currentIndex != -1) {
			oldElement = pge('lsr-'+this._currentIndex);
			
			if (oldElement) {
				oldElement.className = "live-search-row-off";
			}
		}

		currentElement.className = "live-search-row-on";
		this._currentIndex = index;
	}
};

PLiveSearch.prototype.registerInputBox=function registerInputBox()
{
	p_addListener(this._inputBox, "keyup", this.inputBoxKeyPressEvent);
};

PLiveSearch.prototype.createResultContainer=function createResultContainer()
{
	var resultContainer, body, posX, posY;
	resultContainer = pce('div', {_className:'livesearch-resultbox', 'zIndex':'40', position:'absolute', top:'23px', width:this._inputBoxWidth+'px'});
	this._inputBox.parentNode.appendChild(resultContainer);
	this._resultContainer = resultContainer;
};

PLiveSearch.prototype.delayedSearch = function() {
	if(this.timeout) {
		clearTimeout(this.timeout);
	}
	
	var func = this.search.pbind(this);
	this.timeout=setTimeout(func, 300);
}

PLiveSearch.prototype.search=function ()
{
	var requestUrl;
	this._currentIndex = -1;

	if (this._inputBox.value == '' || this._inputBox.value.length <= 2) {
		this._resultContainer.style.display = 'none';
		return;
	}

	requestUrl = this._handlerUrl+'?search='+this._inputBox.value;
	if(requestUrl != this.lastRequestUrl) {
		this._ajaxLiveSearch.sendGetRequest(requestUrl, this.searchResult);
		this.lastRequestUrl = requestUrl;
	}
};