﻿String.format = function(text) {
	if (arguments.length <= 1)
		return text;
	var tokenCount = arguments.length - 2;
	for (var token = 0; token <= tokenCount; token++)
		text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
	return text;
};
function ShowMessage(msg) {
	$("#Results").prepend(String.format("{0}<br />", msg));
}
function CreateSpans(s1, s2) {
	return String.format("{0}<span>{1}</span>", s1, s2);
}
function HideLoadingMsg() {
	if (LoadingDepth > 0)
		LoadingDepth = LoadingDepth - 1;
	if (LoadingDepth == 0)
		$(".Loading").fadeOut("normal");
}
function ShowLoadingMsg() {
	LoadingDepth = LoadingDepth + 1;
	$(".Loading").fadeIn("normal");
}
var gameClasses = ["Started", "Paused", "Finished"];
function ProcessScores(Scores) {
	for (var i = 0; i < (Scores.length); i++) {
		var thisScore = Scores[i];
		var tbody = $("#gid_" + thisScore.game_id);
		if (tbody.length > 0) {
			tbody.addClass(gameClasses[thisScore.scoreStatus - 1]);
			$("#awayScore_" + thisScore.game_id).addClass("ChangedScore").html(CreateSpans(thisScore.awayScore, thisScore.awayStatus));
			$("#homeScore_" + thisScore.game_id).addClass("ChangedScore").html(CreateSpans(thisScore.homeScore, thisScore.homeStatus));
		}
	}
}
function SetScoresTimeout() {
	setTimeout(LoadScores, ScoresInterval);
}

function ScoresLoaded(data, textStatus) {
	if (textStatus == "success") {
		RemoveClass("Normal");
		RemoveClass("Started");
		RemoveClass("Finished");
		RemoveClass("ChangedScore");
		ProcessScores(data.Scores);
	};
	HideLoadingMsg();
	SetScoresTimeout();
}
function ScoresError(XMLHttpRequest, textStatus, errorThrown) {
	HideLoadingMsg();
	SetScoresTimeout();
}
function LoadScores() {
	$.ajax({
		type: "GET",
		url: "jsdata/scoreshandler.ashx",
		data: { show: Math.random(), leagueId: LeagueId, urgents: 0},
		dataType: "json",
		error: ScoresError,
		success: ScoresLoaded,
		timeout: 15000
	});
}
function createItems(items) {
	var s = "";
	for (var i = 0; i < items.length; i++)
		s = s + String.format("<span class='{0}'>{1}</span>", items[i].cls, items[i].dsp);
	return s;
}
function RemoveClass(className) {
	return $("." + className).removeClass(className);
}
function DemoteClasses() {
	var CurrClass;
	var PrevClass;
	$(".change5").removeClass("change5");
	for (var i = 5; i > 0; i--) {
		CurrClass = "change" + i;
		PrevClass = "change" + (i - 1);
		RemoveClass(PrevClass).addClass(CurrClass);
	}
}
function CleanChanges(target) {
	for (var i = 1; i <= 5; i++)
		target.removeClass("change" + i);
}
function ShowMenuBar() {
	if (FirstTime == true) {
		FirstTime = false;
		$(".Links").fadeIn();
		$(".WeatherImg").show();
	}
}
function SetChangesTimeout() {
	setTimeout(LoadChanges, ChangesInterval);
}
function ChangesLoaded(data, textStatus) {
	if (textStatus == "success") {
		DemoteClasses();
		var target;
		var itm;
		var tgId;
		for (var i = 0; i < data.Changes.length; i++) {
			itm = data.Changes[i];
			tgId = "#" + itm.sd + "_" + itm.bk + "_" + itm.gmid + " ." + itm.cls;
			target = $(tgId);
			if (target && target.length > 0) {
				var PrevLine = target.text();
				if (PrevLine != itm.val) {
					var LineChangeText = "from " + target.text() + " to " + itm.val;
					CleanChanges(target.parent());
					target.text(itm.val).attr("title", LineChangeText).parent().addClass("change0");
				}
			}
		}
		HideLoadingMsg();
	}
	ShowMenuBar();
	SetChangesTimeout();
}
function ChangesError(XMLHttpRequest, textStatus, errorThrown) {
	HideLoadingMsg();
	SetChangesTimeout();
	ShowMenuBar();
}
function LoadChanges() {
	$.ajax({
		type: "GET",
		url: "jsdata/freechangeshandler.ashx",
		data: { rnd: Math.random(), leagueId: LeagueId },
		dataType: "json",
		error: ChangesError,
		success: ChangesLoaded,
		timeout: 15000
	});
}
function LinesLoaded(data, textStatus) {
	if (textStatus == "success") {
		for (var i = 0; i < (data.length); i++) {
			var thisItem = data[i];
			$("#away_" + thisItem.id).html(createItems(thisItem.away));
			$("#home_" + thisItem.id).html(createItems(thisItem.home));
		} // for
	} // if (textStatus == "success")
	SetLineCategory(DefaultLineCategory);
	LoadChanges();
	HideLoadingMsg();
}
function LinesError(XMLHttpRequest, textStatus, errorThrown) {
	HideLoadingMsg();
	ShowMenuBar();
}
function LoadLines() {
	$.ajax({
		type: "GET",
		url: "jsdata/lineshandler.ashx",
		data: { show: "DL", leagueId: LeagueId },
		dataType: "json",
		error: LinesError,
		success: LinesLoaded,
		timeout: 15000
	});
}
var LastSelectedClass = ".DL";
function SetLineCategory(newCat) {
	newCat = "." + newCat;
	$(LastSelectedClass).hide();
	$(newCat).show();
	LastSelectedClass = newCat;
}
function GotoLeagueCategory(league, cat) {
		window.location = String.format("FreeScores.aspx?LeagueId={0}&DefaultCat={1}", league, cat);
}

