﻿
$(document).ready(
	function(){
		var allWords = [];
		var wordTest = {}
		$repTable = $('#repTable');
		$searchTerm = $('#searchTerm');
		var initialValue = $searchTerm.val();
		$repRows = $repTable.find('tbody tr').each( 
			function(){
				$this = $(this);
				var txt = $this.text();
				$this.find('td:not(:last)').each(
					function(){
						var myWords = $(this).text().split(/,/gi);
						for( var i in myWords ){
							if( ! wordTest[myWords[i]] ){
								wordTest[myWords[i]] = true;
								allWords.push( myWords[i] );
							}
						}
					}
				);
				$this.attr('txt',txt.toLowerCase());
			}
		 );
		allWords.sort();
		showTable = function(){
			var searchTerm = $searchTerm.val().replace(initialValue,'').toLowerCase();
			var i=0;
			$repRows.each(
				function(){
					if( ! searchTerm || $(this).attr('txt').indexOf( searchTerm ) != -1 ){
						$(this)
						 .show()
						 .attr('class', ( i++ % 2 == 0 ) ? 'even' : '');
					}
					else{
						$(this).hide();
					}
				}
			)
		}
		$searchTerm
			.autocomplete( allWords, {matchContains:true})
			.keyup(showTable)
			.change(showTable);
		showTable();
	}
);
