(function($){
	$.fn.checkgroup = function(options){
		//merge settings
		settings=$.extend({
			groupSelector:null,
			groupName:'group_name',
			enabledOnly:false,
			onComplete:null,
			onChange:null
		},options || {});
		
		var ctrl_box=this;
		
		//allow a group selector override option
		var grp_slctr = (settings.groupSelector==null) ? 'input[name='+settings.groupName+']' : settings.groupSelector;
		
		//setup the callback functions
		var _onComplete = settings.onComplete;
		var _onChange = settings.onChange;
		
		//grab only enabled checkboxes if required
		if(settings.enabledOnly)
		{
			grp_slctr += ':enabled';
		}
		//attach click event to the "check all" checkbox(s)
		ctrl_box.click(function(e){
			var chk_val=(e.target.checked);
			//check the boxes and prepare for callback;
			var boxes=Array();
			var $i=0;
			$(grp_slctr).each(function(){
				if (this.checked!=chk_val) {
					boxes[$i] = this;
					this.checked=chk_val;
					if (typeof _onChange == "function") {
						_onChange(this);
					}
					$i++;
				}	
			});
			//if there are other "select all" boxes, sync them
			ctrl_box.attr('checked',chk_val);
			if(typeof _onComplete == "function")
			{ 
				_onComplete(boxes);
			}			
		});
		//attach click event to checkboxes in the "group"
		$(grp_slctr).click(function(){
			if(!this.checked)
			{
				ctrl_box.attr('checked',false);
			}
			else
			{
				//if # of chkbxes is equal to # of chkbxes that are checked
				if($(grp_slctr).size()==$(grp_slctr+':checked').size()){
					ctrl_box.attr('checked','checked');
				}
			}
		});

		//make this function chainable within jquery
		return this;
	};						
})(jQuery);