/*2008-07-14*/
/*global searchwords, getContents, addClassName, removeClassName */

function sanitize(x)
{
	//stripslashes, replace parentheses, replace non-spaces, then replace spaces at the end
	return x.replace(/\\/g, '').replace(/[()]+/g, '').replace(/\*/g, ' ').replace(/^\s+|\s+$/g, ' ').replace(/\s+$/, '').replace(/\[ Top \]/ig, '').replace(/&amp;/g, '&').toLowerCase();
}




function sift(searchBox, items, thisClassName, searchType)
{

	var filterTerms = sanitize(searchBox.value).split(' ');

	for (var i = 0; i < items.length; i++)
	{
		var allTermsMatch = true;

		var thisItem;
		var thisStuff = '';
		var targetItem;

		switch (searchType)
		{
		case 'faq':
			thisItem = items[i];
			targetItem = thisItem;
			thisStuff = sanitize(getContents(thisItem));
			break;

		case 'posts':
			thisItem = items[i];
			targetItem = thisItem;
			thisStuff = sanitize(getContents(thisItem));
			break;

		case 'navbar':
			thisItem = items[i];
			targetItem = thisItem;
			thisStuff = sanitize(getContents(thisItem)) + ' ' + searchwords[i];
			break;

		case 'smiley':
			thisItem = items[i];
			targetItem = thisItem.parentNode;

			if (thisItem.alt)
			{
				thisStuff += thisItem.alt + ' ';
			}
			if (thisItem.src)
			{
				thisStuff += thisItem.src.substring(thisItem.src.lastIndexOf('/') + 1, thisItem.src.lastIndexOf('.')) + ' ';
			}
			if (thisItem.title)
			{
				thisStuff += thisItem.title + ' ';
			}
			break;

		case 'tables':
			thisItem = items[i];
			targetItem = thisItem;
			thisStuff = sanitize(getContents(thisItem));
			break;
		}

		if (!(thisItem.className === 'cat' && searchType !== 'navbar') && thisItem.className !== 'faq2')
		{
			for (var j = 0; j < filterTerms.length; j++)
			{
				if (!thisStuff.match(filterTerms[j]) && !(searchType === 'tables' && i === 0))
				{
					addClassName(targetItem, thisClassName);
					allTermsMatch = false;
					break;
				}
				else if (allTermsMatch !== false)
				{
					removeClassName(targetItem, thisClassName);
				}
			}
		}
	}
}