jQuery.fn.extend({
	comboList: function()
				{
					this.combo = jQuery(this);
					this.triggers = null;
					this.targets = null;
					this.selection = null;

					this.init = function()
								{
									this.getTriggers();
									this.getTargets();
									this.initSelection();
									this.setTriggerEvents();

									this.formatCombo();
								}


					this.getTriggers = function()
										{
											this.triggers = this.combo.find('a');
										}

					this.getTargets = function()
										{
											var ts = new Array();
											this.triggers
												.each(function()
														{
															ts.push('#' + jQuery(this).attr('rel'));
														});
											this.targets = jQuery(ts.join(', '));
										}

					this.initSelection = function()
											{
												var defaultSelection = this.combo.find('a.selected').parent();
												if (defaultSelection.size() == 0)
													defaultSelection = this.combo.find('a:first').parent();
												else
													defaultSelection.find('a:first').removeClass('selected');

												this.combo.css({
																	height: defaultSelection.height() + 'px',
																	overflow: 'hidden'
																});

												this.selection = defaultSelection.clone();
												this.selection.find('a:first').addClass('selected')
												this.selection.prependTo(this.combo);
												this.setSelectionEvents();

												this.targets.css('display', 'none');
												jQuery('#' + this.selection.find('a:first').attr('rel')).css('display', '');
											}

					this.updateSelection = function(newSelection)
											{
												var currentSelection = this.selection;
												currentSelection.find('a:first').replaceWith(newSelection.find('a:first').clone());
												currentSelection.find('a:first').addClass('selected');
												this.combo.removeClass('expanded');
												this.combo.css({
																	height: currentSelection.height() + 'px',
																	overflow: 'hidden'
																});
												this.selection = currentSelection;
												this.setSelectionEvents();
											}

					this.setSelectionEvents = function()
												{
													this.selection
														.find('a')
														.click(function()
																{
																	var selectionLink = jQuery(this);
																	if (c.hasClass('expanded'))
																	{
																		c.removeClass('expanded')
																			 .css('height', selectionLink.parent().height() + 'px');
																	}
																	else
																	{
																		c.addClass('expanded')
																			 .css('height', 'auto');
																	}
																	return false;
																})
												}

					this.setTriggerEvents = function()
											{
												this.triggers
													.click(function()
															{
																var comboLink = jQuery(this);
																c.find('a[class!=selected]')
																	.each(function()
																			{
																				var lnk = jQuery(this);
																				jQuery('#' + lnk.attr('rel')).css('display', 'none');
																			})
																jQuery('#' + comboLink.attr('rel')).css('display', '');
																c.updateSelection(comboLink.parent());

																return false;
															})
											}

					this.formatCombo = function()
										{
											this.combo
												.css({
														   height: '22px',
														   position: 'absolute',
														   left: '0px',
														   top: '0px'
													   });
										}

					this.init();
					var c = this;
				}
})

jQuery(document).ready(function(){
	jQuery('h3').css('display', 'none');
	jQuery('#controller').comboList();
})