/*
* jQuery checktable plugin
*
* Copyright (c) qmth 
*/
(function($) {

    $.fn.checktable = function(options) {

        var opts = $.extend({}, $.fn.checktable.defaults, options);

        return this.each(function() {
        	var $table = $(this);
        	
        	$table.find('.row').click(function(e){
        		var $row = $(this);
    			var index = $row.index()-1;
    			var checkbox = $row.find('input[type=checkbox]');
    			var isChecked = checkbox.attr('checked');
    			if(isChecked == "checked"){
    				$row.removeClass("active");
    				checkbox.removeAttr('checked');
    			}
    			else{
    				cancelAll($table);
    				$row.addClass("active");
    				checkbox.attr('checked','checked');
    			}
    			
    			var checkedRows = $('.list-table .row.active');
    			if(options)options.onCheck(checkedRows);
        	});
        	
        	$table.find('.check').click(function(e){
        		e.stopPropagation();
    			var target = $(this);
    			var row = target.parent();
    			var index = row.index()-1;
    			var checkbox = row.find('input[type=checkbox]');
    			var isChecked = checkbox.attr('checked');
    			
    			if(isChecked == "checked"){
    				row.removeClass("active");
    				checkbox.removeAttr('checked');
    			}
    			else{
    				row.addClass("active");
    				checkbox.attr('checked','checked');
    			}
    			var checkedRows = $('.list-table .row.active');
    			if(options)options.onCheck(checkedRows);
        	});
        	
        
        });
    };

    function cancelAll(table) {

    	table.find('.active').removeClass("active");
		table.find('input[type=checkbox]').removeAttr('checked');
    }

    
    $.fn.checktable.defaults = {
        onCheck:function(row){}
    };

})(jQuery);





