/***************************************/
/*   GESTIONNAIRE D'AUTO SCROLLER      */
/*                                                                           */
/*   by MaX3315                                                  */
/*   modified by momo321                                   */
/*   http://codessources.votre-web.com            */
/*                                                                           */
/***************************************/

var globalListNewsScroller=Array(); //LISTE OU S'INCRIVE LES COMPOSANTS

var default_mouseOverColor='#99CC99';
var default_hightlightNewsWithoutLinkToo=true;
var default_outGoingStep=-1;
var default_outGoingSpeed=40;
var default_pauseDuration=1000;

var default_inCommingStep=-2;
var default_inCommingSpeed=20;
var default_readPauseDuration=3000;
var default_forceAMinHeightToContainerHeight=false;


/***********************************/
/**          NEWS OBJET                          **/
/***********************************/
function News(_container,_html,_url,_target)
{
	if(typeof(_url)=='undefined') { _url=null; }
	if(typeof(_html)=='undefined') { _html=null; }
	if(typeof(_target)=='undefined') { _target='_self'; }
	//propriete via constructeur
	this.container=_container;  // OBLIGATOIRE
	this.indexInList=0;
	this.html=_html;
	this.url=_url;
	this.target=_target;
	this.div=null;
	this.id=uniqueIDInDOM();
	//autres proprietes
	this.h=0;
	//methode de l'objet news
	this.init=news_init;
}

function news_init()
{
	// Il est possible de des valeurs soit éronnées à ce niveau car la page est en train de se 'fabriquer'
	// Le this.hieght par exemple est faux sur IE à cet endroit du programme
	
	this.div=document.createElement('div');
	this.div.innerHTML=this.html;

	//ajout du div contenant le message de la news
	this.container.obj.appendChild(this.div);

	this.h=this.div.offsetHeight;

	if(this.container.forceAMinHeightToContainerHeight && this.h<this.container.height)	//on retaille chaque news pour qu'elle soit au moins aussi haute que le conteneur.
	{
		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);
}

/***********************************/
/** Methode globales de recherche **/
/** d'un container                **/
/***********************************/
function globalSearchAContainer(id)
{
	var resu=null;
	//on liste tous les news container enregistre dans la liste
	for(var i=0 ; i<globalListNewsScroller.length ; i++)
	{
		if(''+globalListNewsScroller[i].id==''+id) { return i; }
	}
	return resu;
}

/***********************************/
/**       CONTAINER OBJET         **/
/***********************************/
function Container(idNode)
{
	//initialisation
	// Il est possible de des valeurs soit éronnées à ce niveau car la page est en train de se 'fabriquer'
	// Le this.hieght par exemple est faux sur IE à cet endroit du programme
	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=default_mouseOverColor;
	this.hightlightNewsWithoutLinkToo=default_hightlightNewsWithoutLinkToo;
	this.outGoingStep=default_outGoingStep;
	this.outGoingSpeed=default_outGoingSpeed;
	this.pauseDuration=default_pauseDuration;
	this.inCommingStep=default_inCommingStep;
	this.inCommingSpeed=default_inCommingSpeed;
	this.readPauseDuration=default_readPauseDuration;
	this.forceAMinHeightToContainerHeight=default_forceAMinHeightToContainerHeight;

	
	//methodes
	this.addNews=addNews;
	this.initAllNews=initAllNews;
	this.launch=launch;
	this.currentGoOut=currentGoOut;
	this.currentComming=currentComming;
	this.loadFromUl=loadFromUl;
	//les listener
	this.overContainer=overContainer;
	this.outContainer=outContainer;
	this.openMore=openMore;
	
	//enregistrement de l'objet dans la liste global
	this.indexInGlobalContainerList=globalListNewsScroller.length;
	globalListNewsScroller.push(this);
	
	//on ajoute les listener (pause au survole et click)
	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;
}


//methode chargeant et creant a la vole les objet news depuis une structure du DOM au format decrit ci-dessous
  /*
    structure que l'on attends
    <ul id="newsToLoad">  <!--  <= c'est ce noeud que l'on passe en argument-->
      <li> <!-- news 1-->
        <ul>
          <li>CONTENU HTML</li>
          <li><a href="htp://more_about" target="_self">LIEN</a></li>
        </ul>
      </li>
      <li> <!--news 2-->
        <ul>
          <li>CONTENU HTML</li>
          <li></li>  <!-- dans ce cas il n'y a pas de lien-->
        </ul>
      </li>
	  <!--
	  etc...
	  ...
	  -->
    </ul>
  */

function 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 News(null,content,url,target));
  }
  // lancement du defillement
  this.launch();
}

