function ConfirmMsg(msg) { 
  document.returnValue = confirm(msg);
}
function RemoveDialogue(elm) {
	var slideanim = new Fx.Morph(elm);
	slideanim.start({
	    'height': 0, //Morphs the height from the current to 100px.
	    'opacity': 0   //Morphs the opacity from the current to 300px.
	});
}
function InitQueue(refreshdelay) {
	
	if ( $('mailqueue') !== null )
	{
		LoadQueue('null',refreshdelay);
		if ($('mailqueuecontrols')) {
			$('mailqueuecontrols').innerHTML = '<p><a href="#" class="button play" target="_blank" onclick="LoadQueue(\'start\','+refreshdelay+');return false;"><span>Start email queue</span></a>&nbsp;<a href="queue.php?clearqueue" class="button trash" onclick="ConfirmMsg(\'Are you sure you wish to clear the entire email queue?\\n\\nThis can NOT be undone.\');return document.returnValue"><span>Clear email queue</span></a></p>';
		}
	}
}
var task = ''; var status = '';
function LoadQueue(action,refreshdelay) {
	action = typeof(action) != 'undefined' ? action : 'null';
	refreshdelay = typeof(refreshdelay) != 'undefined' ? refreshdelay : 10000;
	dummy = $time() + $random(0, 100);
	//alert('the action is ' + action + ' and the task is ' + task);
	if (action == 'start') {
		$('mailqueue').removeClass('stopped');
		$('mailqueue').addClass('running');
		$('mailqueuecontrols').innerHTML = '<p><a href="#" class="button stop" target="_blank" onclick="LoadQueue(\'stop\');return false;"><span>Stop email queue</span></a>&nbsp;<a href="queue.php?clearqueue" class="button trash" onclick="ConfirmMsg(\'Are you sure you wish to clear the entire email queue?\\n\\nThis can NOT be undone.\');return document.returnValue"><span>Clear email queue</span></a></p>';
		status = 'starting';
		LoadQueue('nextbatch',refreshdelay);
	} else if (action == 'stop') {
		$clear(task);
		status = 'stopped';
		$('mailqueue').removeClass('running');
		$('mailqueue').addClass('stopped');
		$('mailqueuecontrols').innerHTML = '<p><a href="#" class="button play" target="_blank" onclick="LoadQueue(\'start\','+refreshdelay+');return false;"><span>Start email queue</span></a>&nbsp;<a href="queue.php?clearqueue" class="button trash" onclick="ConfirmMsg(\'Are you sure you wish to clear the entire email queue?\\n\\nThis can NOT be undone.\');return document.returnValue"><span>Clear email queue</span></a></p>';
		LoadQueue();
	} else { // starting, nextbatch or null
		if ($('mailqueuestatus')) {
			if (status == 'starting') { 
				$('mailqueuestatus').innerHTML = 'Starting mail queue...'; 
			} else if (status == 'stopped') { 
				$('mailqueuestatus').innerHTML = 'Stopping mail queue...'; 
			} else { 
				$('mailqueuestatus').innerHTML = 'Loading...'; 
			}
			$('mailqueuestatus').addClass('spinner');
		}
		var req = new Request.HTML({url:'queue_process.php?'+dummy, update:'mailqueue',
			onComplete: function() {
				$('mailqueue').innerHTML = $('mailqueue').innerHTML;
				if ($('mailqueue').innerHTML.match('empty') && action != 'stop' && action != 'null') {
					LoadQueue('stop');
					return false;
				}
				// IE6 and IE7 are stupid.
				if (action == 'starting' || action == 'nextbatch'){
					//alert('starting a new delay because the action is ' + action);
					task = function() { LoadQueue('nextbatch',refreshdelay); }.delay(refreshdelay);
				}
			},
			onFailure: function() {
				$('mailqueue').set('text', 'The request failed.');
			}
		}).get({'action': action}); 		
	}
}
