var contentUpdater = null;

function StartContentUpdate()
{
	//trace('StartContentUpdate');
	
	contentUpdater = new PeriodicalExecuter(function(pe){ GetUpdateContent(); }, contentUpdaterInterval);
}

function StopContentUpdate()
{
	//trace('StopContentUpdate');
	
	if(contentUpdater!=null)
	{
		contentUpdater.stop();
		contentUpdater = null;
	}
}

function GetUpdateContent()
{
	//trace('GetUpdateContent');
		
	new Ajax.Request(urlAjax, {
		method: 'post',
		onSuccess: function(transport){ GotUpdateContent(transport); },
		onFailure: null,
		parameters: {action_ajax: 'obtenirNouveauContenu', sequence: Object.toJSON(currentContent) }
	});
}

function GotUpdateContent(p_reponse)
{
	//trace('GotUpdateContent()');
			
	var statusRequete = p_reponse.getResponseHeader("X-status");
	
	if(statusRequete=='OK')
	{
		var nouveauContenu = p_reponse.getResponseHeader("X-contenu");
		
		trace('nouveauContenu = ' + nouveauContenu);
		
		currentContent.push(nouveauContenu);
		
		$('banner').fade({duration: 0.2, afterFinish: function(){
			$('banner').update(p_reponse.responseText);
			$('banner').appear({duration: 0.2});
		}});
				
		CheckUserAction();
	}	
}
		
function ShowMore()
{
	//trace('ShowMore()');
	
	$('more').update($('more').rel);
	$('more').onclick = function(){ return true; };

	Effect.BlindDown('yellow',{duration: 0.5});

	new Ajax.Request(urlAjax, {
		method: 'post',
		onSuccess: null,
		onFailure: null,
		parameters: {action_ajax: 'marquerPanneauOuvert'}
	});

	return false;
}

function CheckUserAction()
{
	//trace('CheckUserAction()');
	
	if($('more')!=null)
	{
		Event.observe('more', 'mouseover', function(){ StopContentUpdate(); });
		Event.observe('more', 'click', function(){ StopContentUpdate(); });
		Event.observe('more', 'touchstart', function(){ StopContentUpdate(); });
	}
}

document.observe('dom:loaded', function()
{
	//trace('DOM Loaded');
	
	if(pannelOpenedAtStart && $('yellow')!=null)
	{
		$('yellow').show();
		
		if($('more')!=null)
		{
			if($('more').rel!='')
			{
				$('more').update($('more').rel);
			}
			$('more').onclick = function(){ return true; };
		}
		
		contentUpdate = false;
	}
	
	if(contentUpdate)
	{
		CheckUserAction();
	
		StartContentUpdate();
	}
});
