var framework = {
	id: "", 
	props: {
		curRace: 0, 
		nextRace: 0, 
		animSpeed: 500, 
		animating: false, 
		standingsVisible: false, 
		showingAllUpdates: false, 
		updatesMinHeight: 400, 
		touchDevice: false, 
		leaderboardGraphShowing: false, 
		standingsGraphShowing: false, 
		sorting: {
			curType: "place", 
			curDir: "asc"
		}
	}, 
	fn: {
		showStandings: 
			function () {
				if (!framework.props.standingsVisible) {
					$("#leaderboard").slideUp();
					$("#whats-happening").slideUp();
					$("#standings").slideDown();
					
					$("div.toplinks:first").removeClass("selected");
					$("div.toplinks:last").removeClass("selected").addClass("selected");
					
					framework.props.standingsVisible		= true;
				}
			}, 

		hideStandings: 
			function () {
				if (framework.props.standingsVisible) {
					$("#leaderboard").slideDown();
					$("#whats-happening").slideDown();
					$("#standings").slideUp();

					$("div.toplinks:last").removeClass("selected");
					$("div.toplinks:first").removeClass("selected").addClass("selected");
					
					framework.props.standingsVisible		= false;
				}
			}, 
			
		switchRace: 
			function (objEvent) {
				if (objEvent.currentTarget) {
					$("div.toplinks").removeClass("hover");
					
					if (framework.props.standingsVisible) {
						if (framework.props.standingsGraphShowing) {
							framework.props.standingsGraphShowing	= false;
							$("#standings-graph").hide();
							$("#standings").show();
						}
						framework.fn.hideStandings();
					}
					if (framework.props.leaderboardGraphShowing) {
						framework.props.leaderboardGraphShowing		= false;
						
						$("#leaderboard-graph").hide();
						$("#leaderboard-chart").show();
					}
					
					$(objEvent.currentTarget).addClass("selected").siblings("li").removeClass("selected");
					
					var strThisID		= $(objEvent.currentTarget).attr("id");
					
					if ((strThisID) && (strThisID.length) && (strThisID.indexOf("-") > -1)) {
						var intRaceNum		= parseInt(strThisID.substr(strThisID.indexOf("-") + 1));
						
						if ((!isNaN(intRaceNum)) && (intRaceNum != framework.props.curRace) && (!framework.props.animating)) {
							framework.props.animating		= true;
							framework.props.nextRace		= intRaceNum;
							
							var objSortSource				= $("#dataset-" + framework.props.nextRace + "-source li");
							
							switch (framework.props.sorting.curType) {
								case "place" : 
									// sort by place
									$(objSortSource).tinysort("ol>li.place", {order: framework.props.sorting.curDir});
									break;
									
								case "bow" : 
									// sort by bow #
									$(objSortSource).tinysort("ol>li.bow", {order: framework.props.sorting.curDir});
									break;
									
								case "sail" : 
									// sort by sail
									$(objSortSource).tinysort("ol>li.sail", {order: framework.props.sorting.curDir});
									break;
									
								case "name" : 
									// sort by vessel name
									$(objSortSource).tinysort("ol>li.name", {order: framework.props.sorting.curDir, attr: "title"});
									break;
									
								case "owner" : 
									// sort by owner name
									$(objSortSource).tinysort("ol>li.owner", {order: framework.props.sorting.curDir});
									break;
									
								case "time" : 
									// sort by owner time
									$(objSortSource).tinysort("ol>li.time", {order: framework.props.sorting.curDir});
									break;
							}
							
							$("#leaderboard h3 strong#race-" + framework.props.curRace)
								.animate(	{
												"opacity": 0
											}, 
											framework.props.animSpeed, 
											"easeOutExpo", 
											function() {
												$("#leaderboard h3 strong#race-" + framework.props.curRace)
													.removeClass("hidden")
													.addClass("hidden")
													.css("display", "none");
												$("#leaderboard h3 strong#race-" + framework.props.nextRace)
													.removeClass("hidden")
													.css(	{
																"display": "block", 
																"opacity": 0
															})
													.animate(	{
																	"opacity": 1
																}, 
																framework.props.animSpeed, 
																"easeOutExpo", 
																function() {
																	/*
																	$("#leaderboard h3 strong#race-" + framework.props.nextRace).find("a").each(function() { $(this).removeClass("selected"); });
																	$("#leaderboard h3 strong#race-" + framework.props.nextRace + " a:first")
																		.addClass("selected")
																		.trigger("click");
																	*/
																});
											});
							
							$("#dataset-" + framework.props.curRace + "-source")
								.parent("div.dataset")
								.animate(	{
												"opacity": 0
											}, 
											framework.props.animSpeed, 
											"easeOutExpo", 
											function() {
												$("#dataset-" + framework.props.curRace + "-source")
													.parent("div.dataset")
													.removeClass("hidden")
													.addClass("hidden")
													.css("display", "none");
												$("#dataset-" + framework.props.nextRace + "-source")
													.parent("div.dataset")
													.removeClass("hidden");
												$("#dataset-" + framework.props.nextRace + "-source")
													.parent("div.dataset")
													.removeClass("hidden")
													.css(	{
																"display": "block", 
																"opacity": 0
															})
													.animate(	{
																	"opacity": 1
																}, 
																framework.props.animSpeed, 
																"easeOutExpo", 
																function() { 
																	framework.props.animating	= false;
																	framework.props.curRace		= framework.props.nextRace;
																	
																	// update title
																	$("#race_title").html($("#races-menu li#race-" + framework.props.curRace + " strong").html());
																	
																	framework.fn.updateWHHeight();
																});
											});
						}
					}
				}
			}, 
			
		switchMark: 
			function (objEvent) {
				if (objEvent.currentTarget) {
					if (framework.props.standingsVisible) {
						if (framework.props.standingsGraphShowing) {
							framework.props.standingsGraphShowing	= false;
							$("#standings-graph").hide();
							$("#standings").show();
						}
						framework.fn.hideStandings();
					}
					if (framework.props.leaderboardGraphShowing) {
						framework.props.leaderboardGraphShowing		= false;
						
						$("#leaderboard-graph").hide();
						$("#leaderboard-chart").show();
					}
					
					var strRelID		= $(objEvent.currentTarget).attr("rel");
					
					if (strRelID.length) {
						var intMarkNum		= parseInt(strRelID);
						
						if (!isNaN(intMarkNum)) {
							// set selected mark
							$("#leaderboard h3 strong#race-" + framework.props.curRace + " a").removeClass("selected");
							$(objEvent.currentTarget).addClass("selected");
							
							var objSortSource			= $("#dataset-" + framework.props.curRace + "-mark" + intMarkNum + " li");
							
							switch (framework.props.sorting.curType) {
								case "place" : 
									// sort by place
									$(objSortSource).tinysort("ol>li.place", {order: framework.props.sorting.curDir});
									break;
									
								case "bow" : 
									// sort by bow #
									$(objSortSource).tinysort("ol>li.bow", {order: framework.props.sorting.curDir});
									break;
									
								case "sail" : 
									// sort by sail
									$(objSortSource).tinysort("ol>li.sail", {order: framework.props.sorting.curDir});
									break;
									
								case "name" : 
									// sort by vessel name
									$(objSortSource).tinysort("ol>li.name", {order: framework.props.sorting.curDir, attr: "title"});
									break;
									
								case "owner" : 
									// sort by owner name
									$(objSortSource).tinysort("ol>li.owner", {order: framework.props.sorting.curDir});
									break;
									
								case "time" : 
									// sort by owner time
									$(objSortSource).tinysort("ol>li.time", {time: framework.props.sorting.curDir});
									break;
							}
							
							// sort list
							$("#dataset-" + framework.props.curRace + "-source").quicksand($("#dataset-" + framework.props.curRace + "-mark" + intMarkNum + ">li"), {useScaling: true, easing: "easeOutElastic", duration: 1500});
							
							// update height of what's happening
							framework.fn.updateWHHeight();
						}
					}
				}
			}, 
			
		sortResults: 
			function (objEvent) {
				if (objEvent.currentTarget) {
					var strRelID		= $(objEvent.currentTarget).attr("rel");
					
					if (strRelID.length) {
						var strSortType		= strRelID.toLowerCase();
						
						// set selected
						$("#chart-header li a").removeClass("selected");
						$(objEvent.currentTarget).addClass("selected");
						
						if (framework.props.sorting.curType != strSortType) {
							framework.props.sorting.curType		= strSortType;
							framework.props.sorting.curDir		= "asc";
						} else {
							if (framework.props.sorting.curDir == "asc") {
								framework.props.sorting.curDir		= "desc";
							} else {
								framework.props.sorting.curDir		= "asc";
							}
						}
						
						var objSortSource		= $("#dataset-" + framework.props.curRace + " #dataset-" + framework.props.curRace + "-source li");
						
						switch (strSortType) {
							case "place" : 
								// sort by place
								$(objSortSource).tinysort("ol>li.place", {order: framework.props.sorting.curDir});
								break;
								
							case "bow" : 
								// sort by bow #
								$(objSortSource).tinysort("ol>li.bow", {order: framework.props.sorting.curDir});
								break;
								
							case "sail" : 
								// sort by sail
								$(objSortSource).tinysort("ol>li.sail", {order: framework.props.sorting.curDir});
								break;
								
							case "name" : 
								// sort by vessel name
								$(objSortSource).tinysort("ol>li.name", {order: framework.props.sorting.curDir, attr: "title"});
								break;
								
							case "owner" : 
								// sort by owner name
								$(objSortSource).tinysort("ol>li.owner", {order: framework.props.sorting.curDir});
								break;
								
							case "time" : 
								// sort by owner time
								$(objSortSource).tinysort("ol>li.time", {order: framework.props.sorting.curDir});
								break;
						}
					}
				}
			}, 
			
		showAllUpdates: 
			function() { 
				framework.props.showingAllUpdates		= true;
				
				$("#whats-happening>a").hide();
				$("#whats-happening-scroll").css("height", "auto");
			}, 
			
		showLeaderboardGraph:
			function() { 
				if (!framework.props.leaderboardGraphShowing) { 
					framework.props.leaderboardGraphShowing = true;
					
					$("#leaderboard-chart").fadeOut(	function() { 
															$("#leaderboard-graph").fadeIn();
														});
				}
			}, 

		hideLeaderboardGraph:
			function() { 
				if (framework.props.leaderboardGraphShowing) { 
					framework.props.leaderboardGraphShowing = false;
					
					$("#leaderboard-graph").fadeOut(	function() { 
															$("#leaderboard-chart").fadeIn();
														});
				}
			}, 

		showStandingsGraph:
			function() { 
				if (!framework.props.standingsGraphShowing) { 
					framework.props.standingsGraphShowing = true;
					
					$("#standings").fadeOut(	function() { 
													$("#standings-graph").fadeIn();
												});
				}
			}, 

		hideStandingsGraph:
			function() { 
				if (framework.props.standingsGraphShowing) { 
					framework.props.standingsGraphShowing = false;
					
					$("#standings-graph").fadeOut(	function() { 
														$("#standings").fadeIn();
													});
				}
			}, 
			
		updateWHHeight: 
			function() {
				if (!framework.props.showingAllUpdates) {
					var intTargetHeight			= ((framework.props.leaderboardGraphShowing) ? $("#leaderboard-graph").height() : $("#leaderboard-chart").height()) - ((framework.props.touchDevice) ? (($("#whats-happening>a").height() + parseInt($("#whats-happening>a").css("marginTop").replace("px", ""))) + 5) : 0);
					
					if (intTargetHeight < framework.props.updatesMinHeight) {
						intTargetHeight				= framework.props.updatesMinHeight;
					}
					
					$("#whats-happening-scroll").css("height", intTargetHeight + "px");
				}
			}
	}
}
