var ll_currentGameCell = null;

$(document).ready(function()
{
	if ($("div.gameDate").size() > 0)
	{
		//	clicking events and hovering effects for the game cells
		$("div.gameDate.public td,div.gameDate.public div.teamAssignmentSlot").not(".gameTime,.locationSpacer,.noGame").click (
			function (e)
			{
				showGameDetailPopup ($(this),e);
				return false;
			}
		).mouseenter (
			function (e)
			{
				$(this).addClass("hover");
			}
		).mouseleave (
			function (e)
			{
				$(this).removeClass("hover");
			}
		).addClass("clickable");

		//	clicking events and hovering effects for the teamname links
		$("div.gameDate.public td a,div.gameDate.public div.teamAssignmentSlot a").unbind ("click").click(
			function (e)
			{
				e.stopPropagation ();
				return true;
			}
		).mouseenter (
			function (e)
			{
				$(this).addClass ("hover").parent ("td").addClass("linkHover");
			}
		).mouseleave (
			function (e)
			{
				$(this).removeClass("hover").parent("td").removeClass("linkHover");
			}
		);
	}
	
	if ($("#teamScheduleTable").size () > 0)
	{
		$("#teamScheduleTable a.openGameDetail").click (
			function (e)
			{
				showGameDetailPopup ($(this).parent ("td"),e);
				return false;
			}
		);
	}
	
	$("#gameDetailPopup a.closeWindowButton").click (
		function ()
		{
			clearGameCell ();
		}
	);
});

function showGameDetailPopup (jGameCell, e)
{
	clearGameCell ();
	ll_currentGameCell = jGameCell;
	setLoadingState (true);
	var position = getPopupCoordinates (e);
	show (cacheGetJQ("#gameDetailPopup").css({"top":position.y,"left":0}));
	
	jGameCell.addClass("viewing");
	info = getGameCellDetails (jGameCell);
	info["Time"] = info["RealTime"];
	info["LeagueID"] = $(jGameCell).parents ("tr").attr ("id")
		? extractID ($(jGameCell).parents ("tr").attr ("id"))
		: jGameCell.parents("div.gameDate").find("input.gameDateHiddenLeagueIDInput").val();

	$.post('/game-detail/', info, function(response,status)
	{
		if(status == "success" && response != "ERROR")
		{
			cacheGetJQ("#gameDetailPopupContent").html (response);
			if ($.browser.msie && $.browser.version == "7.0")
			{
				//	WOW, this hack is almost as awful as IE7
				setTimeout (function ()
				{
					cacheGetJQ("#gameDetailPopup").width (Math.max($("table.gameDetailTable").width() + 60, 530));
				}, 300);
			}
			setLoadingState (false);
		}
		else
		{
			refreshPage ();
		}
	},"text");
}

function clearGameCell ()
{
	if (ll_currentGameCell)
	{
		ll_currentGameCell.removeClass ("viewing");
		ll_currentGameCell = null;
	}
}

//	determine whether to show the loading graphic or not
function setLoadingState (fLoading)
{
	if (fLoading)
	{
		cacheGetJQ("#gameDetailPopupContent").addClass ("hidden");
		cacheGetJQ("#schedulePopUpLoading").removeClass ("hidden");
	}
	else
	{
		cacheGetJQ("#schedulePopUpLoading").addClass ("hidden");
		cacheGetJQ("#gameDetailPopupContent").removeClass ("hidden");
	}
} 
