(function($) {

	var itemDivClassFor = function(item) {
		return item.hasResults ? 'withResults' : 'noResults';
	};

	var open = false;

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

	$.fn.athletesSelector = function(items, options) {
		settings = $.extend({ chooserBuilder: 'list' }, options);
		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 ($.browser.msie) { 
							selectionsDiv.hide();
						} else { 
							selectionsDiv.slideUp('fast');
						}
						if (shouldRound()) { selectedDiv.corner('bottom 5px'); }
						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, selectionsDiv, selectedDiv, parentContainer) {
		var itemDiv = $('<div class="' + itemDivClassFor(item) + '">' + item.label + '</div>').click(function () {
			var appPath = window.location.pathname.split('/')[1];
			selectionsDiv.slideUp('fast');
			window.location.pathname = '/' + appPath + '/' + item.path;
		})
		.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, selectionsDiv, selectedDiv, selectionsDiv);
			});
		},
		year: function(items, selectionsDiv, selectedDiv) {
			var table = $('<table></table>');
			var decade;
			var row;
			var yearInDecade = 0;
			$.each(items, function() {
				var item = this;
				var year = item.label;
				if (decade != Math.floor(year / 10)) {
					if (yearInDecade > 0) {
						for(i = yearInDecade; i >= 0; --i) {
							row.append('<td></td>');
						}
					}
					decade = Math.floor(year / 10);
					row = $('<tr></tr>');
					table.append(row);
					yearInDecade = 0;
				}
				while (year % 10 < yearInDecade) {
					row.append('<td></td>');
					--yearInDecade;
				}
				var cell = $('<td></td>');
				chooserItemBuilder(item, selectionsDiv, selectedDiv, cell);			
				row.append(cell);
				yearInDecade--;
			});
			selectionsDiv.append(table);
		}
	}
})(jQuery);
