(function($) {

	var open = false;
	var firstDraw = true;

	var shouldRound = function() {
		return !($.browser.msie || $.browser.opera);
	}

	var idForEvent = function(text) {
		return text.replace(/\W/g, '');
	}

	var showProgressionDiv = function(item) {
		var progressionDiv = $("#" + item.id);
		progressionDiv.show();
		var drewProgressionPlot = false;
		if (item.progressionPlotter) {
			var plotDiv = $('<div class="plot"></div>');
			progressionDiv.append(plotDiv);
			item.progressionPlotter(plotDiv);
			item.progressionPlotter = null;
			drewProgressionPlot = true;
		}
		var progression = {
			td: progressionDiv.closest("td")
		};
		var honours = {
			td: progression.td.parent().next("tr").children("td") 
		};
		var worldRankings = {
			td: progression.td.prev("td")
		};
		var panels = [ progression, honours, worldRankings ];
		var contentHeight = function(jq) {
			var h = 0;
			jq.children(":visible").each(function () {
				h += $(this).outerHeight(true);
			});
			if (firstDraw && drewProgressionPlot & jq == progression.corner) {
				h += 22;
				firstDraw = false;
			}
			return h;
		};
		$.each(panels, function() {
			this.corner = this.td.children("div.corner");
			this.contentHeight = contentHeight(this.corner);
		});
		$.each(panels, function() {
			this.corner.height(this.contentHeight);
		});
		$.each(panels, function() {
			this.tdHeight = this.td.height();
		});
		$.each(panels, function() {
			this.corner.height(this.tdHeight - 20);
		});
	}

	$.fn.athletesSelector = function(items) {
		settings = $.extend({ chooserBuilder: 'list' });
		var selectedEventId = idForEvent(unescape(window.location.hash.substring(1)));
		var hasSelected = false;
		$.each(items, function() {
			var item = this; 
			item.selected = (item.id == selectedEventId);
			if (item.selected) {
				hasSelected = true;
			}
		});
		if (!hasSelected) {
			items[0].selected = true;
		}
		$.each(items, function() {
			var item = this;
	 		if (item.selected) {
				showProgressionDiv(item);	
			}
		})
		return $(this).each(function() {
			var div = $(this);
			var selectionsDiv = $('<div class="selections"></div>');
			var selectedDiv = $('<div class="selected"></div>')
				.click(function () {
					if (open == selectionsDiv) {
						if (shouldRound()) { selectedDiv.corner('bottom 5px'); }
						if ($.browser.msie) { 
							selectionsDiv.hide();
						} else { 
							selectionsDiv.slideUp('fast');
						}
						open = false;	
					} else {
						if (open) { 
							if ($.browser.msie) { 
								open.hide();
							} else {
								open.slideUp('fast');
							}
						}
						if (shouldRound()) { selectedDiv.corner('bottom 1px'); }
						selectionsDiv.slideDown('fast');
						open = selectionsDiv;
					}
				})
				.hover(
					function() { $(this).css('background-color', '#ddd') },
					function() { $(this).css('background-color', 'white') });
			($.fn.athletesSelector.builders[settings.chooserBuilder])(items, selectionsDiv, selectedDiv);
			selectionsDiv.hide();
			div.append(selectedDiv);
			selectionsDiv.width($.browser.msie ? selectedDiv.outerWidth() : selectedDiv.width());
			div.append(selectionsDiv);
			//selectedDiv.css('border', '1px solid #ddd');
			if (shouldRound()) {
				selectedDiv.corner('5px keep');
				selectionsDiv.corner('bottom 5px');
			}
			selectionsDiv.height(Math.min(200, $.browser.msie ? selectionsDiv.outerHeight() : selectionsDiv.height()));
		});
	};
	
	var chooserItemBuilder = function(item, items, selectionsDiv, selectedDiv, parentContainer) {
		var itemDiv = $('<div class="withResults">' + item.label + '</div>').click(function () {
			selectionsDiv.slideUp('fast');
			selectedDiv.empty();
			selectedDiv.append(
				itemDiv.clone().css('background-color', 'transparent'));
			if (shouldRound()) { selectedDiv.corner('bottom 5px'); }
			if ($.browser.msie) { 
				selectionsDiv.hide();
			} else { 
				selectionsDiv.slideUp('fast');
			}
			open = false;
			$.each(items, function() { $("#" + this.id).hide() });
			showProgressionDiv(item);
		})
		.hover(
			function() { $(this).css('background-color', '#ddd') },
			function() { $(this).css('background-color', 'white') });
		parentContainer.append(itemDiv);
		if (item.selected) {
			selectedDiv.append(itemDiv.clone());
		}
	}

	$.fn.athletesSelector.builders = {
		list: function(items, selectionsDiv, selectedDiv) {
			$.each(items, function() {
				chooserItemBuilder(this, items, selectionsDiv, selectedDiv, selectionsDiv);
			});
		}
	}
})(jQuery);