function GotoCasinoSet(league, CasinoSet) {
	var s = String.format("FreeScores.aspx?LeagueId={0}&Show={1}", league, CasinoSet);
	window.location = s;
}

function InjuriesLoaded(data, textStatus) {
	if (textStatus == "success") {
		$("#InjuriesContent").html(data);
		$("#InjuriesDisplay").jqm({ modal: true,
			onHide: function(h) {
				h.w.fadeOut("fast"); // hide window
				h.o.remove(); // remove overlay
			},
			onShow: function(h) {
				h.w.fadeIn("fast");
			}
		}).jqmShow();
	}
	HideLoadingMsg();
}
function InjuriesError(XMLHttpRequest, textStatus, errorThrown) {
	HideLoadingMsg();
}
function ShowInjuries(showLeague, LeagueName) {
	if (LeagueName)
		$("#InjuriesLeague").text(LeagueName);
	ShowLoadingMsg();
	$.ajax({
		type: "GET",
		url: "jsdata/InjuriesHandler.ashx",
		data: { leagueId: showLeague },
		cache: true,
		dataType: "html",
		error: InjuriesError,
		success: InjuriesLoaded,
		timeout: 10000
	});
}
function HideInjuries() {
	$("#InjuriesContent").html("");
	$("#InjuriesDisplay").jqmHide();
}
function reloadAll() {
	history.go(0);
}
function ShowWeather(display, position) {
	$("#WeatherDisplay").
		css("top", position.top + 10).
		css("left", position.left + 25).
		html(display.split("|").join("<br />")).
		fadeIn("fast");
}
function HideWeather() {
	$("#WeatherDisplay").fadeOut("fast");
}
function SetGameInfoTimeout() {
	setTimeout(LoadGameInfo, ScoresInterval);
}
function GameInfoLoaded(data, textStatus) {
	if (textStatus == "success") {
		for (var i = 0; i < data.GameInfoItems.length; i++) {
			var itm = data.GameInfoItems[i];
			$("#capr" + itm.GameId).
				html(
					String.format("{1}&nbsp;&nbsp;<a target='_top' href='gotohandicapper.aspx?handicapperid={0}'>Click Here</a>", itm.CapperId, itm.Message)
				);
		}
	};
	HideLoadingMsg();
	SetGameInfoTimeout();
}
function GameInfoError(XMLHttpRequest, textStatus, errorThrown) {
	HideLoadingMsg();
	SetGameInfoTimeout();
}
function LoadGameInfo() {
	// ShowLoadingMsg();
	$.ajax({
		type: "GET",
		url: "jsdata/GameCapperInfoHandler.ashx",
		data: { show: Math.random(), leagueId: LeagueId },
		dataType: "json",
		error: GameInfoError,
		success: GameInfoLoaded,
		timeout: 15000
	});
}

function PropsError() {
}
function PropsLoaded(data, textStatus) {
	$("#InnerProps").html(data);
	$("#PropsViewer").jqm({ modal: true,
		onHide: function(h) {
			h.w.fadeOut("fast"); // hide window
			h.o.remove(); // remove overlay
		},
		onShow: function(h) {
			h.w.fadeIn("fast");
		}
	}).jqmShow();
	$(".PropDetail div:odd").addClass("OddItem");
	$(".PropSurround").click(function() {
		var s = "#PropDetail" + $(this).attr("idx");
		$(".PropDetail").not(s).slideUp("fast");
		$(s).slideDown("fast");
	});
}
function ShowProps(LeagueId, LeagueName) {
	$("#PropsLeague").text(LeagueName);
	$.ajax({
		type: "GET",
		url: "jsdata/propsHandler.ashx",
		data: { leagueId: LeagueId },
		dataType: "html",
		error: PropsError,
		success: PropsLoaded,
		cache: true,
		timeout: 15000
	});
}
function HideProps() {
	$("#InnerProps").html("");
	$("#PropsViewer").jqmHide();
}
var HashCodes = new Object();
var UrgentChanges = new Object();
var FirstTime = true;
var LoadingDepth = 0;
$(
			function() {
				$("tbody:odd").addClass("OddRow");
				LoadLines();
				LoadScores();
				LoadGameInfo();
				setTimeout(reloadAll, 1000 * 60 * 60 * 15);
				$(".WeatherImg").hover(
					function() {
						ShowWeather($(this).attr("WeatherData"), $(this).position());
					},
					function() {
						HideWeather();
					});
			}
		);
