/*****************************************************************************
*	CIFL GameCenter Live Stats JS code behind
*	
*	Written by Dan Rushe
*	©2010 VBW Software
*
*	This code-behind conains the support functions for the live stats page.
*****************************************************************************/

var Accordion1;		// accordian panel
var TabbedPanels1;	// tabbed panel
var openPanel = 0; 	// currently open accordian panel
var openTab = 0;	// currently open tab
var timerID = 0;		// currentRefresh timer ID
var selectedRadio = 0;	// currently selected radio button

/******************************************************************************
*	This function positions the scoreboard on the screen
*******************************************************************************/
function positionScoreboard()
{
	var posX;	// new x-coordinate of the scoreboard
	var scoreboard = document.getElementById("liveBoard");	// scoreboard element
	
	// calculate the new position
	posX = (document.body.scrollWidth - scoreboard.scrollWidth) / 2 + 7.5;
	
	// use browser-safe properties
	if(document.styleSheets[0].rules)
	{
		document.styleSheets[0].rules[2].style.left = posX + "px";
	}
	else
	{
		document.styleSheets[0].cssRules[3].style.left = posX + "px";
	}
		
}

/******************************************************************************
*	This function is called to initalize the page after it loads.
******************************************************************************/
function init()
{
	var scoreboard;		//scoreboard panel
	var content;		// panel content
	
	refreshStats();		// load the stats
	positionScoreboard();
	
	scoreboard = document.getElementById("liveBoard");
	content = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"\n';
	content += '	codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="479" height="369" id="scoreboard" align="middle">\n';
	content += '	<param name="allowScriptAccess" value="sameDomain" />\n'
	content += '	<param name="allowFullScreen" value="false" />\n'
	content += '	<param name="movie" value="scoreboard.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#e5e5e5" />\n'
	content += '	<embed src="scoreboard.swf" quality="high" bgcolor="#e5e5e5" width="479" height="369" name="scoreboard" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\n'
	content += '	</object>';
	
	scoreboard.innerHTML = content;
}

/****************************************************************************************
*	This function loads the XML data from the server and applies the XST to it.
*****************************************************************************************/
function refreshStats()
{
	var content;
	var element = document.getElementById("mainFrame");
	
	// determine browser
	if(navigator.userAgent.indexOf("MSIE") > 0)
		content = loadStatsIE();
	else
		content = loadStatsMoz();
	
	// replace content
	if(content != null)
		element.innerHTML = content;
	
	// create Spry widgets
	Accordion1 = new Spry.Widget.Accordion("statsAccordian", {defaultPanel: openPanel});
	TabbedPanels1 = new Spry.Widget.TabbedPanels("DriveTabs", {defaultTab: openTab});
	
	document.forms[0].intervalRadio[selectedRadio].checked = true;
	resetInterval(selectedRadio);
}

/****************************************************************************************
*	This function loads the XML data for Internet Explorer.
*
*	Returns:
*		String: HTML content
*****************************************************************************************/
function loadStatsIE()
{
	var xml = new ActiveXObject("Microsoft.XMLDOM");
	var xslt = new ActiveXObject("Microsoft.XMLDOM");
	
	// force the documents to load completely
	xml.async = false;
	xslt.async = false;
	xslt.validateOnParse = false;
	
	// load the XML data and stylesheet
	if(!xml.load("stats.xml"))
		return;	// exit on bad refresh
		
	xslt.load("livestats.xsl");
	
	// transform the XML and return the content
	return xml.transformNode(xslt);
}

/****************************************************************************************
*	This function loads the XML data for Mozilla (not IE) browsers.
*
*	Returns:
*		String: HTML content
*****************************************************************************************/
function loadStatsMoz()
{
	var xml = document.implementation.createDocument("", "", null);
	var xslt = document.implementation.createDocument("", "", null);
	var processor = new XSLTProcessor();
	var xmlDOM;
	var serializer = new XMLSerializer();
	
	// force the script to wait for the documents
	xml.async = false;
	xslt.async = false;
	
	// load the documents
	try
	{
		xml.load("stats.xml");
		xslt.load("livestats.xsl");
	}
	catch(ex)
	{
			// web browser does not support this method, so load the page the hard way.
			var loader = new XMLHttpRequest();
			
			// load xml file
			loader.open("GET", "stats.xml", false);
			loader.send(null);
			xml = loader.responseXML.documentElement;
			
			// load stylesheet
			loader = new XMLHttpRequest();
			loader.open("GET", "livestats.xsl", false);
			loader.send(null);
			xslt = loader.responseXML.documentElement;
	}
	
	try
	{
		processor.importStylesheet(xslt);	// import the stylesheet into the processor
		xmlDOM = processor.transformToDocument(xml);	// perform the transformation
	}
	catch(ex)
	{
		// refresh failed; do not change page
		return;
	}
		// serialize the document into a string and return it
		return serializer.serializeToString(xmlDOM.documentElement);
}

/********************************************************************************************
*	This function sets the refresh interval
*
*	Inputs: 
*		interval: number of seconds to set for refresh interval; 0 to disable timer.
*********************************************************************************************/
function setRefreshTimer(interval)
{
	// clear out any existing interval
	if(timerID != 0)
		clearInterval(timerID);
		
	// set the new timer
	if(interval != 0)
		timerID = setInterval(refreshStats, interval * 1000);
}

/********************************************************************************************
*	This function sets the refresh interval using the selected radio button
*
*	Inputs: 
*		index: the radio button selected
*********************************************************************************************/

function resetInterval(index)
{	
	switch(index)
	{
		case 0:
			setRefreshTimer(15);
			break;
			
		case 1:
			setRefreshTimer(30);
			break;
			
		case 2:
			setRefreshTimer(60);
			break;
			
		case 3:
			setRefreshTimer(0);
			break;
	}
	
	selectedRadio = index;
}
