/***************************************/
/*   GESTIONNAIRE D'AUTO SCROLLER      */
/*                                     */
/*   http://codessources.votre-web.com */
/*                                     */
/***************************************/

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

var NEWSPCT_default_mouseOverColor='#ffffff';
var NEWSPCT_default_hightlightNewsWithoutLinkToo=false;
var NEWSPCT_default_outGoingFade=0.1;
var NEWSPCT_default_outGoingSpeed=20;
var NEWSPCT_default_pauseDuration=0;

var NEWSPCT_default_inCommingFade=-0.1;
var NEWSPCT_default_inCommingSpeed=20;
var NEWSPCT_default_readPauseDuration=2000;
var NEWSPCT_default_forceAMinHeightToContainerHeight=true;


//Il est fortement déconseillé de mofidier le code à partir de cette ligne

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

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

/***********************************/
/**       CONTAINER OBJET         **/
/***********************************/
function NEWSPCT_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=NEWSPCT_default_mouseOverColor;
	this.hightlightNewsWithoutLinkToo=NEWSPCT_default_hightlightNewsWithoutLinkToo;
	this.outGoingFade=NEWSPCT_default_outGoingFade;
	this.outGoingSpeed=NEWSPCT_default_outGoingSpeed;
	this.pauseDuration=NEWSPCT_default_pauseDuration;
	this.inCommingFade=NEWSPCT_default_inCommingFade;
	this.inCommingSpeed=NEWSPCT_default_inCommingSpeed;
	this.readPauseDuration=NEWSPCT_default_readPauseDuration;
	this.forceAMinHeightToContainerHeight=NEWSPCT_default_forceAMinHeightToContainerHeight;

	
	//methodes
	this.addNews=NEWSPCT_addNews;
	this.initAllNews=NEWSPCT_initAllNews;
	this.launch=NEWSPCT_launch;
	this.currentGoOut=NEWSPCT_currentGoOut;
	this.currentComming=NEWSPCT_currentComming;
	this.loadFromUl=NEWSPCT_loadFromUl;
	//les listener
	this.overContainer=NEWSPCT_overContainer;
	this.outContainer=NEWSPCT_outContainer;
	this.openMore=NEWSPCT_openMore;
	
	//enregistrement de l'objet dans la liste global
	this.indexInGlobalContainerList=NEWSPCT_globalListNewsZapper.length;
	NEWSPCT_globalListNewsZapper.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 NEWSPCT_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 NEWSPCT_News(null,content,url,target));
  }
    
  this.launch();
}

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

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

//handler gerant la sortie de la sourie, permettant le redemarrage du defilement
function NEWSPCT_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=NEWSPCT_globalSearchAContainer(nodeDOM.id);
	while(myContainerObjIndex==null) //on recherche le container de news
	{
		nodeDOM=nodeDOM.parentNode;
		myContainerObjIndex=NEWSPCT_globalSearchAContainer(nodeDOM.id);
	}
	
	if(NEWSPCT_globalListNewsZapper[myContainerObjIndex].currentUrl!=null && NEWSPCT_globalListNewsZapper[myContainerObjIndex].currentUrl!='' || NEWSPCT_globalListNewsZapper[myContainerObjIndex].hightlightNewsWithoutLinkToo)
	{
		NEWSPCT_globalListNewsZapper[myContainerObjIndex].obj.style.backgroundColor='';
	}
	NEWSPCT_globalListNewsZapper[myContainerObjIndex].inPause=false;
}

//gestion du clic sur le container, avec eventuelle ouverture du lien de la news courante (si elle en possede un)
function NEWSPCT_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=NEWSPCT_globalSearchAContainer(nodeDOM.id);
	while(myContainerObjIndex==null)
	{
		nodeDOM=nodeDOM.parentNode;
		myContainerObjIndex=NEWSPCT_globalSearchAContainer(nodeDOM.id);
	}
	if(NEWSPCT_globalListNewsZapper[myContainerObjIndex].currentUrl!=null && NEWSPCT_globalListNewsZapper[myContainerObjIndex].currentUrl!='')
	{
		window.open(NEWSPCT_globalListNewsZapper[myContainerObjIndex].currentUrl,NEWSPCT_globalListNewsZapper[myContainerObjIndex].currentTarget,'');
	}
}

//lie une nouvelle instance d'un objet News a cette instance de container
function NEWSPCT_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 NEWSPCT_initAllNews()
{
	//evite affichage de la liste ul/li tant que le script n'a pas été chargé
	document.getElementById('refs_ctnr').style.display = "block";
	
	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 NEWSPCT_launch()
{
	if(!this.initilised) { this.initAllNews(); }
	
	this.listNews[this.currentIndex].div.style.top='0px';
	this.listNews[this.currentIndex].div.style.display='block';
	this.listNews[this.currentIndex].div.style.opacity='1';
	this.currentUrl=this.listNews[this.currentIndex].url;
	this.currentTarget=this.listNews[this.currentIndex].target;
	
	this.obj.appendChild(this.listNews[this.currentIndex].div);
	setTimeout('NEWSPCT_globalListNewsZapper['+this.indexInGlobalContainerList+'].currentGoOut()',this.readPauseDuration);
}


//animation de sortie d'une news
function NEWSPCT_currentGoOut()
{
	if(this.inPause)
	{
		setTimeout('NEWSPCT_globalListNewsZapper['+this.indexInGlobalContainerList+'].currentGoOut()',this.pauseDuration);
	}
	else
	{		
		var tmpOpac=this.listNews[this.currentIndex].div.style.opacity;
		this.listNews[this.currentIndex].div.style.opacity=(tmpOpac-this.outGoingFade);
		this.listNews[this.currentIndex].div.style.filter='alpha(opacity='+(tmpOpac-this.outGoingFade)+')'; 
		
		if(tmpOpac>=0)
		{
			setTimeout('NEWSPCT_globalListNewsZapper['+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.opacity='0';
			//this.listNews[this.currentIndex].div.style.filter='alpha(opacity=0)'; 
			this.listNews[this.currentIndex].div.style.top='0px';
			
			this.obj.appendChild(this.listNews[this.currentIndex].div);
	
			setTimeout('NEWSPCT_globalListNewsZapper['+this.indexInGlobalContainerList+'].currentComming()',this.outGoingSpeed);
		}
	}
}

//animation d'entré d'une news
function NEWSPCT_currentComming()
{
	if(this.inPause)
	{
		setTimeout('NEWSPCT_globalListNewsZapper['+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 tmpOpac=this.listNews[this.currentIndex].div.style.opacity;
		this.listNews[this.currentIndex].div.style.opacity=(tmpOpac-this.inCommingFade);
		
		/*var tmpFilter=this.listNews[this.currentIndex].div.style.filter.replace('alpha(opacity=','');
		tmpFiler.replace(')','');
		var newOpac = tmpFilter-this.inCommingFade-(-20)
		this.listNews[this.currentIndex].div.style.filter='alpha(opacity='+newOpac+')'; */
		
		if(tmpOpac<=1)
		{
			setTimeout('NEWSPCT_globalListNewsZapper['+this.indexInGlobalContainerList+'].currentComming()',this.inCommingSpeed);
		}
		else
		{
			//on passe a l'animation de sortie apres une pause de this.readPauseDuration milisecondes
			setTimeout('NEWSPCT_globalListNewsZapper['+this.indexInGlobalContainerList+'].currentGoOut()',this.readPauseDuration);
		}
	}
}
