/*
	Tools/Chart/IncrementalSearch 
	Copyright (c) Art. Lebedev Studio | http://www.artlebedev.ru/
	Andrew Shitov | ash@design.ru
	v1.4.835 (05.11.2005) based on v. 2.0 (28.09.2005)
*/

var KEY_LENGTH = 3;

var AncorList = new Array();
var WordIndex1 = new Array();
var WordIndex2 = new Array();
var WordIndex3 = new Array();

var filterValue = '';
var Index = null;
var PrevIndex = null;
var Timer = null;
var isFirstPass = true;
var forceFilter = null;

var DefaultDelayValue = 2;
var WorkingDelayValue = 2;
var DelayValue = DefaultDelayValue;

var years = new Array();
var cats = new Array();
var selecting = new Array();
var monthIndexes = new Array();

var incrementalSearch = null;

function IncrementalSearch (tableID, filterID)
{
	this.Table = document.getElementById (tableID);
	this.Filter = document.getElementById (filterID);

	if (!this.Table || !this.Filter) return null;

	this.Initialize();
	
	if (this.Filter.value != '')
	{
		forceFilter = this.Filter;
		this.matchFilter();
	}
	return this;
}

IncrementalSearch.prototype.Initialize = function()
{
	this.buildIndexes();

	this.Rows = new Array();
	var trs = this.Table.getElementsByTagName ('TR');
	for (var c = 0; c != trs.length; c++)
		if (trs[c].className != 'portfolio-timeline' && trs[c].className != 'portfolio-total') push (this.Rows, trs[c]);

	incrementalSearch = this;
	this.Filter.IncrementalSearch = this;
	this.Filter.onkeyup = this.matchFilter;

	CacheMonthStat();

	this.Filter.focus();
}

IncrementalSearch.prototype.buildIndexes = function()
{	
	for (var c = 0; c != SearchList.length; c++)
	{
		AncorList[c] = document.getElementById ('a' + (c + 1)).parentNode;
		var currentText = SearchList[c];
		this.indexText (c, currentText);
	}
}

IncrementalSearch.prototype.matchFilter = function()
{
	var filter = forceFilter ? forceFilter : this;
	if (!filter.IncrementalSearch) return false;
	
	filterValue = filter.value.toLowerCase();

	filterValue = filterValue.replace ('-', ' ');
	filterValue = filterValue.replace (/\s+$/g, "");
	filterValue = filterValue.replace (/^\s+/g, "");
	filterValue = filterValue.replace (/\s+/g, " ");

	if (filterValue == '') DelayValue = DefaultDelayValue;

	QueueJob();
}

function QueueJob()
{
	switch (filterValue.length)
	{
		case 1:
			Index = WordIndex1[String (filterValue)];
			break;
		case 2:
			Index = WordIndex2[String (filterValue)];
			break;
		case 3:
			Index = WordIndex3[String (filterValue)];
			break;
		default:
			Index = WordIndex3[String (filterValue.substr (0, KEY_LENGTH))];
			break;
	}
	
	if (Timer) clearInterval (Timer);
	Timer = setInterval (ApplyFilter, DelayValue);
}

function ApplyFilter()
{
	if (Timer) clearInterval (Timer);

	if (filterValue) UpdateSelected();
	else RestoreOriginal();

	ColorizeLabels();
}

function RestoreOriginal()
{
	ClearPrevious();

	for (var c = 0; c != incrementalSearch.Rows.length; c++)
	{
		var current = incrementalSearch.Rows[c];
		if (current.className.substring (0, 1) == 'w')
			current.className = current.className.substring (1);			
	}

	document.getElementById ('StatCounter').innerHTML = '';
	ToggleMonthStat (true);

	isFirstPass = true;
}

function UpdateSelected()
{
	if (isFirstPass) BeforeFirstPass();
	else ClearPrevious()

	if (!Index)
	{
		document.getElementById ('StatCounter').innerHTML = '0';
		ColorizeLabels();
		return;
	}

	if (filterValue.length <= KEY_LENGTH) HighlightAsInIndex();
	else HighlightAsStartsInIndex();

	var c = 0;
	for (var x in selecting) c++;
	document.getElementById ('StatCounter').innerHTML = c;

	ToggleMonthStat (false);

	PrevIndex = Index;
}

