// main.js

var knMAIN_SIDE_TABS = 8;	// 7 for 2007 indoor!
function showTab(nWhich)
{
	indicatorOn(nWhich);
	
	var nIt = 1;
	for (; nIt <= knMAIN_SIDE_TABS; ++nIt)
	{
		$('#tcontent' + nIt).hide();
		//	var elTab = document.getElementById('tcontent' + nIt);
		//	if (elTab)
		//	{
		//		elTab.style.display = 'none';
		//		elTab.style.display = (nIt == nWhich ? 'block' : 'none');
		//	}
	}
	
	if (nWhich == 2)
	{
		$('#tcontent2').load('get.php', {what: 'recentMatches'});	//, getDone);
	}
	if (nWhich == 3)
	{
		$('#tcontent3').load('get.php', {what: 'upcomingMatches'});	//, getDone);
	}

	if (nWhich >= 4 && nWhich < 7)
	{
		$('#tcontent' + nWhich).load('get.php', {what: 'schedule', div: 'ABM'.charAt(nWhich - 4)});	//, getDone);
	}

	if (nWhich == 7)
	{
		$('#tcontent7').load('get.php', {what: 'cuplinks'});	//, getDone);
	}
	
	$('#tcontent' + nWhich).fadeIn("medium");
}


function showSBTab(sId)
{
	$(".sbtIndicator").each(function(nIt, el) { $(el).removeClass('sbtActive'); });
	$("#sbtIndicator" + sId).addClass('sbtActive');
	
	$(".sbtab").hide();
	
	var $tab = $("#sbt" + sId);
	if ($tab)
	{
		// [0] fails in f'n IE6; works elsewhere
		if ($tab.html().charAt(0) == ':')
		{
			$("#sbtLoading").fadeIn("fast");

			var myData = $tab.html().split(':');
			// nId - getWhat - div
			
			if (myData)
			{
				//ajaxSend(function(evt, request, settings){
				$tab.load('get.php', {what: myData[2], div: myData[3]}, 
					function(responseText, textStatus, XMLHttpRequest)
					{
						$("#sbtLoading").hide();
						$tab.fadeIn("medium");
					});
			}
		}
		else
		{	// we've already got it, just fade it in
			$tab.fadeIn("medium");
		}
	}
	else
	{
		alert("couldn't find sidebar tab " + sId);
	}
}

function getDone()
{
	alert('get.php -- done\n\n');
}

function indicatorOn(nWhich)
{
	var nIt = 1;
	for (; nIt <= knMAIN_SIDE_TABS; ++nIt)
	{
		var elIndicator = document.getElementById('tindicator' + nIt);
		if (elIndicator)
		{
			elIndicator.style.backgroundColor = (nIt == nWhich ? '#6f6' : '');
		}
	}
}

function extrainfoShow(trigger, sContent)
{
	var extra = document.getElementById('extrainfo');
	if (extra)
	{
		extra.style.display = 'block';
//		if (!$(extra).is(':visible'))
//		{
//			$(extra).fadeIn("medium");
//		}

		var content = document.getElementById('extracontent');
		if (content)
		{
			content.innerHTML = sContent.replace(/~/g, "'");;
		}
	
		var aXY = findPosition(trigger);
		if (aXY)
		{
			extra.style.top = (aXY[1] - 6) + "px";
	
			aWindowSize = getWindowSizeXY();
			aWindowScroll = getWindowScrollXY();
			if (extra.offsetTop + extra.offsetHeight - aWindowScroll[1] > aWindowSize[1])
			{
				extra.style.top = extra.offsetTop - (extra.offsetTop + extra.offsetHeight - aWindowSize[1] - aWindowScroll[1]) + "px";
			}

//			window.status = extra.offsetTop + " + " + extra.offsetHeight + " = " + (extra.offsetTop + extra.offsetHeight) + " >? " + aWindowSize[1] + " - " + aWindowScroll[1] + " = " + (aWindowSize[1] - aWindowScroll[1]);
		}
	}
}

function extrainfoHide()
{
//	$('#extrainfo').fadeOut("slow");
	$('#extrainfo').hide();
	//var extra = document.getElementById('extrainfo');
	//if (extra)
	//{
	//	extra.style.display = 'none';
	//}
}


function findPosition(oLink)
{
	if (oLink.offsetParent)
	{
		var sPath = '';
		for (var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent )
		{
			posX += oLink.offsetLeft;
			posY += oLink.offsetTop;
		}
		return [posX, posY];
	}
	else
	{
		return [oLink.x, oLink.y];	// Netscape 4, I believe
	}
}


// http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=16
function getWindowSizeXY() {
	var myWidth = 0, myHeight = 0;
	if (typeof(window.innerWidth) == 'number')
	{
		// Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else
	{
		if (document.documentElement && 
			(document.documentElement.clientWidth || document.documentElement.clientHeight))
		{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		}
		else
		{
			if (document.body && (document.body.clientWidth || document.body.clientHeight))
			{
				// IE 4 compatible
				myWidth = document.body.clientWidth;
				myHeight = document.body.clientHeight;
			}
		}
	}
	
	return [myWidth, myHeight];
}