/**
* Soccersuite Sats Javascript
*/

var http = create_request_object();
var content_hide = new Array();
var content_show = new Array();
var content_container = '';
// urls
var root_path = './../../../';
var user_stats_url = root_path + 'soccersuite/ajax/retrieve_user_stats.php';
var user_predictions_url = root_path + 'soccersuite/ajax/retrieve_user_predictions.php';

function create_request_object()
{
	var request_o; 
	var browser = navigator.appName; 
	
	if(browser == "Microsoft Internet Explorer")
	{
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		request_o = new XMLHttpRequest();
	}
	return request_o; 
}

function get_value(req_url)
{
	http.open('get', req_url);
	http.onreadystatechange = handle_data;
	http.send(null);
}

function handle_data()
{
	if(http.readyState == 4)
	{
		var response = http.responseText;
		
		document.getElementById(content_container).innerHTML = response;
		document.getElementById(content_container).style.display = 'block';
		// hide any elements if requiured
		for (i = 0; i < window.content_hide.length; i++)
		{
			document.getElementById(window.content_hide[i]).style.display = 'none';
		}
		// show any elements if requiured
		for (i = 0; i < window.content_show.length; i++)
		{
			document.getElementById(window.content_show[i]).style.display = 'block';
		}
	}
}

/* statistical specific functions */
function display_user_stats(user_id)
{
	if (!user_id.value)
	{
		return;
	}
	
	document.getElementById('user_stats_container').style.display = 'block';
	document.getElementById('loading').style.display = 'block';
	// reset any previous stats
	document.getElementById('predictions_content').innerHTML = '';
	document.getElementById('content').innerHTML = '';
	
	window.content_container = 'content';
	
	window.content_hide = new Array('loading');
	window.content_show = new Array();
	
	get_value(window.user_stats_url + '?user=' + user_id.value);	
}

function show_predictions(user_id, mode)
{
	if (!user_id)
	{
		return;
	}
	
	document.getElementById('predictions_loading').style.display = 'block';
	document.getElementById('predictions_content').style.display = 'none';
	
	window.content_container = 'predictions_content';
	
	window.content_hide = new Array('predictions_loading');
	
	get_value(window.user_predictions_url + '?user=' + user_id + '&mode=' + mode);
}

function switch_predictions(user_id, stamp, mode)
{
	if (!stamp.value || !user_id)
	{
		return;
	}
	// reset any previous stats
	document.getElementById('predictions_loading').style.display = 'block';
	document.getElementById('prediction-content-container').style.display = 'none';
	
	window.content_hide = new Array('predictions_loading');
	
	window.content_container = 'prediction-content-container';
			
	get_value(window.user_predictions_url + '?user=' + user_id + '&stamp=' + stamp.value + '&action=switch' + '&mode=' + mode);
	
	window.location.href='#predictions';
}
