var queueactive = null;
var queueinterval = 3000;

//Event.observe(window, 'load', function()
//{
//	Status();
//	$('queueControlButton').update(unescape(startButton));
//});

function Status(action)
{
	if (action == null) action = 'status';
	
	new Ajax.Request('/services/queue/' + action + '/', 
	{
		method:'get',
		onSuccess: function(transport)
		{
			var json = eval('(' + transport.responseText + ')');
			$('queueControlButton').update(unescape((json.active != null && json.active == true) ? stopButton : startButton));
			
			if (json)
			{
				if (json.success == true)
				{	
					if (queueinterval < 86400000)
					{
						var call = window.setTimeout('Status();', queueinterval);
						queueinterval = queueinterval * 2; // Decay.
					}

					if (json.startNew == true && json.token != null)
					{
						ResetInterval();
						$('list').replace(unescape(json.queue));
						window.location = 'http://' + behemothWarehouseDomainPrefix + '.' + behemothDomain + '/' + json.token + '/';
					}
				}
				else
				{
					// alert(json.message);
				}
			}
			else
			{
				// alert('Could not parse the returned JSON.');
			}
		}
	});
}

function ResetInterval()
{
	queueinterval = 2000;
	return queueinterval;
}
