/**
 * jQuery 外部定義
 */
$(function() {

	formatScore();

	query = window.location.search.substring(1);

	jQuery.getJSON('script/d6List.json', function(json) {
		jsonData = json;
		
		// ページデータ表示
		displayPageData();
		
		// 試合リスト表示
		display6dList();
		
		$("#" + query).css("backgroundColor", "#fff");
		
		
	});
	
	jQuery.getJSON('script/d6Result.json', function(json) {
		scoreData = json;
		
		// スコア表示
		displayScore();
	});
	
	/********************/

	// 試合リストイベント
	
	$("#6dTokyo").click(function() {
		d6Sort = "tokyo";
		display6dList();
	});
	$("#6dWaseda").click(function() {
		d6Sort = "waseda";
		display6dList();
	});
	$("#6dRikkio").click(function() {
		d6Sort = "rikkio";
		display6dList();
	});
	$("#6dHosei").click(function() {
		d6Sort = "hosei";
		display6dList();
	});
	$("#6dKeio").click(function() {
		d6Sort = "keio";
		display6dList();
	});
	$("#6dMeiji").click(function() {
		d6Sort = "meiji";
		display6dList();
	});
	$("#6dAll").click(function() {
		d6Sort = "all";
		display6dList();
	});
	
	// スコア表示切り替えイベント
	$("#switcher").click(function() {
		switch (scoreStatus) {
			case "hidden":
				scoreStatus = "visible";
				break;
			case "visible":
				scoreStatus = "hidden";
				break;
		}
		$(this).removeClass();
		$(this).addClass(scoreStatus);
		displayScore();
	});
		
	function display6dList() {
		$("#d6Lists").empty();
		$.each(jsonData, function(key) {
			switch (d6Sort) {
				case "all":
					if (this.display == "yes") {
						$("#d6Lists").append(setD6List(this, key));
					}
					break;
				default:
					if (this.display == "yes" && (this.team1 == d6Sort || this.team2 == d6Sort)) {
						$("#d6Lists").append(setD6List(this, key));
					}
			}
		});
	}	
	
	// ページ表示試合オブジェクト
	function displayPageData() {
		if (query.length && jsonData[query] != undefined) {
			matchTeam = jsonData[query];
			
			// 試合タイトル
			$("#title").text(setMatchTitle(matchTeam));
			
			// 大学名
			$("#match-team1").html(setUniversity(matchTeam, "left"));
			$("#match-team2").html(setUniversity(matchTeam, "right"));
			
			// スコアボードチーム名
			$("#team1").html(getD6Name(matchTeam.team1));
			$("#team2").html(getD6Name(matchTeam.team2));			
		}	
	}
	
	function displayScore() {
		switch (scoreStatus) {
		case "visible":
	
			if (query.length && scoreData[query] != undefined) {
				gameScore  = scoreData[query];
			
				// スコア
				$(gameScore.team1).each(function(i) {
					switch (this.toString()) {
					case "98":
						$("#team1_" + (i + 1)).html("X");
						break;
					case "99":
						$("#team1_" + (i + 1)).html("&nbsp;");
						break;
					default:
						if (i === 0)  {
							$("#team1_" + (i + 1)).html(this.toString());
						} else {
							$("#team1_" + (i + 1)).html(this.toString());
						}
						
						if (0 <= i && i <= 11) {
							$("#team1_" + (i + 1)).mouseover(function() {
								$(this).css({ backgroundColor: "#eff1e6", color: "#000", cursor: "pointer" });
							});
							$("#team1_" + (i + 1)).mouseout(function() {
								$(this).css({ background: "none", color: "#fff", cursor: "default" });
							});
							$("#team1_" + (i + 1)).click(function() {
								fPlayer.location.href = "player.html?" + query.substring(1) + "_" + sn(i + 1) + "_01_" + movieType; 
							});
						}
					}
				});
				$(gameScore.team2).each(function(i) {
					switch (this.toString()) {
					case "98":
						$("#team2_" + (i + 1)).html("X");
						break;
					case "99":
						$("#team2_" + (i + 1)).html("&nbsp;");
						break;
					default:
						if (i === 0)  {
							$("#team2_" + (i + 1)).html(this.toString());
						} else {
							$("#team2_" + (i + 1)).html(this.toString());
						}
						
						if (0 <= i && i <= 11) {
							$("#team2_" + (i + 1)).mouseover(function() {
								$(this).css({ backgroundColor: "#eff1e6", color: "#000", cursor: "pointer" });
							});
							$("#team2_" + (i + 1)).mouseout(function() {
								$(this).css({ background: "none", color: "#fff", cursor: "default" });
							});
							$("#team2_" + (i + 1)).click(function() {
								fPlayer.location.href = "player.html?" + query.substring(1) + "_" + sn(i + 1) + "_02_" + movieType; 
							});
						}
					}
				});
			}
			break;
		
		case "hidden":
			for (var i = 1; i <= 15; i++) {
				$("#team1_" + i).html("");
				$("#team1_" + i).unbind("mouseover");
				$("#team1_" + i).unbind("mouseout");
				$("#team1_" + i).unbind("click");
				$("#team2_" + i).html("");
				$("#team2_" + i).unbind("mouseover");
				$("#team2_" + i).unbind("mouseout");
				$("#team2_" + i).unbind("click");
			}
			break;
			
		} // end switch
	}
	
	function formatScore() {
		src = "<table summary=\"対戦チーム\" style=\"width:410px;\">";
		src += "<tr><th>&nbsp;</th>";
		for (var i = 1; i <= 12; i++) {
			src += "<th>" + i + "</th>";
		}
		src += "<th>R</th><th>H</th><th>E</th></tr>";
		src += "<tr>";
		src += "<td id=\"team1\" nowrap=\"nowrap\" style=\"text-align:left;\">&nbsp;</td>";
		for (var i = 1; i <= 15; i++) {
			src += "<td id=\"team1_" + i + "\">&nbsp;</td>";
		}
		src += "</tr>";
		src += "<tr>";
		src += "<td id=\"team2\" nowrap=\"nowrap\" style=\"text-align:left;\">&nbsp;</td>";
		for (var i = 1; i <= 15; i++) {
			src += "<td id=\"team2_" + i + "\">&nbsp;</td>";
		}
		src += "</tr>";
		src += "</table>";
		
		$("#scoreboard").append(src);
	}
	

});

