var popularsCache = new Array(3);
var popularsDivs = new Array(3);
popularsDivs[0] = 'menu_stats_pastday';
popularsDivs[1] = 'menu_stats_past7days';
popularsDivs[2] = 'menu_stats_ever';

function GamesAjax() {
	this.request = new XHR(15000);
	this.serverScript = 'games.php?m=ajax';
	this.request.SetErrorFunc(setWindowCursorDefault)

	this.getStats = function(view) {
		var postInfo = 'act=switchPopularTimeframe&view='+view;
		this.request.Post(this.serverScript,postInfo,this.getStatsResult, view);
		setWindowCursorWait(); //waiting cursur
	}

	this.getStatsResult = function(responseText, view) {
		$('populars_container').innerHTML = responseText;
		popularsCache[view] = responseText;
		setWindowCursorDefault(); //default cursor
	}
	
	this.setMyFavorites = function(game_id) {
		var postInfo = 'act=addfavoritegame&gid='+game_id;
		this.request.Post(this.serverScript,postInfo,this.setMyFavoritesResult);
		setWindowCursorWait(); //waiting cursor
	}

	this.setMyFavoritesResult = function(responseText) {
		if  (responseText=='success') {
			$('favorites_btn').innerHTML = $('favorites_msg_added').innerHTML; 
		} else {
			$('favorites_btn').innerHTML = responseText;
		}
		setWindowCursorDefault(); //default cursur
	}
}

function switchPopularTimeframe(view) {
	// internal page cache to prevent multi-requests per stats-list 
	if (typeof popularsCache[view] == 'undefined')
		gamesAjax.getStats(view);
	else
		$('populars_container').innerHTML = popularsCache[view];
	
	// switch block colors
	for (var stats_div_id in popularsDivs) {
		$(popularsDivs[stats_div_id]).className='right menu_stats_block_off';
	}
	$('menu_stats_'+view).className='right menu_stats_block_on';
}

gamesAjax = new GamesAjax();

docReady.push(function() { switchPopularTimeframe('pastday'); });