/* 
jquery plugin list filter

*/

// create closure
(function($) {
	// plugin definition
	jQuery.fn.mcomListFilter = function(options) {
	  // standard settings
	  settings = jQuery.extend({
		animated: true, //show/hide animated,
		selectMatch: true
	  }, options);
   
	  return this.each(function(){
		jQuery(this).keyup(function () {
			var filterWord = jQuery(this).val();
			var filterUl = jQuery(this).nextAll('ul');
			filterUl.children().each(function () {
				if (jQuery(this).text().search(new RegExp(filterWord, "i")) < 0) {
					if (settings.animated) {
						jQuery(this).slideUp();
					}
					else {
						jQuery(this).css('display','none');
					}
				} 
				else {
					if (settings.animated) {
						jQuery(this).slideDown();
					}
					else {
						jQuery(this).css('display','block');
					}
				}
			});
			
			if (settings.selectMatch && filterUl.children(':visible').length == 1) {
				filterUl.children(':visible').children().eq(0).attr('checked',true);
			}
			else if ( settings.selectMatch && filterUl.children(':visible').length != 1 ) {
				filterUl.children().each(function() {
					jQuery(this).children().eq(0).attr('checked',false);
				});
			}
			
			return false;
		});
	  }); 
	}
// end of closure
})(jQuery);
