/**
* @Author: jctr<jctr88@gmail.com>
* http://blog.jctr.cn
*/


/**
 * 对表格加样式表(使表格各行颜色交替变色)
 *
 * @name     cycle
 * @param    c1 偶数行显示的颜色 0,2,4,6...
 * @param    c2 奇数行显示的颜色 1,3,5,7...
 * @param    cm 鼠标移动上去时的颜色
 * @param    fn 点击某个TD时的回调函数
 * @example  $(function(){$(".tableLine").cycle("csstr1","csstr2","csstr3",function(obj){var ck = jQuery("td",obj).get(5).firstChild; ck.checked = !ck.checked;})});
 */
jQuery.fn.cycle = function(c1, c2, cm, fn){
	if(!(c1 && c2)) return; //判断有没得参数传入
	var css, oj;
	this.each(function(){ //选中了多少个表格
		oj = jQuery("tr",jQuery(this));
		oj.each(function(i){ // 选中表格中的 TR 加 CSS
			css = i % 2 == 0 ? c1 : c2;
			jQuery(this).addClass(css);
		});
		
		if(cm){ //如果有第三个参数
			oj.hover(function(){
				jQuery(this).addClass(cm);
			},
			function(){
				jQuery(this).removeClass(cm);
			});
		}
		
		if(fn && "function" == typeof fn){ //用来处理点击事件的回调函数
			oj.click(function(event){
				event = jQuery.event.fix( event || window.event || {} ); 
				var cn = event.target;
				if( cn && cn.nodeName.toLowerCase() == "td"){
					cn = jQuery(cn).parent();
					fn.call(this,cn); //返回点击的那一行的HTML
				}
			});
		}
	});
} 
