function updateShouts(){
	var url = '/getlatestshouts/'+$('#shout_maxid').val();
	clearTimeout(updateShoutsTimer);
	$.ajax({
		type: "GET",
		dataType: "xml",
		url: url,
		cache: false,
		success: function(response){
			var maxid = response.getElementsByTagName('maxid').item(0).firstChild.data;
			var old_maxid = $('#shout_maxid').val();
	
			$('#shout_maxid').val(maxid);
	
			if (maxid > old_maxid){
				var random_id = Math.random() * 100000000000;
				random_id = Math.floor(random_id);
		
				document.getElementById('shout-area').innerHTML = '<div id="s'+random_id+'" style="display:none;">' + response.getElementsByTagName('shoutentries').item(0).firstChild.data + '</div>' + document.getElementById('shout-area').innerHTML;
				$('#s'+random_id).fadeIn('slow');
			}
		},
		complete: function(){
			$('#shout_updating').val('0');
			updateShoutsTimer = setTimeout(updateShouts, updateShoutsPeriod);
		}
	});
}

function submitComplete(){
	$('#shout_content').val(''); 
	$('#shout_content').removeAttr('disabled');
	$('#shoutSubmit').removeAttr('disabled');
	
	$('#shout_updating').val('1');
	updateShouts();
/*	
	new Ajax.Request('/getlatestshouts/'+$('#shout_maxid')+'', {onLoading:function(request){$('#shout_updating').value='1';}, onComplete:function(request){updateShouts(request); $('#shout_updating').value='0';}, asynchronous:true, evalScripts:true});
*/
}

function toggleHistory(){
		
	if (!$('#shoutBox_history').is(":visible")){
		$('#shoutBox_history').html('<div id="shoutBox_loading"><img src="/img/loading-shouthistory.gif" alt="Loading" /><br />Loading...</div>');
		setTimeout('loadHistory()', 800);
	}
	
	$('#shoutBox_history').slideToggle('slow');
}

function loadHistory(){
	$.ajax({
		type: "GET",
		dataType: "html",
		url: '/shouts/history',
		cache: false,
		success: function(msg){
				$('#shoutBox_history').html(msg);
		}
	});
		
	return false;
}

function historyLoading(){
	$('#shoutHistoryNavi-b').innerHTML = '<img src="/img/loading-shouthistory.gif" alt="loading" />';
	$('#shoutHistoryNavi').innerHTML = '<img src="/img/loading-shouthistory.gif" alt="loading" />';
}

// **********************************
// *****      Standalone        *****
// **********************************

function updateShoutsStandalone(submitcomplete){
	var url = '/getlatestshouts/'+$('#shout_maxid').val();
	clearTimeout(updateShoutTimer);
	$.ajax({
		type: "GET",
		dataType: "xml",
		url: url,
		cache: false,
		success: function(response){
			var maxid = response.getElementsByTagName('maxid').item(0).firstChild.data;
			var old_maxid = $('#shout_maxid').val();

			$('#shout_maxid').val(maxid);
			if (maxid > old_maxid){
				var random_id = Math.random() * 100000000000;
				random_id = Math.floor(random_id);

				if (submitcomplete != true){
					$('#shout_newmsg').val(parseInt($('#shout_newmsg').val()) + (maxid - old_maxid));
					updatetitle();
				}
				else{
					restoretitle();
				}

				document.getElementById('shout-area').innerHTML = '<div id="s'+random_id+'" style="display: none;">' + response.getElementsByTagName('shoutentries').item(0).firstChild.data + '</div>' + document.getElementById('shout-area').innerHTML;

				// TODO: jQuery: Add in highlight later		
				$('#s'+random_id).slideDown('slow');
			}
		},
		complete: function(){
			$('#shout_updating').val('0');
			updateShoutTimer = setTimeout(updateShoutsStandalone, updateStandaloneShoutPeriod);
		}
	});
}

function submitCompleteStandalone(){
	$('#shout_content').val(''); 
	$('#shout_content').removeAttr('disabled'); 
	$('#shoutSubmit').removeAttr('disabled');
	
	$('#shout_updating').val('1');
	updateShoutsStandalone(true);
}

function updatetitle(){
	document.title = '[' + $('#shout_newmsg').val() + '] Ping.sg/shouts';
}

function restoretitle(){
	$('#shout_newmsg').val(0);
	document.title = 'Ping.sg/shouts';
}
