if(jQuery) (function($){
	$.extend($.fn, {
			 
		multiSelect: function(options, callback) {
			var defaults = {
				selectAll: true,
				selectAllText: 'Select All',
				noneSelected: 'Select options',
				allSelected: 'All select',
				oneOrMoreSelected: '% selected',
				inputClass: 'multiSelect',
				listClass: 'multiSelectOptions',
				listWidth: '',
				listTop: 34,
				listLeft: 4,
				listDirection: 'down'
			};
			var options = $.extend(defaults, options);
			var hasfocus = 0;
			
			// Initialize each multiSelect
			$(this).each( function() {
				var select = $(this);
				var html = '<input type="text" readonly="readonly" class="' + options.inputClass + '" value="" style="cursor: default;" />';
				html += '<div class="' + options.listClass + '" style="position: absolute; z-index: 301; display: none;">';
				if (options.selectAll) html += '<label class="selectAll"><span>' + options.selectAllText + '</span><input type="checkbox" class="selectAll" /></label>';
				$(select).find('option').each( function() {
					if ($(this).val() != '') {
						html += '<label><span>' + $(this).html() + '</span><input type="checkbox" name="' + $(select).attr('name') + '" value="' + $(this).val() + '"';
						if ($(this).attr('selected')) html += ' checked="checked"';
						html += ' /></label>';
					}
				});
				html += '</div>';
				$(select).after(html);
				
				// Events
				$(select).next('.' + options.inputClass).mouseover( function() {
					$(this).addClass('hover');
				}).mouseout( function() {
					$(this).removeClass('hover');
				}).click( function() {
					// Show/hide on click
					if( $(this).hasClass('active') ) {
						$(this).multiSelectOptionsHide();
					} else {
						$(this).multiSelectOptionsShow(options);
					}
					return false;
				}).focus( function() {
					// So it can be styled with CSS
					$(this).addClass('focus');
				}).blur( function() {
					$(this).removeClass('focus');
					if ($(this).hasClass('active') && hasfocus > 0 ) {
						//alert('container visible and has focus')
					} else {
						$(this).multiSelectOptionsHide();
					}
				});
				
				// Determine if Select All should be checked initially
				if (options.selectAll) {
					var sa = true;
					$(select).next('.' + options.inputClass).next('.' + options.listClass).find('input:checkbox').not('.selectAll').each( function() {
						if (!$(this).attr('checked')) sa = false;
					});
					if (sa) $(select).next('.' + options.inputClass).next('.' + options.listClass).find('input.selectAll').attr('checked', true).parent().addClass('checked');
				}
				
				// Handle Select All
				$(select).next('.' + options.inputClass).next('.' + options.listClass).find('input.selectAll').click( function() {
					if ($(this).attr('checked') == true) $(this).parent().parent().find('input:checkbox').attr('checked', true).parent().addClass('checked'); else $(this).parent().parent().find('input:checkbox').attr('checked', false).parent().removeClass('checked');
				});
				
				// Handle checkboxes
				$(select).next('.' + options.inputClass).next('.' + options.listClass).find('input:checkbox').click( function() {
					$(this).parent().parent().multiSelectUpdateSelected(options);
					$(this).parent().parent().find('label').removeClass('checked').find('input:checked').parent().addClass('checked');
					$(this).parent().parent().prev('.' + options.inputClass).focus();
					if (!$(this).attr('checked')) $(this).parent().parent().find('input:checkbox.selectAll').attr('checked', false).parent().removeClass('checked');
					if (callback) callback($(this).val());
				});
				
				// Initial display
				$(select).next('.' + options.inputClass).next('.' + options.listClass).each( function() {
					$(this).multiSelectUpdateSelected(options);
					$(this).find('input:checked').parent().addClass('checked');
				});
				
				// Handle hovers
				$(select).next('.' + options.inputClass).next('.' + options.listClass).find('label').mouseover( function() {
					hasfocus = 1;
					$(this).parent().find('label').removeClass('hover');
					$(this).addClass('hover');
				}).mouseout( function() {
					hasfocus = -1;
					$(this).parent().find('label').removeClass('hover');
				});
				
				// Keyboard
				$(select).next('.' + options.inputClass).keydown( function(e) {
					// Is dropdown visible?
					if( $(this).next('.' + options.listClass).is(':visible') ) {
						// Dropdown is visible
						// Tab
						if( e.keyCode == 9 ) {
							$(this).addClass('focus').trigger('click'); // esc, left, right - hide
							$(this).focus().next(':input').focus();
							return true;
						}
						
						// ESC, Left, Right
						if( e.keyCode == 27 || e.keyCode == 37 || e.keyCode == 39 ) {
							// Hide dropdown
							$(this).addClass('focus').trigger('click');
						}
						// Down
						if( e.keyCode == 40 ) {
							if( !$(this).next('.' + options.listClass).find('label').hasClass('hover') ) {
								// Default to first item
								$(this).next('.' + options.listClass).find('label:first').addClass('hover');
							} else {
								// Move down, cycle to top if on bottom
								$(this).next('.' + options.listClass).find('label.hover').removeClass('hover').next('label').addClass('hover');
								if( !$(this).next('.' + options.listClass).find('label').hasClass('hover') ) {
									$(this).next('.' + options.listClass).find('label:first').addClass('hover');
								}
							}
							return false;
						}
						// Up
						if( e.keyCode == 38 ) {
							if( !$(this).next('.' + options.listClass).find('label').hasClass('hover') ) {
								// Default to first item
								$(this).next('.' + options.listClass).find('label:first').addClass('hover');
							} else {
								// Move up, cycle to bottom if on top
								$(this).next('.' + options.listClass).find('label.hover').removeClass('hover').prev('label').addClass('hover');
								if( !$(this).next('.' + options.listClass).find('label').hasClass('hover') ) {
									$(this).next('.' + options.listClass).find('label:last').addClass('hover');
								}
							}
							return false;
						}
						// Enter, Space
						if( e.keyCode == 13 || e.keyCode == 32 ) {
							// Select All
							if( $(this).next('.' + options.listClass).find('label.hover input:checkbox').hasClass('selectAll') ) {
								if( $(this).next('.' + options.listClass).find('label.hover input:checkbox').attr('checked') ) {
									// Uncheck all
									$(this).next('.' + options.listClass).find('input:checkbox').attr('checked', false).parent().removeClass('checked');
								} else {
									// Check all
									$(this).next('.' + options.listClass).find('input:checkbox').attr('checked', true).parent().addClass('checked');
								}
								$(this).next('.' + options.listClass).multiSelectUpdateSelected(options);
								if( callback ) callback($(this));
								return false;
							}
							// Other checkboxes
							if( $(this).next('.' + options.listClass).find('label.hover input:checkbox').attr('checked') ) {
								// Uncheck
								$(this).next('.' + options.listClass).find('label.hover input:checkbox').attr('checked', false);
								$(this).next('.' + options.listClass).multiSelectUpdateSelected(options);
								$(this).next('.' + options.listClass).find('label').removeClass('checked').find('input:checked').parent().addClass('checked');
								// Select all status can't be checked at this point
								$(this).next('.' + options.listClass).find('input:checkbox.selectAll').attr('checked', false).parent().removeClass('checked');
								if( callback ) callback($(this));
							} else {
								// Check
								$(this).next('.' + options.listClass).find('label.hover input:checkbox').attr('checked', true);
								$(this).next('.' + options.listClass).multiSelectUpdateSelected(options);
								$(this).next('.' + options.listClass).find('label').removeClass('checked').find('input:checked').parent().addClass('checked');
								if( callback ) callback($(this));
							}
						}
						return false;
					} else {
						// Dropdown is not visible
						if( e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 13 || e.keyCode == 32 ) { // down, enter, space - show
							// Show dropdown
							$(this).removeClass('focus').trigger('click');
							$(this).next('.' + options.listClass).find('label:first').addClass('hover');
							return false;
						}
						//  Tab key
						if( e.keyCode == 9 ) {
							// Shift focus to next INPUT element on page
							$(this).focus().next(':input').focus();
							return true;
						}
					}
					// Prevent enter key from submitting form
					if( e.keyCode == 13 ) return false;
				});
				
				// Eliminate the original form element
				$(select).remove();
			});
			
		},
		
		// Hide the dropdown
		multiSelectOptionsHide: function() {
			$(this).hasfocus = 0;
			$(this).removeClass('active').next('div').hide();
		},
		
		// Show the dropdown
		multiSelectOptionsShow: function(options) {
			//alert('show');
			// Hide any open option boxes
			$('.' + options.inputClass).multiSelectOptionsHide();
			$(this).next('.' + options.listClass).find('label').removeClass('hover');
			$(this).addClass('active').next('.' + options.listClass).show();
			
			// Set the width of the option list
			if (options.listWidth != '') {
				$(this).next('.' + options.listClass).css({ width: options.listWidth + 'px' });
			}
			
			// Position it
			if (options.listDirection == 'up') {
				var offset = $(this).next('.' + options.listClass).outerHeight();
				$(this).next('.' + options.listClass).css({ top: -(offset) + 'px' });
			} else {
				$(this).next('.' + options.listClass).css({ top: options.listTop + 'px' });
			}
			$(this).next('.' + options.listClass).css({ left: options.listLeft + 'px' });
			
			// Disappear on hover out
			multiSelectCurrent = $(this);
			var timer = '';
			$(this).next('.' + options.listClass).hover( function() {
				clearTimeout(timer);
			}, function() {
				timer = setTimeout('$(multiSelectCurrent).multiSelectOptionsHide(); $(multiSelectCurrent).unbind("hover");', 250);
			});
			
		},
		
		// Update the textbox with the total number of selected items
		multiSelectUpdateSelected: function(options) {
			var i = 0, s = '';
			$(this).find('input:checkbox:checked').not('.selectAll').each( function() {
				i++;
			})
			//alert(i);
			if (i == 0) {
				$(this).prev('input.' + options.inputClass).val(options.noneSelected);
			} else {
				if (options.selectAll && $(this).find('label.selectAll input:checkbox').attr('checked')) {
					$(this).prev('input.' + options.inputClass).val(options.allSelected);
				} else {
					if (options.oneOrMoreSelected == '*') {
						var display = '';
						$(this).find('input:checkbox:checked').each( function() {
							if ($(this).parent().text() != options.selectAllText) display = display + $(this).parent().text() + ', ';
						});
						display = display.substr(0, display.length - 2);
						$(this).prev('input.' + options.inputClass).val(display);
					} else {
						$(this).prev('input.' + options.inputClass).val(options.oneOrMoreSelected.replace('%', i));
					}
				}
			}
		}
		
	});
	
})(jQuery);