var actu_globalListNewsScroller=Array();

var actu_default_mouseOverColor='#000000'; // COULEUR AU SURVOL
var actu_default_hightlightNewsWithoutLinkToo=true;
var actu_default_outGoingStep=-1;
var actu_default_outGoingSpeed=20;
var actu_default_pauseDuration=1000;

var actu_default_inCommingStep=-2;
var actu_default_inCommingSpeed=30;
var actu_default_readPauseDuration=3000;
var actu_default_forceAMinHeightToContainerHeight=false;


function actu_News(_container,_html,_url,_target)
{
	if(typeof(_url)=='undefined') { _url=null; }
	if(typeof(_html)=='undefined') { _html=null; }
	if(typeof(_target)=='undefined') { _target='_self'; }
	
	this.container=_container;  // OBLIGATOIRE
	this.indexInList=0;
	this.html=_html;
	this.url=_url;
	this.target=_target;
	this.div=null;
	this.id=uniqueIDInDOM();
	this.h=0;
	this.init=actu_news_init;
}

function actu_news_init()
{
	this.div=document.createElement('div');
	this.div.innerHTML=this.html;

	this.container.obj.appendChild(this.div);

	this.h=this.div.offsetHeight;

	if(this.container.forceAMinHeightToContainerHeight && this.h<this.container.height)	
	{
		this.h=this.container.height;
		this.div.style.height=this.h+'px';
	}
	
	this.div.style.position='relative';
	this.div.style.display='none';
	
	this.container.obj.removeChild(this.div);
}


function actu_globalSearchAContainer(id)
{
	var resu=null;
	for(var i=0 ; i<actu_globalListNewsScroller.length ; i++)
	{
		if(''+actu_globalListNewsScroller[i].id==''+id) { return i; }
	}
	return resu;
}

function actu_Container(idNode)
{
	this.id=idNode;
	
	this.obj=document.getElementById(this.id);
	this.width=this.obj.offsetWidth;//-(this.obj.style.paddingLeft.replace('px',''))-(this.obj.style.paddingRight.replace('px',''))
	this.height=this.obj.offsetHeight;//-(this.obj.style.paddingTop.replace('px',''))-(this.obj.style.paddingBottom.replace('px',''));
	//initialisation du style du container
	this.obj.style.overflow='hidden';
	
	//proprietes
	this.listNews=Array();
	this.currentIndex=0;
	this.inPause=false;
	this.initilised=false;
	
	this.currentUrl=null;
	this.currentTarget=null;
	
	//propriete graphique
	this.mouseOverColor=actu_default_mouseOverColor;
	this.hightlightNewsWithoutLinkToo=actu_default_hightlightNewsWithoutLinkToo;
	this.outGoingStep=actu_default_outGoingStep;
	this.outGoingSpeed=actu_default_outGoingSpeed;
	this.pauseDuration=actu_default_pauseDuration;
	this.inCommingStep=actu_default_inCommingStep;
	this.inCommingSpeed=actu_default_inCommingSpeed;
	this.readPauseDuration=actu_default_readPauseDuration;
	this.forceAMinHeightToContainerHeight=actu_default_forceAMinHeightToContainerHeight;

	
	//methodes
	this.addNews=actu_addNews;
	this.initAllNews=actu_initAllNews;
	this.launch=actu_launch;
	this.currentGoOut=actu_currentGoOut;
	this.currentComming=actu_currentComming;
	this.loadFromUl=actu_loadFromUl;
	//les listener
	this.overContainer=actu_overContainer;
	this.outContainer=actu_outContainer;
	this.openMore=actu_openMore;
	
	//enregistrement de l'objet dans la liste global
	this.indexInGlobalContainerList=actu_globalListNewsScroller.length;
	actu_globalListNewsScroller.push(this);
	
	//on ajoute les listener (pause au survol et clic)
	if(document.all) { this.obj.attachEvent('onmousemove',this.overContainer); }
	else { this.obj.addEventListener('mousemove',this.overContainer,false);}
	if(document.all) { this.obj.attachEvent('onmouseout',this.outContainer); }
	else { this.obj.addEventListener('mouseout',this.outContainer,false);}
	if(document.all) { this.obj.attachEvent('onclick',this.openMore); }
	else { this.obj.addEventListener('click',this.openMore,false);}
	
	
	return;
}


