/*
	IncrementalSearch
	Copyright (c) Art. Lebedev Studio | http://www.artlebedev.ru/
	Andrew Shitov | ash@design.ru
	v. 2.0 (28.09.2005)
*/

var KEY_LENGTH = 3;
function IncrementalSearch (listID, filterID)
{
	this.List = document.getElementById (listID);
	this.Filter = document.getElementById (filterID);

	if (!this.List || !this.Filter) return null;

	this.Initialize();

	return this;
}

IncrementalSearch.prototype.Initialize = function()
{
	this.SearchList = new Array();
	this.ItemsList = new Array();

	this.WordIndex = new Array();

	this.cacheListData();

	this.Filter.IncrementalSearch = this;
	this.Filter.onkeyup = this.matchFilter;

	//this.Filter.focus();
}

IncrementalSearch.prototype.cacheListData = function()
{	
	var c = 0;
	var Items = this.List.getElementsByTagName("li");
	for (var itemIndex = 0; itemIndex != Items.length; itemIndex++)
	{
		var currentItem = Items[itemIndex];
		var parentTagName = currentItem.parentNode.tagName.toLowerCase();
		if (parentTagName != 'ul') continue;
		
		this.ItemsList[c] = currentItem;

		var currentText = currentItem.firstChild.innerHTML.toLowerCase();
		
		this.SearchList[c] = currentText;
		this.indexText (c, currentText);

		c++;
	}
}

IncrementalSearch.prototype.matchFilter = function()
{
	if (!this.IncrementalSearch) return false;

	var search = this.IncrementalSearch;
	var filterValue = this.value.toLowerCase();
	filterValue = filterValue.replace (/\s+$/g, "");
	filterValue = filterValue.replace (/^\s+/g, "");
	filterValue = filterValue.replace (/\s+/g, " ");
	
	if (filterValue != '')
	{

		var words = filterValue.toLowerCase().split (' ');
		var selectedItems = new Array();
		for (var c = 0; c != words.length; c++)
		{
			if (words[c] == '') continue;

			var key = words[c].substr (0, KEY_LENGTH);
			if (this.IncrementalSearch.WordIndex[key])
			{
				// WordIndex[key] содержит список номеров строк, номера соответствуют индексам массива ItemsList,
				// который содержит указатели на строки.
				var selected = this.IncrementalSearch.WordIndex[key];
				
				for (var s = 0; s != selected.length; s++)
					selectedItems[selected[s]] = true;
			}
		}

		if (filterValue.length <= KEY_LENGTH)
		{
			for (var r = 0; r != search.ItemsList.length; r++)
				if (search.ItemsList[r])
				{
					if (selectedItems[r])
					{
						cmnRemove_class(search.ItemsList[r], 'hidden');
					}
					else
					{
						cmnSet_class(search.ItemsList[r], 'hidden');
					}
				}
		}
		else
		{
			for (var r = 0; r != search.ItemsList.length; r++)
				if (search.ItemsList[r])
				{
					if (selectedItems[r] && search.SearchList[r].indexOf (filterValue) != -1)
					{
						cmnRemove_class(search.ItemsList[r], 'hidden');
					}
					else
					{
						cmnSet_class(search.ItemsList[r], 'hidden');
					}
				}
		}
	}
	else
	{
		for (var r = 0; r != search.ItemsList.length; r++)
			cmnSet_class(search.ItemsList[r], 'hidden');
	}

	// сдвигаем блок ATMsInfo вниз при слишком длинном списке городов в инкр. поиске
	// см. cities-switch.js
	if ( window.shiftATMsInfo )
	{
		shiftATMsInfo();
	}
}

IncrementalSearch.prototype.indexText = function (itemIndex, text)
{
	text = text.replace ('"', '');
	text = text.replace ('-', '');
	text = text.replace ('(', '');
	text = text.replace (')', '');
	var words = text.split (' ');
	for (var c = 0; c != words.length; c++)
	{
		var key = words[c].toLowerCase();
		
		this.storeKey (itemIndex, key);
	}
}

IncrementalSearch.prototype.storeKey = function (itemIndex, key)
{
	for (var c = 1; c <= KEY_LENGTH; c++)
	{
		var subkey = key.substr (0, c);

		if (!this.WordIndex[subkey])
			this.WordIndex[subkey] = new Array();
		
		this.WordIndex[subkey].push (itemIndex);
	}
}
