/*2011-12-20*/
/*global searchwords, getContents, addClassName, removeClassName */

function sanitize(x) {
	"use strict";
	//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, '&').replace(/&#38;/g, '&').toLowerCase();
}




function sift(searchBox, items, thisClassName, searchType) {
	"use strict";
	var filterTerms = sanitize(searchBox.value).split(' '), i, allTermsMatch, thisItem, thisStuff, targetItem, j;

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

		thisStuff = '';

		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 (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);
				}
			}
		}
	}
}