function actu_loadFromUl(ulNode)
{
  var tabNews=ulNode.getElementsByTagName('ul');
  for(var i=0 ; i<tabNews.length ; i++)
  {
	var tmpLi=tabNews[i].getElementsByTagName('li');
	
	var content=tmpLi[0].innerHTML;
	var url='';
	var target='_self';
	
	if(tmpLi.length>1)
	{
		var tmpA=tmpLi[1].getElementsByTagName('a');
		if(tmpA[0])
		{
			url=tmpA[0].href;
			if(tmpA[0].target)
			{
				target=tmpA[0].target;
			}
		}
	}
	this.addNews(new actu_News(null,content,url,target));
  }
  
  this.launch();
}

//Handler pour la gestion du survol avec la souris permettant la pause de l'animation
function actu_overContainer(aEvent)
{
  	var myEvent = aEvent ? aEvent : window.event; //recuperation de l'evenement selon le navigateur
	//on recupere cible de l'evenemet
	var nodeDOM= myEvent.target ? myEvent.target : myEvent.srcElement;
	
	var myContainerObjIndex=actu_globalSearchAContainer(nodeDOM.id);
	while(myContainerObjIndex==null)
	{
		nodeDOM=nodeDOM.parentNode;
		myContainerObjIndex=actu_globalSearchAContainer(nodeDOM.id);
	}

	if(actu_globalListNewsScroller[myContainerObjIndex].currentUrl!=null && actu_globalListNewsScroller[myContainerObjIndex].currentUrl!='' || actu_globalListNewsScroller[myContainerObjIndex].hightlightNewsWithoutLinkToo)
	{
		actu_globalListNewsScroller[myContainerObjIndex].obj.style.backgroundColor=actu_globalListNewsScroller[myContainerObjIndex].mouseOverColor;
	}
	if(actu_globalListNewsScroller[myContainerObjIndex].currentUrl!=null && actu_globalListNewsScroller[myContainerObjIndex].currentUrl!='')
	{ if(actu_globalListNewsScroller[myContainerObjIndex].obj) { actu_globalListNewsScroller[myContainerObjIndex].obj.style.cursor='pointer'; }
	} else { if(actu_globalListNewsScroller[myContainerObjIndex].obj) { actu_globalListNewsScroller[myContainerObjIndex].obj.style.cursor='default'; } }
	
	actu_globalListNewsScroller[myContainerObjIndex].inPause=true;
}

//handler gerant la sortie de la souris, permettant le redemarrage du defilement
function actu_outContainer(aEvent)
{
  	var myEvent = aEvent ? aEvent : window.event; //recuperation de l'evenement selon le navigateur
	//on recupere cible de l'evenemet
	var nodeDOM= myEvent.target ? myEvent.target : myEvent.srcElement;
	
	var myContainerObjIndex=actu_globalSearchAContainer(nodeDOM.id);
	while(myContainerObjIndex==null) //on recherche le container de news
	{
		nodeDOM=nodeDOM.parentNode;
		myContainerObjIndex=actu_globalSearchAContainer(nodeDOM.id);
	}
	
	if(actu_globalListNewsScroller[myContainerObjIndex].currentUrl!=null && actu_globalListNewsScroller[myContainerObjIndex].currentUrl!='' || actu_globalListNewsScroller[myContainerObjIndex].hightlightNewsWithoutLinkToo)
	{
		actu_globalListNewsScroller[myContainerObjIndex].obj.style.backgroundColor='';
	}
	actu_globalListNewsScroller[myContainerObjIndex].inPause=false;
}

