/**
 * div, id = menuId, class = scroll_container
 * div/ul, id = subMenuId, class = scroll_content
 */

function Carrousel()
{
    var oThis = this; 					// création d'une référence vers l'objet courant
	this.sIdCarrousel = '';
	this.iDuree = 2;					// duree en seconde pour le fade
	this.bBoucleAuto = true;			// boolean
	this.iPos = 0;						// element en cours
	this.iNbItems = 0;					// nombre de li 
	this.bActionable = true;
	
	this.Init = function(sIdCarrousel)
	{
		// Attributs du composant
		this.sIdCarrousel = sIdCarrousel;
		var ulListe = document.getElementById(sIdCarrousel); 		// ul contenaut les actus renvoie la classe caroussel
		// recuper nb items	
		 var filsUlListe = ulListe.getElementsByTagName("*");	
		 for(var i=0; i<filsUlListe.length; i++){
			if( filsUlListe[i].id.indexOf(sIdCarrousel)>-1) {
				this.iNbItems = this.iNbItems +1;
			}
    	 }
	}

	this.AfficheUnique = function(iIndex)
	{
		if (this.bActionable && iIndex <= this.iNbItems-1 && iIndex!=this.iPos )
		{			
				this.SetActive(false);
				new Effect.Fade(this.sIdCarrousel +'_' + this.iPos,{ duration: this.iDuree });
				new Effect.Appear(this.sIdCarrousel +'_' + iIndex,{ duration: this.iDuree });
				setTimeout(function() { oThis.SetActive(true); }, this.iDuree*1000);
				this.iPos = iIndex;
				this.ActualiserPositionPlayer(iIndex);
				this.bBoucleAuto = false;
		}
	}
	
	this.Affiche = function(iIndex)
	{
		// on ne peut pas afficher plus d'éléments que ce qu'on a dans la liste et on n'agit pas sur l'élément déjà affiché
		
		if (this.bActionable && iIndex <= this.iNbItems-1 && iIndex!=this.iPos )
		{			
				this.SetActive(false);
				new Effect.Fade(this.sIdCarrousel +'_' + this.iPos,{ duration: this.iDuree });
				new Effect.Appear(this.sIdCarrousel +'_' + iIndex,{ duration: this.iDuree });
				setTimeout(function() { oThis.SetActive(true); }, this.iDuree*1000);
				this.iPos = iIndex;
				this.ActualiserPositionPlayer(iIndex);
				//document.getElementById('position_courante').innerHTML=this.iPos+1 + '/'+ this.iNbItems;
		}
	}
	
	this.ActualiserPositionPlayer = function(iIndex)
	{
		var ulPlayer = document.getElementById('player_slideshow_' + this.sIdCarrousel);
		if (ulPlayer != null) {
			 var filsulPlayer = ulPlayer.getElementsByTagName("*");
			for(var i=0; i<filsulPlayer.length; i++){
				
					filsulPlayer[i].className = 'lien_slideshow';
	    	 }
		}
		
		var liPlayer = document.getElementById(this.sIdCarrousel + '_player_' + this.iPos);
		if (liPlayer != null) {
			liPlayer.className = liPlayer.className + " actif";
		}
	}

	this.SetActive = function(actif) {
		this.bActionable = actif;
	}

	this.Defile = function()
	{
		// gerer defilement
		if (this.bBoucleAuto)
		{
			if (this.iPos < this.iNbItems-1) {
				this.Affiche(this.iPos+1);
			}
			else {
				this.Affiche(0);
				this.bBoucleAuto=true;
				
				//this.MasquerPause();
			}
			// gerer recursif
			setTimeout(function() { oThis.Defile(); }, 7000);
		}
	}

	this.Play = function(auto) 
	{
		if (this.bActionable){
			this.bBoucleAuto = true;
			this.MasquerPlay();
			this.Defile();
		}
	}

	this.Pause = function()
	{
		this.bBoucleAuto = false;
		this.MasquerPause();
	}

	this.Suivant = function()
	{
		if (this.bActionable && this.iPos < this.iNbItems-1){		
			this.bBoucleAuto = false;
			this.Affiche(this.iPos+1);
			this.MasquerPause();
		}
	}

	this.Precedent = function()
	{
		if (this.bActionable && this.iPos > 0){
			this.bBoucleAuto = false;
			this.Affiche(this.iPos-1);
			this.MasquerPause();
		}
	}

	this.MasquerPlay = function()
	{
		document.getElementById('bouton_pause').style.display='block';
		document.getElementById('bouton_play').style.display='none';
	}
	
	this.MasquerPause = function()
	{
		document.getElementById('bouton_pause').style.display='none';
		document.getElementById('bouton_play').style.display='block';
	}
	
	this.hauteurImage = function(sIdCarrousel)
	{
		var docPere = document.getElementById(sIdCarrousel);
		var docFils = docPere.getElementsByTagName("*");
		
		
			if (document.all) // ok I.E
			{
				H = doc[0].currentStyle.height;
			}
			else // ok firefox.0.9.2 , pas mozilla.1.0 ni netscape.7.02
			{
				H = document.defaultView.getComputedStyle(docFils[0], null).height;
			}
			
			docPere.style.height= H ;
		
	}
}

