$(document).ready(function() {
	
	// TEXT INPUT FILTER FUNCTIONALITY DONE DIRT SIMPLE
	
	// If a user types something, deletes something, or copies and paste.
	$( "#product_filter_form input")
	.change( function(event) {
		filter( $(this).val() );
	})
	.keyup( function( event) {
		filter( $(this).val() );
	})
	.onpaste( 	function( event) {
		filter( $(this).val() );
	});
	
	// Filter product names
	function filter( string ) {
		
		// Clean up the input
		string = string.toUpperCase();
		string = string.replace( ' ', '' );
		
		$( ".result").each ( function() {
			
			// Clean up the product name
			var name = $( this ).attr('id').toUpperCase();;
			name = name.replace( ' ', '' );
						
			// If the name doesn't contain any of the letters
			// hide it.
			if ( string == '' ) {
				$( this ).show();
			}
			
			// indexOf is funky.  It returns the index
			// of the first occurence of the string.  This is 0
			// if it's the first letter of the string.  It returns
			// -1 if no match is found.  A +1 will fix a match of
			// the first letter throwing a negative.
			else if ( name.indexOf( string ) + 1 ) {
				$( this ).show();
			}
			else {
				$( this ).hide();
			}
			
		});
	}
});