(function($){
/*
 * @author denisdeng 
 * emmail dexibe@gmail.com || denisdeng@jobkoo.com
 * $Version: 2010.02.10. 1.0
 *  $.fn.tableFilter plugin
 *  @requires: jquery-1.3.2.min.js
 *  If an options Object is provided, the following attributes are supported:
 *
 *  lang[String]:The language of web application.
 *  			 default:'cn'
 *  filterClass: The className of filter box.
 				 default:'filters'
*/		  		  
$.fn.tableFilter = function(options){
	var settings = {
					lang:'cn',
					filterClass:'filters',
					isPageBar:true
					
	}
	return this.each(function(){
		if(options) settings = $.extend(settings, options);
		var $table = $(this);
		if($table.attr("tagName").toLowerCase() == "table"){		
			var keyWords = [];
			$table.alternateRowColors();
			$table.find('th').each(function (column) {
				if ($(this).is('.filter')) {
					var trigger = $($(this).html());
					var $filters = $('<div class="filters"></div>').addClass(settings.filterClass);
					var keyList = $("<ul></ul>").hide();
					var h = $(this).outerHeight();
					var pos = $(this).position();
					var rows = $table.find('tbody tr');
					var all = $("<li><a>" + (settings.lang == 'cn' ? "全部" : "All") + "</a></li>");
					keyList.append(all);
					all.bind("click",function(){
						rows.show();
						keyList.isOpen = false;
						keyList.slideUp(100);
						$table.alternateRowColors().tablePaginate();
					});
					rows.find('td').filter(':nth-child(' + (column + 1) + ')').each(function() {
						if($.inArray($(this).text(),keyWords) < 0){
							keyWords.push($(this).text());
						}
					}); 
					$.each(keyWords,function(index, keyword){
							var li = $("<li><a>"+ keyword +"</a></li>");
							li.bind("click",{'keyword': keyword},function(event){
								$("li",keyList).removeClass("current");
								rows.removeClass("isSort");
								$(this).addClass("current");
								rows.each(function() {
									if ($('td', this).filter(':nth-child(' + (column + 1) +')').text() == event.data['keyword']) {
										$(this).addClass("isSort").show();
									}else if ($('th',this).length == 0){
										$(this).hide();
									}
								});
								keyList.isOpen = false;
								keyList.slideUp(100);
								//刷新页码标签和斑马线效果
								if(settings.isPageBar){
									$table.alternateRowColors().tablePaginate({
																				pageRange:"isSort"
																			  });
								}
							})
							keyList.append(li);
					})
					$filters.append(trigger);
					$filters.append(keyList);
					$(this).empty().append($filters);
					trigger.bind('click',function(e){
						if(!keyList.isOpen){
							keyList.css({'position':'absolute','z-index':2000,'left':pos.left,'top':pos.top + h}).slideDown(100);
							keyList.isOpen = true;
						}else{
							keyList.isOpen = false;
							keyList.slideUp(100);
						}
						e.preventDefault();
					})
				}
			});
		}else{
			alert("当前对象不是表格对象");	
		}
	})
}
/*
 * @author denisdeng 
 * emmail dexibe@gmail.com || denisdeng@jobkoo.com
 * $Version: 2010.02.10. 1.0
 *  $.fn.alternateRowColors plugin
 *  @requires: jquery-1.3.2.min.js
*/
$.fn.alternateRowColors = function() {
	return this.each(function(){
		var rows = $('tbody tr', this).filter(function(){
			return $(this).css('display') !== 'none';
		}).removeClass("even").removeClass("odd").get();
		$.each(rows,function(index,row){
			index%2 == 0 ? $(row).addClass("even") : $(row).addClass("odd");
		})
	})
};

})(jQuery)