/* JS file for user_stats */
var us_ajax_timeout = 15000;
var us_container = null;


/**
 * Init the user stats js file.
 */
function us_init() {
	//get container element
	us_container = document.getElementById('user_stats_container');
	
	//preload indicator image
	var preload_image = new Image();
	preload_image.src = us_loading_image_url;
	
	//load stats
	us_load_stats();
}

/**
 * Retrieve the stats for the given user
 */
function us_load_stats() {
	//show loading indicator
	show_us_loading('user_stats_container');
	
	//make ajax request
	var connectionObject = YAHOO.util.Connect.asyncRequest(
		'GET',
		us_ajax_url + '?a=load_stats'
		            + '&cust_id=' + us_cust_id
		            + '&cust_uid=' + us_cust_uid
		            + '&stats=' + us_stats
		            + '&site_url=' + us_site_url,
		{
			success: handle_us_load_stats,
			failure: us_report_error,
			timeout: us_ajax_timeout
		}
	);
}

/**
 * Handle the response from the us_load_stats function and print the 
 * results to the div.
 */
function handle_us_load_stats(response) {
	//print to container
	us_container.innerHTML = response.responseText;
}

/**
 * Report that there was an error with the Ajax request.
 */
function us_report_error(request) {
	//print error message
	us_container.innerHTML = 'Unable to load stats.';
}

/**
 * Blur the Audio Memebers module
 */
function us_blur() {
	us_container.focus();
	us_container.blur();
}

/**
 * Show the loading indicator for an Ajax request and place the indicator
 * in the given parent element.
 */
function show_us_loading(parent_id) {
	//blur module
	us_blur();
	
	//get parent
	var parent = document.getElementById(parent_id);
	
	//remove all content
	//parent.innerHTML = '';
	
	//add loading image to parent
	var loadingdiv = document.createElement("div");
	loadingdiv.style.position = 'absolute';
	loadingdiv.style.top = '0px';
	loadingdiv.style.width = parent.offsetWidth + 'px';
	loadingdiv.style.textAlign = 'center';
	/*
	loadingdiv.innerHTML = '<image src="' + us_loading_image_url + '" '
	                        + 'alt="Loading..." '
	                        + 'style="width:20px;margin:0 auto;position:relative;'
	                               + 'top:5px;text-align:center;" />';
	*/
	//loadingdiv.innerHTML = 'Loading...';
	
	//add to parent
	parent.appendChild(loadingdiv);
}