// SQUARE BREVES > Developpement Paul Fauchille
var SQBreves = function(){
  this.Anim;
  this.Nb;
  this.TimeAnim;
  this.Time;
  this.imgHeight;
  this.Width;
  this.CloneList;
  this.WidthClone;
  this.Left;
  this.Num=0;
  this.Start=0;
  this.Defil=1;
  this.TimeByChar=90; // Time in ms for one character
  this.AdditionnalTime=1000; // Time to add at "normal" next time
  this.count=0;
};

SQBreves.prototype.StopDefil=function(){ MyBreve.Defil=0; };
SQBreves.prototype.StartDefil=function(){ MyBreve.Defil=1; };


SQBreves.prototype.config=function(Width,Height,BgColor,Anim,TimeAnim,Time){

	this.imgHeight = Number($("#breveimg").height());
	this.Anim = Anim;
	this.Nb=$(".actus").length-1;
	this.TimeAnim = Number(TimeAnim);
	this.Time = Number(Time)+(2*this.TimeAnim);
	this.Width = Width;
	$("#actu").css({height: (Number(Height)+Number(this.imgHeight))+"px",width: Width+'px',overflow:'hidden'});
	$("#breves").css({"height": Height+"px", "background-color": BgColor, "overflow": "hidden"});
	
	if(Anim=="slideLeft"){ // #### ANIMATION SLIDE LEFT #### //
	
		$(".actus").each( function(){ $(this).css({height: Height+'px', overflow: "hidden"}); } );
		
	}else if(Anim=="slideLeft2"){ // #### ANIMATION SLIDE (FLUX CONTINU) #### //
	
		this.Time=Number(Time); this.Nb++; var w=0;
		$("#breves").css({width: '50000px'});
		$(".actus").each(function(){
			wid=$(this).css({'display':'block','float':'left','overflow':'hidden','height':Height+'px','line-height':Height+'px','padding':'0px','margin':'0px'}).removeAttr("id").width();
			wid+=15; $(this).css({width: wid+'px'}); w+=wid;
		});
		var clone=$("#breves").css({width:w+'px',overflow:'visible'}).html();
		$("#actu").append('<div id="breves2">'+clone+'</div>');
		$("#breves").css({width: w+'px'});
		$("#breves2").css({position:'absolute',top:0,left:w+'px',width:w+'px',"height": Height+"px", "background-color": BgColor, "overflow": "hidden"});
		this.WidthClone=w; w2=2*w; this.CloneList=new Array("breves","breves2");
		while(w2<Width*2){
			var nb=this.CloneList.length+1; this.CloneList[nb-1]="breves"+nb;
			w2+=w; $("#actu").append('<div id="'+this.CloneList[nb-1]+'">'+clone+'</div>');
			$("#"+this.CloneList[nb-1]).css({position:'absolute',top:0,left:w+'px',width:w+'px',"width": Width+"px", "height": Height+"px", "background-color": BgColor, "overflow": "hidden"});
		}
		$(".actus").hover(this.StopDefil,this.StartDefil)
		
	}
	
	this.start();
	
};


