﻿/*
* jQuery table search Plugin 1.0.1
*widget that adds search functionality in a table element
* Copyright (c) 2008 Leonardo Rossetti (motw.leo@gmail.com)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
(function($) {
    $.fn.tableSearch = function(options) {
        //default settings
        var settings = $.extend({
            ignore: [],
            selector: true, //will be implemented in the next release
            turbo: false, //will be implemented in the next release
            field: { className: "table-search-field", name: "table-search-field", size: "20", required: "true" },
            label: { selector: "Select a column: ", field: "Insert a text to search: " },
            button: { className: "table-search-button", name: "table-search-button", value: "Search" },
            empty: "Please fill the search field.",
            notFound: "Data not found.",
            container: "table-search-container"
        }, options);
        //search function
        var search = function(obj) {
            var found = false;
            var criteria = $(".table-search-selector").val();
            var field = $("." + settings.field.className).val().toLowerCase();
            var counter = 0;
            //checks if field is empty
            if ($.trim(field) === "") {
                $("#" + settings.container).find("span.table-search-error-message").html(settings.empty).show();
                return;
            }
            //sets reference column
            $(obj)
				.find("thead tr th").each(function() {
				    if ($(this).text() === criteria) {
				        return false;
				    }
				    counter++;
				}).end()
            //searches for text
				.find("tbody tr").each(function() {
				    var columnText = $(this).find("td").eq(counter).text().toLowerCase();
				    var result = columnText.indexOf(field) != -1;
				    if (result) {
				        found = true;
				        $(this).show();
				        $("#" + settings.container).find("span.table-search-error-message").empty().hide();
				    } else if (!result && found) {
				        $(this).hide();
				    }
				});
            //if no results were found
            if (!found) {
                $("#" + settings.container).find("span.table-search-error-message").html(settings.notFound).show();
            }
        };
        //reset search and show all information
        var reset = function(obj) {
            $(obj).find("tbody tr").show();
        };
        //checks ignore list
        var isIgnored = function(text) {
            var ignore = false;

            if ($(settings.ignore).size() === 0) {
                return false;
            }

            $(settings.ignore).each(function() {
                if (($(this).get() + "").toLowerCase() === text.toLowerCase()) {
                    ignore = true;
                    return false;
                }
            });

            return ignore;
        };
        //for each table element
        return this.each(function() {
            var $this = this;
            //inserts container for search fields
            $("<div></div>")
				.attr("id", "table-search-container")
				.insertBefore($this);
            //insert select field with th values
            if (settings.selector) {
                $("<select></select>")
					.attr({ className: "table-search-selector", name: "table-search-selector" })
					.each(function() {
					    var select = this;
					    //inserts select
					    $($this).find("thead tr th").each(function() {
					        if (!isIgnored($(this).text())) {
					            $(select).append("<option value='" + $(this).text() + "'>" + $(this).text() + "</option>");
					        }
					    });
					})
					.appendTo("#" + settings.container);

                $("<label></label>")
					.attr("for", "table-search-selector")
					.html(settings.label.selector)
					.insertBefore(".table-search-selector");
            }
            //inserts input text field
            $("<input />")
				.attr(settings.field)
				.each(function() {
				    this.type = "text";
				})
				.bind("keypress", function(e) {
				    if (e.which === 13) {
				        search($this);
				    } else if (e.which === ($.browser.mozilla ? 0 : 27)) {
				        reset($this);
				    }
				})
				.appendTo("#" + settings.container);
            $("<label></label>")
				.attr("for", settings.field.name)
				.css("margin-left", "5px")
				.html(settings.label.field)
				.insertBefore("." + settings.field.className);
            //inserts button
            $("<input type='button'/>")//for ie problems
				.css("margin-left", "5px")
				.each(function() {
				    this.value = settings.button.value;
				})
				.attr(settings.button)
				.bind("click", function() {
				    search($this);
				})
				.appendTo("#" + settings.container);
            $("<br />").appendTo("#" + settings.container);
            $("<span></span>")
				.addClass("table-search-error-message")
				.css("display", "none")
				.appendTo("#" + settings.container);
        });
    };
})(jQuery);

