/*
	Home - Blog / Forum via AJAX
	hamu278 @ TECHNETCAL
*/

$(function() {
	$('div#newsbox .more').prepend('<span id="newsbox_forum" onclick="changetab(\'forum\');">' + NEWSBOX_FORUM + '</span>');
});

function changetab(element,reload) {

	// Tab appearance
	$('div#newsbox ul.newsbox_menu li').attr("class", "newsbox_unselected");
	$('div#newsbox ul.newsbox_menu li#newsbox_'+element).removeAttr("class");

	// Hide all element
	$('div#newsbox div.full_box_content').hide();

	if (element == 'news')
	{
		$('div#newsbox div#full_box_news').show();
		$('div#newsbox h3').html(NEWSBOX_NEWS);
		$('div#newsbox .more').html('<span id="newsbox_forum" onclick="changetab(\'forum\');">' + NEWSBOX_FORUM + '</span> <a href="home/news.html">More...</a>');
	}
	else
	{
		$('div#newsbox h3').html(NEWSBOX_FORUM);
		$('div#newsbox .more').html('<span id="newsbox_news" onclick="changetab(\'news\');">' + NEWSBOX_NEWS + '</span> <a href="http://forum.technetcal.com/">More...</a>');

		if ($('div#newsbox div#full_box_'+element).html() == null)
		{

			if (element == 'forum')
			{
			$('div#newsbox div.newsbox_main').append('<div class="full_box_content" id="full_box_forum"></div>');
			ajaxml(element);
			}

		}
		else
		{
			$('div#newsbox div#full_box_'+element).show();
		}
	}
}

function ajaxml(type) {

	$.ajax({
		type: "GET",
		url:"buffer2.php",
		data: {id:type},
		beforeSend:function()  {
			$('div#newsbox div#full_box_'+type).html('<div class="loading"><img src="images/commons/ajax-loader.gif" alt="Loading" /></div>');
		},
		complete:function() {
			$('div#newsbox div#full_box_'+type+' div.loading').remove();
		},
		success:function(xml){
			$('div#newsbox div#full_box_'+type).html(xml);
		},
		error:function() {
			$('div#newsbox div#full_box_'+type).html('<div class="error">無法讀取相關數據。請<a href="javascript:ajaxml(\''+type+'\');">按此</a>重試。</div>');
		}
	});

	setTimeout('reloadajaxml(\''+type+'\');', 60000);

}

function reloadajaxml(type) {
	
	$('div#newsbox div#full_box_'+type+' ul').remove();
	ajaxml(type);

}