SQBreves.prototype.start=function(){
	this.count++;
	if(this.Defil==1){ // ## DEFILE OU ARRET ON HOVER
		
		
		// ######### NEWS ACTUELLE ######### //
		if(this.Anim!="slideLeft2"){
		
			var d = $("#breve"+this.Num); // Breve Actuel
			if(this.Num<this.Nb&&this.Start!=0) this.Num++; else this.Num=0; // Verifie si retour à 0
			var e = $("#breve"+this.Num); // Breve Suivante
			
		} 
		// ######### NEWS ACTUELLE ######### //
		
		
		
		if(this.Start==0) { // ## INITIALISATION ##
			
			// ################# INITIALISATION #################### //
		
			if(this.Anim=="slideLeft") { // Show First News > Anim

				d.css({position: 'absolute',left: this.Width+'px',display: 'block'});
				var show = setTimeout( function(){ d.animate({left: 0},this.TimeAnim); clearTimeout(show); }, 50 );
				this.NexTime = e.text().length * this.TimeByChar; this.NexTime+=this.AdditionnalTime;

			}else if(this.Anim=="slideLeft2"){ // Set Position for each Clone
			  this.Left=0;
				for(var i=0;i<this.CloneList.length;i++){
					w=this.WidthClone*i;
					$("#"+this.CloneList[i]).css({position:'absolute',top:0,left:w+'px'});
				}
				
			}else{ // ## Show First News
			
				d.show();
				
			}
			this.Start=1; // Defini Initialisation est terminée
			
			// ################# INITIALISATION #################### //
			
			
			
			
		}else if(this.Start==1 && this.Nb>0){ // ## Cas classique > Passage à la suivante
		
			
			// ################# ANIMATION > SUIVANTE #################### //
		
			if(this.Anim=="slide"){ // Hide Actual SLIDE (UP)
			
				d.slideUp(this.TimeAnim);
				
			}else if(this.Anim=="slideLeft"){ // Hide Actual SLIDE LEFT
			
				d.css({position: 'absolute'}).animate({left: this.Width*-1}, this.TimeAnim); // set position tou absolute and animate left
				var disapear = setTimeout( function(){ d.hide().css({left: this.Width+'px'}); clearTimeout(disapear); }, this.TimeAnim );
				
			}else if(this.Anim=="slideLeft2"){ // Defilement des block clones, et remet à la suite de la chaine sinon
        
        this.Left-=this.Time; // this.Time = Nombre de pixel en moins
        if(this.Left<-this.WidthClone) this.Left=this.WidthClone*(this.CloneList.length-1);
				for(var i=0;i<this.CloneList.length;i++){
          if(this.Left>0){
            w=this.Left-( -(i-(this.CloneList.length-1))*this.WidthClone );
            if(w<-this.WidthClone) w=this.Left+( i*this.WidthClone );
          }else{
            w=this.Left+( i*this.WidthClone );
          }
					if(w<=-this.WidthClone){ w=this.WidthClone*(this.CloneList.length-1); }
          $("#"+this.CloneList[i]).css({left:w+'px'});
				}
				
			}else{ // Hide Actual FADE
			
				d.fadeOut(this.TimeAnim);
				
			}
			
			if(this.Anim=="slide"){ // Show Next SLIDE (DOWN)
				
				var show = setTimeout( function(){ e.slideDown(this.TimeAnim); clearTimeout(show); }, this.TimeAnim );
				
			}else if(this.Anim=="slideLeft") { // Show Next SLIDE LEFT
			
				e.css({position: 'absolute',left: this.Width+'px',display: 'block'}).animate({left: 0},this.TimeAnim);
				this.NexTime = e.text().length * this.TimeByChar; this.NexTime+=this.AdditionnalTime; clearTimeout(show);
				
			}else if(this.Anim=="slideLeft2"){
				
				// Nothing
				
			}else{ // Show Next FADE
			
				var show = setTimeout( function(){ e.fadeIn(this.TimeAnim); clearTimeout(show); }, this.TimeAnim );
				
			}
			
			// ################# ANIMATION > SUIVANTE #################### //		
				
		}
		
	}// ## DEFILE OU ARRET ON HOVER
	
	
	
	
	// ########### TIMER POUR ANIMATION SUIVANTE ########### //
	if(this.Anim=="slideLeft2"){ // ## CAS DEFILEMENT CONTINUE
	
		var next = setTimeout(function(){ MyBreve.start(); clearTimeout(next); }, this.TimeAnim/10 ); // Temps d'animation / 10 pour le prochain decalage
		
	}else{ // ## AUTRE CAS
	
		if(!this.NexTime || this.NexTime < this.Time) this.NexTime = this.Time;
		var next = setTimeout( function() { MyBreve.start(); clearTimeout(next); } , this.NexTime );
		
	}
	// ########### TIMER POUR ANIMATION SUIVANTE ########### //
	
	
};

MyBreve=new SQBreves();