function HighlightAsInIndex()
{
	years = [];
	cats = [];
	selecting = [];

	for (var c = 0; c != Index.length; c++)
	{
		var current = Index[c];

		AncorList[current].className = 'selected';

		years ['Y' + YearList[current]] = 1;	
		cats ['C' + CategoryList[current]] = 1;	
		selecting['x' + current] = 1;
	}
}

function HighlightAsStartsInIndex()
{
	years = [];
	cats = [];
	selecting = [];

	for (var c = 0; c != Index.length; c++)
	{
		var current = Index[c];

		var string = SearchList[current];
		var matched = true;		
		var words = filterValue.split (' ');
		for (var w = 0; w != words.length; w++)
		{
			if (string.indexOf (words[w]) == -1)
			{
				matched = false;
				break;
			}
		}
		if (matched)
		{
			AncorList[current].className = 'selected';
			
			years ['Y' + YearList[current]] = 1;
			cats ['C' + CategoryList[current]] = 1;
			selecting['x' + current] = 1;
		}
	}
}

function ColorizeLabels()
{
	if (MinYear >= MaxYear) return;

	var isEmptyFilter = filterValue == '';	
	var isEmptyIndex = (!Index || !Index.length) && !isEmptyFilter;

	for (var year = MinYear; year <= MaxYear; year++)
	{
		var label = 'Y' + parseInt ((String (year)).substring (2));
		var td = document.getElementById (label);
		if (td)
		{			
			if (td.className != '') td.className = td.className.replace ('grayed', '');
			if (isEmptyIndex || (!isEmptyFilter && !years[label])) td.className += ' grayed';
		}
	}

	var MinCat = CategoryList[0];
	var MaxCat = CategoryList[CategoryList.length - 1];
	if (MaxCat <= MinCat) return;

	for (var cat = MinCat; cat <= MaxCat; cat++)
	{
		var label = 'C' + cat;
		var span = document.getElementById (label);
		if (span)
		{			
			if (span.className != '') span.className = span.className.replace ('grayed', '');
			if (isEmptyIndex || (!isEmptyFilter && !cats[label])) span.className += ' grayed';
		}
	}
}

function ClearPrevious()
{
	if (!PrevIndex) return;
	for (var c = 0; c != PrevIndex.length; c++)
		AncorList[PrevIndex[c]].className = '';
}

function BeforeFirstPass()
{
	for (var c = 0; c != incrementalSearch.Rows.length; c++)
	{
		var current = incrementalSearch.Rows[c];
		if (current.className.substring (0, 1) != 'w')
			current.className = 'w' + current.className;
	}

	isFirstPass = false;
	DelayValue = WorkingDelayValue;
}

IncrementalSearch.prototype.indexText = function (rowIndex, text)
{	
	var words = text.split (' ');
	for (var c = 0; c != words.length; c++)
	{
		this.storeKeys (rowIndex, words[c]);
	}
}

IncrementalSearch.prototype.storeKeys = function (rowIndex, key)
{
	var subkey1 = key.substr (0, 1);
	var subkey2 = key.substr (0, 2);
	var subkey3 = key.substr (0, 3);

	if (!WordIndex1[subkey1]) WordIndex1[subkey1] = [rowIndex];
	else push (WordIndex1[subkey1], rowIndex);

	if (!WordIndex2[subkey2]) WordIndex2[subkey2] = [rowIndex];
	else push (WordIndex2[subkey2], rowIndex);

	if (!WordIndex3[subkey3]) WordIndex3[subkey3] = [rowIndex];
	else push (WordIndex3[subkey3], rowIndex);
}

function push (array, value)
{
	array[array.length] = value;
}

function ShowExample (who)
{
	document.getElementById ('FilterField').value = who.innerHTML;
	if (incrementalSearch)
	{
		forceFilter = incrementalSearch.Filter;
		incrementalSearch.matchFilter();
	}
	
	if (++ExamplePosition >= ExampleList.length) ExamplePosition = 0;
	who.innerHTML = ExampleList[ExamplePosition];
}

function CacheMonthStat()
{
	for (var c = 1; ; c++)
	{
		var statLabel = document.getElementById ('Stat' + c);
		if (!statLabel) break;
		monthIndexes[c - 1] = statLabel;
	}
}

function ToggleMonthStat (state)
{
	var visibility = state ? 'visible' : 'hidden';
	for (var c = 0; c != monthIndexes.length; c++)
		monthIndexes[c].style.visibility = visibility;
}

