// JavaScript Document
 /***************************************/
 /* GESTIONNAIRE D'AUTO SCROLLER */
 /* */
 /* by MaX3315 */
 /* http://codessources.votre-web.com */
 /* */
 /***************************************/

 var MAX3315_globalListNewsScroller=Array(); //LISTE OU S'INCRIVE LES COMPOSANTS

 var MAX3315_default_mouseOverColor=''; // Couleur d'arrière plan lorsqu'on met sa souris au dessus du contenu qui défile
 var MAX3315_default_hightlightNewsWithoutLinkToo=true;
 var MAX3315_default_outGoingStep=-1; // Le pas de défilement pour partir
 var MAX3315_default_outGoingSpeed=20; // La vitesse de défilement de l'info pour partir
 var MAX3315_default_pauseDuration=1000; // Le temps de pause lorsqu'on passe la souris au dessus de l'info et qu'on repars

 var MAX3315_default_inCommingStep=-2; // Le pas de défilement pour arriver
 var MAX3315_default_inCommingSpeed=10; // La vitesse de défilement de l'info pour arriver
 var MAX3315_default_readPauseDuration=3500; // La vitesse de pause pour lire l'info
 var MAX3315_default_forceAMinHeightToContainerHeight=false;


 //Il est fortement dÃ©conseillÃ© de mofidier le code Ã  partir de cette ligne

 /***********************************/
 /** NEWS OBJET **/
 /***********************************/
 function MAX3315_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=MAX3315_news_init;
 }

 function MAX3315_news_init()
 {
 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 MAX3315_globalSearchAContainer(id)
 {
 var resu=null;
 //on liste tous les news container enregistre dans la liste
 for(var i=0 ; i<MAX3315_globalListNewsScroller.length ; i++)
 {
 if(''+MAX3315_globalListNewsScroller[i].id==''+id) { return i; }
 }
 return resu;
 }

 /***********************************/
 /** CONTAINER OBJET **/
 /***********************************/
 function MAX3315_Container(idNode)
 {
 //initialisation
 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=MAX3315_default_mouseOverColor;
 this.hightlightNewsWithoutLinkToo=MAX3315_default_hightlightNewsWithoutLinkToo;
 this.outGoingStep=MAX3315_default_outGoingStep;
 this.outGoingSpeed=MAX3315_default_outGoingSpeed;
 this.pauseDuration=MAX3315_default_pauseDuration;
 this.inCommingStep=MAX3315_default_inCommingStep;
 this.inCommingSpeed=MAX3315_default_inCommingSpeed;
 this.readPauseDuration=MAX3315_default_readPauseDuration;
 this.forceAMinHeightToContainerHeight=MAX3315_default_forceAMinHeightToContainerHeight;


 //methodes
 this.addNews=MAX3315_addNews;
 this.initAllNews=MAX3315_initAllNews;
 this.launch=MAX3315_launch;
 this.currentGoOut=MAX3315_currentGoOut;
 this.currentComming=MAX3315_currentComming;
 this.loadFromUl=MAX3315_loadFromUl;
 //les listener
 this.overContainer=MAX3315_overContainer;
 this.outContainer=MAX3315_outContainer;
 this.openMore=MAX3315_openMore;

 //enregistrement de l'objet dans la liste global
 this.indexInGlobalContainerList=MAX3315_globalListNewsScroller.length;
 MAX3315_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 MAX3315_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 MAX3315_News(null,content,url,target));
 }

 this.launch();
 }

 //Handler pour la gestion du survol avec la sourie permettant la pause de l'animation
 function MAX3315_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=MAX3315_globalSearchAContainer(nodeDOM.id);
 while(myContainerObjIndex==null)
 {
 nodeDOM=nodeDOM.parentNode;
 myContainerObjIndex=MAX3315_globalSearchAContainer(nodeDOM.id);
 }

 if(MAX3315_globalListNewsScroller[myContainerObjIndex].currentUrl!=null && MAX3315_globalListNewsScroller[myContainerObjIndex].currentUrl!='' || MAX3315_globalListNewsScroller[myContainerObjIndex].hightlightNewsWithoutLinkToo)
 {
 MAX3315_globalListNewsScroller[myContainerObjIndex].obj.style.backgroundColor=MAX3315_globalListNewsScroller[myContainerObjIndex].mouseOverColor;
 }
 if(MAX3315_globalListNewsScroller[myContainerObjIndex].currentUrl!=null && MAX3315_globalListNewsScroller[myContainerObjIndex].currentUrl!='')
 { if(MAX3315_globalListNewsScroller[myContainerObjIndex].obj) { MAX3315_globalListNewsScroller[myContainerObjIndex].obj.style.cursor='pointer'; }
 } else { if(MAX3315_globalListNewsScroller[myContainerObjIndex].obj) { MAX3315_globalListNewsScroller[myContainerObjIndex].obj.style.cursor='default'; } }

 MAX3315_globalListNewsScroller[myContainerObjIndex].inPause=true;
 }

 //handler gerant la sortie de la sourie, permettant le redemarrage du defilement
 function MAX3315_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=MAX3315_globalSearchAContainer(nodeDOM.id);
 while(myContainerObjIndex==null) //on recherche le container de news
 {
 nodeDOM=nodeDOM.parentNode;
 myContainerObjIndex=MAX3315_globalSearchAContainer(nodeDOM.id);
 }

 if(MAX3315_globalListNewsScroller[myContainerObjIndex].currentUrl!=null && MAX3315_globalListNewsScroller[myContainerObjIndex].currentUrl!='' || MAX3315_globalListNewsScroller[myContainerObjIndex].hightlightNewsWithoutLinkToo)
 {
 MAX3315_globalListNewsScroller[myContainerObjIndex].obj.style.backgroundColor='';
 }
 MAX3315_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 MAX3315_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=MAX3315_globalSearchAContainer(nodeDOM.id);
 while(myContainerObjIndex==null)
 {
 nodeDOM=nodeDOM.parentNode;
 myContainerObjIndex=MAX3315_globalSearchAContainer(nodeDOM.id);
 }
 if(MAX3315_globalListNewsScroller[myContainerObjIndex].currentUrl!=null && MAX3315_globalListNewsScroller[myContainerObjIndex].currentUrl!='')
 {
 window.open(MAX3315_globalListNewsScroller[myContainerObjIndex].currentUrl,MAX3315_globalListNewsScroller[myContainerObjIndex].currentTarget,'');
 }
 }

 //lie une nouvelle instance d'un objet News a cette instance de container
 function MAX3315_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 MAX3315_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 MAX3315_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('MAX3315_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentGoOut()',this.readPauseDuration);
 }


 //animation de sortie d'une news
 function MAX3315_currentGoOut()
 {
 if(this.inPause)
 {
 setTimeout('MAX3315_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('MAX3315_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('MAX3315_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentComming()',this.outGoingSpeed);
 }
 }
 }

 //animation d'entrÃ© d'une news
 function MAX3315_currentComming()
 {
 if(this.inPause)
 {
 setTimeout('MAX3315_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('MAX3315_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentComming()',this.inCommingSpeed);
 }
 else
 {
 //on passe a l'animation de sortie apres une pause de this.readPauseDuration milisecondes
 setTimeout('MAX3315_globalListNewsScroller['+this.indexInGlobalContainerList+'].currentGoOut()',this.readPauseDuration);
 }
 }
 } 
