function updateStatus()
{
	msg = $('status-text').value;
	
	$('status-text').setAttribute('style', 'background-image:url(/img/status-loading.gif);color:rgba(0, 0, 0, 0.3);');
	$('status-text').value = 'waiting for this to finish...';
	
	$('status-text').setAttribute('disabled', 'disabled');
	$('status-submit').setAttribute('disabled', 'disabled');
	
	Complete = function()
	{
		response = this.transport.responseText;
		
		if (response.match(/^err:/, response))
		{
			Reset(response.replace(/^err:/, ''));
			return false;
		}
		else
		{
			$('status-text').value = msg;
		
			$('status-text').setAttribute('style', 'width:635px');
			$('status-submit').setAttribute('style', 'display:none');
			
			$('activity-normal').style.display = 'none';
			$('activity-status').style.display = null;
		}
	}
	
	Reset = function(error)
	{
		$('status-text').value = 'reading an error, raging...';
	
		toggleOverlay(1);
		alert('Error: ' + error);
		toggleOverlay(0);
	
		$('status-submit').removeAttribute('style');
		$('status-text').removeAttribute('style');
		
		$('status-text').value = msg;
		
		$('status-text').removeAttribute('disabled');
		$('status-submit').removeAttribute('disabled');
	}
	
	if (msg.length <= 3 || msg.match(/^( +)?$/))
	{
		Reset('message is too short.');
		return false;
	}
	
	new Ajax('/updateStatus.php', {method: 'post', postBody: 'msg=' + encodeURIComponent(msg), onComplete: Complete}).request();
}

var currentStatusTab = 'normal';
function switchStatusTab(to)
{
	$('activity-' + currentStatusTab).style.display = 'none';
	$('activity-' + to).style.display = 'block';	
	
	$('activity-link-' + to).className = 'activity-link-active';
	$('activity-link-' + currentStatusTab).className = '';
	
	currentStatusTab = to;
}

// -----------------------------
//   		  Boxes
// -----------------------------

function hideBox(box)
{
	if (box == "init")
	{	
		whatsnew_box =		parseInt(getCookie('whatsnew_box'));
		mygames_box =		parseInt(getCookie('mygames_box'));
		usersonline_box =	parseInt(getCookie('usersonline_box'));
		
		if (isNaN(whatsnew_box))		whatsnew_box = 1;
		if (isNaN(mygames_box))			mygames_box = 1;
		if (isNaN(usersonline_box))		usersonline_box = 1;
		
		if (!whatsnew_box)
		{
			document.getElementById("whatsnew-box").style.display = "none";
			document.getElementById("whatsnew-box-minus").src = "img/plus.jpg";
		}
		
		if (!mygames_box)
		{
			document.getElementById("mygames-box").style.display = "none";
			document.getElementById("mygames-box-minus").src = "img/plus.jpg";
		}
		
		if (!usersonline_box)
		{
			document.getElementById("usersonline-box").style.display = "none";
			document.getElementById("usersonline-box-minus").src = "img/plus.jpg";
		}
	}
	else
	{
		box_str = box;
		box_minus = document.getElementById(box + "-box-minus");
		box = document.getElementById(box + "-box");
		
		if (box.style.display == "block")
		{		
			document.cookie = box_str + '_box=0';
		
			box.style.display = "none";
			box_minus.src = "img/plus.jpg";
		}
		else
		{
			document.cookie = box_str + '_box=1';
		
			box.style.display = "block";
			box_minus.src = "img/minus.jpg";
		}
	}
}

// -----------------------------
// 		  Feature images
// -----------------------------

var featureTimeout;
var featureI = 2;
var featureIOld = 1;

function featureChange(start)
{
	if (start > 0)
	{
		featureIOld = start;
		featureI = start + 1;
		if (featureI == 4) featureI = 1;
	}

	if (featureI != featureIOld)
	{
		element = 'feature-' + featureI;
		previous = 'feature-' + featureIOld;
		
		switcher = 'switcher-' + featureI;
		switcher_pr = 'switcher-' + featureIOld;

		$(switcher).setAttribute("class", "active");
		$(switcher).setAttribute("className", "active");
		
		$(switcher_pr).removeAttribute("class");
		$(switcher_pr).removeAttribute("className");

		new Fx.Style(element, 'opacity', {duration: 1000}).start(0,1);
		new Fx.Style(previous, 'opacity', {duration: 1000}).start(1,0);

		featureIOld = featureI;
	}
	
	featureI++;
	if (featureI == 4) featureI = 1;
	
	featureTimeout = setTimeout("featureChange();", 10000);
}

// -----------------------------
//     Pointless ajax stuff
// -----------------------------

var canUpdateOnline		= true;
var pendingOnlineUpdate	= false;
var updateOnlineTO		= null;

function updateOnline()
{
	var Complete = function()
	{
		updateOnlineTO = setTimeout("updateOnline()", 30000);
	};

	if (canUpdateOnline)
		new Ajax('/userson_ajax.php', {method: 'get', update: $('userson_ajax'), onComplete: Complete}).request();
	else
		pendingOnlineUpdate = true;
}

window.onfocus = function()
{
	canUpdateOnline = true;
	
	if (pendingOnlineUpdate)
	{
		clearTimeout(updateOnlineTO);
		updateOnline();
	}
};

window.onblur = function() { canUpdateOnline = false; };

function moreNews()
{
	$('more_news3').innerHTML = 'Loading&hellip;';
	$('more_news3').removeAttribute('href');

	var Complete = function()
	{
		$('more_news3').style.display = 'none';
		$('more_news2').removeAttribute('style');
	}
	
	new Ajax('/morenews.php', {method: 'get', update: $('more_news'), onComplete: Complete}).request();
}
