// Global JS variable declarations for shoutbox
var shoutboxRefreshInterval = 10000;
var shoutboxEditing = false;
var shoutboxLastUpdated = 0;
var shoutboxPage = 1;


// Initialisation function for shoutbox
function shoutboxInit()
{
	shoutboxUpdate();
	setTimeout( 'shoutboxTimer()', shoutboxRefreshInterval );
}


// Timer function for shoutbox
function shoutboxTimer()
{
	if ( !shoutboxEditing )
		shoutboxUpdateCheck();
	setTimeout( 'shoutboxTimer()', shoutboxRefreshInterval );
}


// Update check function for shoutbox
function shoutboxUpdateCheck()
{
	// Get current localtime
	var clienttime = new Date();
	clienttime = clienttime.getTime();

	ajaxOpen ( "shoutbox.php?act=updatecheck&clienttime="+clienttime+"&timestamp="+shoutboxLastUpdated, shoutboxUpdateCheckCallback );
}


// Update check function callback for shoutbox
function shoutboxUpdateCheckCallback ( content )
{
	if ( content == "1" )
		shoutboxUpdate();
}


// Update function for shoutbox
function shoutboxUpdate()
{
	ajaxOpen ( "shoutbox.php?page="+shoutboxPage, shoutboxUpdateCallback );
}


// Update function callback for shoutbox
function shoutboxUpdateCallback( content )
{
	document.getElementById ( "shoutboxContent" ).innerHTML = content;

	 // Set last updated time
	var timestamp = new Date();
	shoutboxLastUpdated = timestamp.getTime();
}


// Jump to the next page of the shoutbox
function shoutboxSetPage ( page )
{
	shoutboxPage = page;
	shoutboxUpdate();
}


// Checks the validity of a shout, and submits it if it is valid
function shoutboxShout()
{
	// Get a copy of the content of the shoutbox shout field with whitespace trimmed
	var shout = document.getElementById('shoutForm').shout.value.replace(/^\s+|\s+$/g,"");

	// Check for shout length too short / long
	if ( shout.length <= 0 )
	{
		alert ( 'You cannot submit an empty shout!' );
		return;
	}
	else if ( shout.length > 220 )
	{
		alert ( 'Your shout cannot be more than 220 characters in length, currently it is '+shout.length+' characters long' );
		return;
	}


	// Get a copy of the content of the shoutbox username field with whitespace trimmed
	if ( document.getElementById('shoutForm').username != null )
	{
		var username = document.getElementById('shoutForm').username.value.replace(/^\s+|\s+$/g,"");

		// Check for username length too short / long
		if ( username.length <= 0 )
		{
			alert ( 'You must provide a name when shouting as a guest.' );
			return;
		}
		else if ( username.length > 25 )
		{
			alert ( 'Your gust name cannot be more than 25 characters in length, currently it is '+username.length+' characters long' );
			return;
		}
	}

	// If we didn't already return then the shout (and username) are OK, so continue
	ajaxSubmitPOST('shoutForm','shoutbox.php?act=submitshout', shoutboxActionCompletedCallback );
}




// Start editing a shout
function shoutboxEditStart ( shoutId )
{
	shoutboxEditing = true;
	ajaxOpen ( "shoutbox.php?act=editshout&shoutId="+shoutId, shoutboxEditStartCallback, shoutId );
}


// Callback for shoutboxEditStart
function shoutboxEditStartCallback ( editForm, shoutId )
{
	document.getElementById ( "shout_" + shoutId ).innerHTML = editForm;
}


// Finish editing a shout
function shoutboxEditFinish ( shoutId )
{
	// Get a copy of the content of the shoutbox shout field with whitespace trimmed
	var shout = document.getElementById("shoutEditForm_"+shoutId).shout.value.replace(/^\s+|\s+$/g,"");

	// Check for shout length too short / long
	if ( shout.length <= 0 )
	{
		alert ( 'You cannot submit an empty shout!' );
		return;
	}
	else if ( shout.length > 220 )
	{
		alert ( 'Your shout cannot be more than 220 characters in length, currently it is '+shout.length+' characters long' );
		return;
	}


	// If we didn't already return then the shout is OK, so continue
	ajaxSubmitPOST( 'shoutEditForm_'+shoutId,'shoutbox.php?act=submitshoutedit', shoutboxActionCompletedCallback );
	document.getElementById ( "shout_" + shoutId ).innerHTML = "Editing...";
	shoutboxEditing = false;
}


// Cancel editing a shout
function shoutboxEditCancel()
{
	shoutboxEditing = false;
}




// Delete a shout
function shoutboxDelete ( shoutId )
{
	ajaxOpen( 'shoutbox.php?act=deleteshout&shout='+shoutId, shoutboxActionCompletedCallback );
}




// Callback for when an action has been completed
function shoutboxActionCompletedCallback ( content )
{
	shoutboxUpdate();
}