//Handler pour la gestion du survol avec la sourie permettant la pause de l'animation
function 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=globalSearchAContainer(nodeDOM.id);
	while(myContainerObjIndex==null)
	{
		nodeDOM=nodeDOM.parentNode;
		myContainerObjIndex=globalSearchAContainer(nodeDOM.id);
	}

	if(globalListNewsScroller[myContainerObjIndex].currentUrl!=null && globalListNewsScroller[myContainerObjIndex].currentUrl!='' || globalListNewsScroller[myContainerObjIndex].hightlightNewsWithoutLinkToo)
	{
		globalListNewsScroller[myContainerObjIndex].obj.style.backgroundColor=globalListNewsScroller[myContainerObjIndex].mouseOverColor;
	}
	if(globalListNewsScroller[myContainerObjIndex].currentUrl!=null && globalListNewsScroller[myContainerObjIndex].currentUrl!='')
	{ if(globalListNewsScroller[myContainerObjIndex].obj) { globalListNewsScroller[myContainerObjIndex].obj.style.cursor='pointer'; }
	} else { if(globalListNewsScroller[myContainerObjIndex].obj) { globalListNewsScroller[myContainerObjIndex].obj.style.cursor='default'; } }
	
	globalListNewsScroller[myContainerObjIndex].inPause=true;
}

//handler gerant la sortie de la sourie, permettant le redemarrage du defilement
function 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=globalSearchAContainer(nodeDOM.id);
	while(myContainerObjIndex==null) //on recherche le container de news
	{
		nodeDOM=nodeDOM.parentNode;
		myContainerObjIndex=globalSearchAContainer(nodeDOM.id);
	}
	
	if(globalListNewsScroller[myContainerObjIndex].currentUrl!=null && globalListNewsScroller[myContainerObjIndex].currentUrl!='' || globalListNewsScroller[myContainerObjIndex].hightlightNewsWithoutLinkToo)
	{
		globalListNewsScroller[myContainerObjIndex].obj.style.backgroundColor='';
	}
	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 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=globalSearchAContainer(nodeDOM.id);
	while(myContainerObjIndex==null)
	{
		nodeDOM=nodeDOM.parentNode;
		myContainerObjIndex=globalSearchAContainer(nodeDOM.id);
	}
	if(globalListNewsScroller[myContainerObjIndex].currentUrl!=null && globalListNewsScroller[myContainerObjIndex].currentUrl!='')
	{
		window.open(globalListNewsScroller[myContainerObjIndex].currentUrl,globalListNewsScroller[myContainerObjIndex].currentTarget,'');
	}
}

//lie une nouvelle instance d'un objet News a cette instance de container
function addNews(aNews)
{
	aNews.container=this;
	aNews.indexInList=this.listNews.length;
	this.listNews.push(aNews);
	return;
}

//fonction d'initialisation du container
// elle appel principallement la fonction d'initialisation de news sur chaque news
function 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 appeler manuellement dans le cadre d'une chargement manuelle des news via addNews
function 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);
	// TimeOut avant de sortir la news pour passer à la suivante
	setTimeout('globalListNewsScroller['+this.indexInGlobalContainerList+'].currentGoOut()',this.readPauseDuration);
}


//animation de sortie d'une news-------------------------------------------------------------------------------------
function currentGoOut()
{
	// Si le bloc est en cours de survol, on attend avant de réessayer le GoOut
	if(this.inPause)
	{
		setTimeout('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;
		//Sortir de 'outGoingStep' la news
		this.listNews[this.currentIndex].div.style.top=(tmpTop+this.outGoingStep)+'px';
		
		if(divHeight>-tmpTop)
		{
			//Si la news n'est pas encore entièrement sorti -> appel récursif
			//alert("mise en pause: "+this.outGoingSpeed);
			setTimeout('globalListNewsScroller['+this.indexInGlobalContainerList+'].currentGoOut()',this.outGoingSpeed);
		}
		else
		{
			this.listNews[this.currentIndex].div.style.display='none';
			
			this.obj.removeChild(this.listNews[this.currentIndex].div);
			
			//passage à la news suivante
			this.currentIndex=(this.currentIndex+1)%this.listNews.length;
			this.listNews[this.currentIndex].div.style.display='block';
			
			//Correction le 05/01/2007 par MOMO321
			//la ligne suivante est remplacé par celle qui suit, l'information this.height etaient en effet erronée car calculée trop tôt
			//this.listNews[this.currentIndex].div.style.top=this.height+'px';
			this.listNews[this.currentIndex].div.style.top=this.obj.offsetHeight+'px';
			
			this.obj.appendChild(this.listNews[this.currentIndex].div);
	
			globalListNewsScroller[this.indexInGlobalContainerList].currentComming();
		}
	}
}

//animation d'entrée d'une news-------------------------------------------------------------------------------------------
function currentComming()
{
	// Si le bloc est en cours de survol, on attend avant de réessayer le GoOut
	if(this.inPause)
	{
		setTimeout('globalListNewsScroller['+this.indexInGlobalContainerList+'].currentComming()',this.pauseDuration);
	}
	else
	{
		// lecture des informations de la news
		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'; }
		
		//monter de 'inComingStep' la news
		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)
		{
			//Si il reste de la place dans le bloc appel recursif pour monter encore
			setTimeout('globalListNewsScroller['+this.indexInGlobalContainerList+'].currentComming()',this.inCommingSpeed);
		}
		else
		{
			//on passe a l'animation de sortie apres une pause de this.readPauseDuration milisecondes
			setTimeout('globalListNewsScroller['+this.indexInGlobalContainerList+'].currentGoOut()',this.readPauseDuration);
		}
	}
}

