/*
* jQuery table search Plugin 1.0.0
*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({
			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: "50", required: "true"},
			label: {selector: "<div style='clear: both; font: normal 12px arial, sans-serif; color: #646464;'>Selecione uma Coluna para Pesquisar: </div>", field: "<br><br><div style='font: normal 12px arial, sans-serif; color: #646464;'>Texto para Pesquisa: </div>"},
   		button: {className: "table-search-button", name: "table-search-button", value: "Busca"},
			empty:     "<span style='clear: both; font: bold 12px arial, sans-serif; color: red;'>Campo de Busca está vazio!</span>",
			notFound:  "<span style='clear: both; font: bold 12px arial, sans-serif; color: red;'>Dados Não Encontrados!</span>",
			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();
			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();
					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();
		};
		// 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"})
               .css("height", "24px")
					.each(function () {
						var select = this;
						// inserts select
						$($this).find("thead tr th").each(function () {
						   $(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)
            .attr("onBlur", "javascript:this.value=this.value.toUpperCase();")
            .css("height", "18px")
				.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", "10px")
            .css("width", "80px")
            .css("height", "22px")
				.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);
