/* ### top-10 block ### */
var last_top10_period_requested = 'day';
var block_top10_cached = [];

function switch_top10(period) {
	if (typeof(jQuery)!=='function')
		return;
	if (period!=='week')
		period='day';
	if (last_top10_period_requested == period)
		return;
	last_top10_period_requested = period;
	if (block_top10_cached[period]) {
		switch_top10_display(period);
		return;
	}
	show_loadingbox();
	jQuery.ajax({
		type: "GET",
		url: "/ajax/get_top10/?period="+period,
		success: function(response) {
			hide_loadingbox();
			block_top10_cached[period] = response;
			switch_top10_display(period);
		}
	});
}

function switch_top10_display(period) {
	if (typeof(jQuery)!=='function')
		return;
	jQuery('#bl_top').html(block_top10_cached[period]);
	if (period=='week') {
		jQuery('#topswitchday').html("<a href=\"javascript:void(0);\" onclick=\"switch_top10('day'); return false;\">Today</a>");
		jQuery('#topswitchweek').html("Past 7 days");
	} else {
		jQuery('#topswitchday').html("Today");
		jQuery('#topswitchweek').html("<a href=\"javascript:void(0);\" onclick=\"switch_top10('week'); return false;\">Past 7 days</a>");
	}
}

/* ### last-news block ### */

var last_lastnews_type_requested = 'news';
var block_lastnews_cached = [];

function switch_lastnews(type) {
	if (typeof(jQuery)!=='function')
		return;
	if (type!=='articles')
		type='news';
	if (last_lastnews_type_requested == type)
		return;
	last_lastnews_type_requested = type;
	if (block_lastnews_cached[type]) {
		switch_lastnews_display(type);
		return;
	}
	show_loadingbox();
	jQuery.ajax({
		type: "GET",
		url: "/ajax/get_lastnews/?type="+type,
		success: function(response) {
			hide_loadingbox();
			block_lastnews_cached[type] = response;
			switch_lastnews_display(type);
		}
	});
}

function switch_lastnews_display(type) {
	if (typeof(jQuery)!=='function')
		return;
	jQuery('#bl_lastnews').html(block_lastnews_cached[type]);
	if (type=='articles') {
		jQuery('#lastnewsswitchnews').html("<a href=\"javascript:void(0);\" onclick=\"switch_lastnews('news'); return false;\">Latest news</a>");
		jQuery('#lastnewsswitcharticles').html("Articles");
	} else {
		jQuery('#lastnewsswitchnews').html("Latest news");
		jQuery('#lastnewsswitcharticles').html("<a href=\"javascript:void(0);\" onclick=\"switch_lastnews('articles'); return false;\">Articles</a>");
	}
}
