/*
 * jQuery Table Search plugin 2.0
 * Released: July 21, 2010
 * 
 * Modified 2010 Shawn McGinnis
 * Email: jquery@zxcdev.com
 * 
 * Copyright (c) 2008 Seetha Ramaiah Mangamuri
 * Email: M8R-tk5fe51@mailinator.com
 * 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * @license http://www.opensource.org/licenses/mit-license.php
 * @license http://www.gnu.org/licenses/gpl.html
 * @project jquery.searchable
 */

(function($){
    $.fn.searchable = function(){
        return this.each(function(){
            var targetTable = this;
            $('<input class="searchbox" />').insertBefore(targetTable).after('&nbsp;&nbsp; Ex. "103", "John", or "Friday George Sand"').before('<b>What are you looking for? &nbsp;&nbsp;</b>').keyup(function(event){
                event.preventDefault();
                var c = event.keyCode;
                // Check for conditions to trigger auto-search
                if ( (c == 8) || (c == 46) || (c == 109 || c == 189) || (c >= 65 && c <= 90) || (c >= 48 && c <= 57) ) {
					var search = $(this).val().split(" ");					
                    $('tbody tr', targetTable).each(function(){
                        var $tr = $(this);
                        $tr.filter(function(){
							var result = true;
							var keyword;
							for(i = 0; i < search.length && result; i++){
								keyword = new RegExp(search[i], "i");
								result = keyword.test($(this).html());
							}
							return result;
                        }).length ? $tr.show() : $tr.hide();
                    });
                }
            });
        });
    };
})(jQuery);