//	JavaScript & CSS support for incorporating the old Arena Broker with Joomla

function getStyleSheet( url)
{
	document.write('<link rel="stylesheet" href="' + url + '" type="text/css" />	');

}	//	getStyleSheet


function getJavaScript( url)
{
	document.write('<script type="text/javascript" src="' + url + '"></scr'+'ipt>');
	//	Note that you should split the closing script tag into two parts.
}	//	getJavaScript


if (window != top)	// inside a frame - assume Joomla
{	//	get template styles: first from Joomla general
//	getStyleSheet( '/cms/Joomla15/templates/system/css/general.css');
	//	SiteGround's nr 22 for Joomla 1.5
	getStyleSheet( '/cms/Joomla15/templates/siteground-j15-22/css/template.css');
	getStyleSheet( '/cms/Joomla15/templates/siteground-j15-22/css/grey.css');
	
	//	define some appropriate custom styles
	getStyleSheet( '/javascripts/JoomlaOn.css');
}
else
{	//	old style
	//	define some appropriate custom styles
	getStyleSheet( '/javascripts/JoomlaOff.css');
}


//	see if there are any (separate) Arena specific things in the url
//	when called from Joomla, and redirect when necesary
//
//	Be very carefull not to loop when calling this !!
//	It's called only from /Arena/Broker/index.html, 
//	since that's where the Joomla wrapper is pointing to
function DispatchFromJoomla()
{	
	if (window != top)	// inside a frame - assume Joomla
	{	//	now see if we need to redirect to a subpage
		//	be defensive!
		
		//	work around Safari's problem with going Back or Forward (it blanks the referrer)
		//	since the cgi scripts defer to Joomla on an emtpy referrer, we get a Droste box effect
		//	it's ugly, but it works, sort of (the Droste effect flashes, than corrects itself)
		var win2replace = (window.parent == top) ? window : window.parent;
		var win2check = window.parent;
		var newloc = null;
	
		if (win2check.location.href.indexOf( "/players-section/Serve.cgi") > 0)		//	game page
		{	newloc = "/cgi-bin/Arena/Serve.cgi" + win2check.location.search;
		}
		else if (win2check.location.href.indexOf( "/players-section/ListGames.cgi") > 0)		//	game page
		{	newloc = "/cgi-bin/Arena/ListGames.cgi" + win2check.location.search;
		}
		else if (win2check.location.href.indexOf( "/players-section/ListScores.cgi") > 0)		//	game page
		{	newloc = "/cgi-bin/Arena/ListScores.cgi" + win2check.location.search;
		}
		else if (win2check.location.href.indexOf( "/players-section/Broker/") > 0)		//	Broker pages, like About and FAQ
		{	newloc = "/Arena" + win2check.location.href.substring( win2check.location.href.indexOf("/Broker/"));
		}
		
		if (newloc != null)
		{	//	see http://www.quirksmode.org/js/detect.html
			//	for this Opera detection
//			if (window.opera)
//			{	//	(older?) Opera doesn't like changing the location of an iframe
//				//	instead the src of the iframe has to be changed
//				//	see http://www.quirksmode.org/js/iframe.html
//				win2replace.parent.document.getElementById( 'blockrandom').src = newloc;
//				//	note the iframe id is defined in Joomla!
//				//	so, although this works for all browsers, we limit usage to Opera anyway
//			}
//			else
//			{	//	this seems to work fine with Opera 9.52 too, so commented the previous bit

				win2replace.location.replace( newloc);

//			}
		}
	}
	else	//	window == top
	{	//	/Arena/Broker/index.html _not_ inside a frame
		if (top.location.href.indexOf( "/Arena/Broker/") > 0)		//	doesn't fix Safari's bug :( but can't hurt
		{	//	nothing fancy
			top.location.replace( "/index.php/players-section/");		//	Joomla wrapper
		}
	}

}	//	DispatchFromJoomla


//	make sure the other (old) broker files open within Joomla too
//	don't use DispatchFromJoomla for this, or it will loop indefinitely
function DispatchFromOldBroker()
{
	if (window == top)	// not inside a frame - so not in Joomla
	{
		var newloc = null;

		if (top.location.href.indexOf( "/Arena/Broker/") > 0)		//	Broker pages, like About and FAQ
		{	newloc = "/index.php/players-section" + top.location.href.substring( top.location.href.indexOf("/Broker/"));
		}
		
		if (newloc != null)
		{	window.location.replace( newloc);
		}

	}

}	//	DispatchFromOldBroker
