/*********************
//* jQuery MTC (multiple select to checkboxes) - created by mck: http://www.daesso.com/ | http://www.bomb.pl/
*********************/

(function($) {
	$.fn.MTC = function(options) {
		
		var defaults = {
			limit: 0
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function()
		{
			var count = 0;
			
			var select = jQuery(this);
			var select_name = select.attr('name');
			var new_name = select_name.replace('[]', '');
			var max_counts = 0;
			
			var optgroups = select.children('optgroup');
			
			if(optgroups.length > 0)
			{
				select.html('');
				select.removeAttr('multiple');
				select.attr('size', 1);
				select.attr('name', new_name + '_select');
				
				jQuery('<div/>').attr('id', new_name + '_options_selected').attr('class', 'form_element indent').insertAfter(select);
				var selected = jQuery('#' + new_name + '_options_selected');
				
				jQuery.each(optgroups, function(i)
				{
					jQuery('<option/>').html( jQuery(this).attr('label') ).attr('value', i).appendTo(select);
					
					var group_options = jQuery(this).children('option');
					
					if(group_options.length > 0)
					{
						jQuery('<div/>').attr('id', new_name + '_options_'+i).attr('class', 'form_element indent form_select_options').insertAfter(selected);
						var container = jQuery('#' + new_name + '_options_'+i);
						var counts = 0;
						
						if(i > 0)
						{
							container.hide();
						}
						
						jQuery.each(group_options, function(j)
						{
							var name = jQuery(this).text();
							var value = jQuery(this).attr('value');
							var checked = jQuery(this).is(':selected');
							
							jQuery('<p>').addClass('form_option').html('<input type="checkbox" name="'+select_name+'" value="'+value+'" id="'+new_name+'_value_'+value+'" '+(checked ? 'checked="checked" ' : '')+'/><label for="'+new_name+'_value_'+value+'">'+name+'</label>').appendTo(container);
							jQuery('#'+new_name+'_value_'+value).change(function() {
								if(jQuery(this).is(':checked'))
								{
									count++;
									jQuery('<span/>').attr('class', 'form_selected_option').html(name).attr('id', new_name+'_value_'+value+'_label').bind('click',function() { jQuery('input#'+new_name+'_value_'+value).attr('checked', false).trigger('change'); }).appendTo(selected);
								}
								else
								{
									count--;
									jQuery('#'+new_name+'_value_'+value+'_label').remove();
								}
								
								if(options.limit > 0)
								{
									if(count >= options.limit)
									{
										select.siblings('div.form_select_options').find('input:checkbox').not(':checked').attr('disabled', true);
									}
									else
									{
										select.siblings('div.form_select_options').find('input:checkbox:disabled').attr('disabled', false);
									}
								}
							});
							
							if(checked)
							{
								count++;
								counts++;
								jQuery('<span/>').attr('class', 'form_selected_option').html(name).attr('id', new_name+'_value_'+value+'_label').bind('click',function() { jQuery('input#'+new_name+'_value_'+value).attr('checked', false).trigger('change'); }).appendTo(selected);
							}
						});
						
						if(counts > max_counts)
						{
							max_counts = counts;
							select.val(i);
							container.siblings('div.form_select_options').hide();
							container.show();
						}
					}
				});
				
				select.change(function() {
					var n = jQuery(this).children('option:selected').val();
					var container = jQuery('#' + new_name + '_options_'+n);
					
					container.siblings('div.form_select_options').hide();
					container.show();
				});
			}
		});
	}
})(jQuery);