//gestion du clic sur le container avec eventuelle ouverture du lien de la news courante (si elle en possede un)
function actu_openMore(aEvent)
{
  	var myEvent = aEvent ? aEvent : window.event; //recuperation de l'evenement selon le navigateur
	//on recupere cible de l'evenemet
	var nodeDOM= myEvent.target ? myEvent.target : myEvent.srcElement;
	
	var myContainerObjIndex=actu_globalSearchAContainer(nodeDOM.id);
	while(myContainerObjIndex==null)
	{
		nodeDOM=nodeDOM.parentNode;
		myContainerObjIndex=actu_globalSearchAContainer(nodeDOM.id);
	}
	if(actu_globalListNewsScroller[myContainerObjIndex].currentUrl!=null && actu_globalListNewsScroller[myContainerObjIndex].currentUrl!='')
	{
		window.open(actu_globalListNewsScroller[myContainerObjIndex].currentUrl,actu_globalListNewsScroller[myContainerObjIndex].currentTarget,'');
	}
}

//lie une nouvelle instance d'un objet News a cette instance de container
function actu_addNews(aNews)
{
	aNews.container=this;
	aNews.indexInList=this.listNews.length;
	this.listNews.push(aNews);
	return;
}

//fonction d'initialisation du container
//appelle principalement la fonction d'initialisation de news sur chaque news
function actu_initAllNews()
{
	this.obj.innerHTML='';
	
	//initilisation d'une news apres l'autre
	for(var i=0 ; i<this.listNews.length ; i++) { this.listNews[i].init(); }
}

//fonction de lancement de l'animation
// automatiquement appeler par loadFromUl
// Mais peut-être appelée manuellement dans le cadre d'une chargement manuelle des news via addNews
function actu_launch()
{
	if(!this.initilised) { this.initAllNews(); }
	
	this.listNews[this.currentIndex].div.style.top='0px';
	this.listNews[this.currentIndex].div.style.display='block';
	this.currentUrl=this.listNews[this.currentIndex].url;
	this.currentTarget=this.listNews[this.currentIndex].target;
	
	this.obj.appendChild(this.listNews[this.currentIndex].div);
	setTimeout('actu_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentGoOut()',this.readPauseDuration);
}


//animation de sortie d'une news
function actu_currentGoOut()
{
	if(this.inPause)
	{
		setTimeout('actu_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentGoOut()',this.pauseDuration);
	}
	else
	{
		var tmpTop=Math.round(this.listNews[this.currentIndex].div.style.top.replace('px',''));
		var divHeight=this.listNews[this.currentIndex].h;
		this.listNews[this.currentIndex].div.style.top=(tmpTop+this.outGoingStep)+'px';
		if(divHeight>-tmpTop)
		{
			setTimeout('actu_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentGoOut()',this.outGoingSpeed);
		}
		else
		{
			this.listNews[this.currentIndex].div.style.display='none';
			
			this.obj.removeChild(this.listNews[this.currentIndex].div);
			
			this.currentIndex=(this.currentIndex+1)%this.listNews.length;
			this.listNews[this.currentIndex].div.style.display='block';
			this.listNews[this.currentIndex].div.style.top=this.height+'px';
			
			this.obj.appendChild(this.listNews[this.currentIndex].div);
	
			setTimeout('actu_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentComming()',this.outGoingSpeed);
		}
	}
}

//animation d'entré d'une news
function actu_currentComming()
{
	if(this.inPause)
	{
		setTimeout('actu_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentComming()',this.pauseDuration);
	}
	else
	{
		this.currentUrl=this.listNews[this.currentIndex].url;
		this.currentTarget=this.listNews[this.currentIndex].target;
		
		if(this.currentUrl==null || this.currentUrl=='') { this.obj.style.cursor='default'; }
		else { this.obj.style.cursor='pointer'; }
		
		var tmpTop=Math.round(this.listNews[this.currentIndex].div.style.top.replace('px',''));
		this.listNews[this.currentIndex].div.style.top=(tmpTop+this.inCommingStep)+'px';
		
		if(tmpTop>0)
		{
			setTimeout('actu_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentComming()',this.inCommingSpeed);
		}
		else
		{
			//on passe a l'animation de sortie apres une pause de this.readPauseDuration milisecondes
			setTimeout('actu_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentGoOut()',this.readPauseDuration);
		}
	}
}
