var DrCustomGalerie = new Class({
	/* Type de transition :  
	 * null , 0  : total fade 
	 * 1  : fade
	 * 2  : translation horisontal 
	 * 3  : translation vertical
	 * */
	initialize: function(element,images,typetrans,animtemps,defaulttemps,transition,intrafadetime,intrafadestart) {
		this.element = element;
		this.typetrans = typetrans;		
		
		
		if(!$defined(typetrans))
			this.typetrans = 0;
		this.animtemps = animtemps;
		
		if(!$defined(this.animtemps))
			this.animtemps = 500;
		
		this.intrafadetime = intrafadetime
		if(!$defined(this.intrafadetime))
			this.intrafadetime = false;
		
		
		
		this.defaulttemps = defaulttemps;
		if(!$defined(defaulttemps))
			this.defaulttemps = 5000
			
		this.intrafadestart = intrafadestart
		if(!$defined(this.intrafadestart))
			this.intrafadestart = (this.defaulttemps-this.intrafadetime)/2;
			
			
		this.transition = transition;
		if(!$defined(transition))
			this.transition = Fx.Transitions.linear;	
		
		this.transparentdelay = 200;
		
		this.animencour = false;
		
		this.linktab = [];
		
		var el = new Element("div");
		el.setStyle("width","100%");
		el.setStyle("height","100%");
		el.setStyle("overflow","hidden");
		el.setStyle("position","relative");
		
		
	
		
		this.imageToLoad = 0;
		this.imageLoaded = 0;
		
		for(var i=0;i<images.length;i++){
			
			
			
			var titre = "";
			if(images[i].titre){
				titre = images[i].titre;
			}
								
			var description = "";
			if(images[i].description){
				description = images[i].description;
			}
			if(images[i].url){
				
				var div = new Element('div');
				div.setStyles({'display':'block','width':'100%','height':'100%','position':'absolute','top':0,'left':0});
				//var img = new Element("img",{"src":images[i].url,"alt":titre,"title":description});
				this.imageToLoad++;
				var img = new Asset.image(images[i].url,{'alt':titre,'title':description,onload:this.imageLoad.bind(this)});
				
				img.setStyle("left","0");
				img.setStyle("top","0");
				img.setStyle("position","absolute");
				img.setOpacity(1);
				div.adopt(img);
				
				if(images[i].url2)
				{
					//var img2 = new Element("img",{"src":images[i].url2,"alt":titre,"title":description});
					this.imageToLoad++;
					var img2 = new Asset.image(images[i].url2,{'alt':titre,'title':description,onload:this.imageLoad.bind(this)});
					
					img2.setStyle("left","0");
					img2.setStyle("top","0");
					img2.setStyle("position","absolute");
					img2.setOpacity(0);
					div.adopt(img2);
				}
				else
					this.intrafadetime = false;
				
				
				if(images[i].link)
				{					
					this.linktab[i]=images[i].link;
					div.addEvent('click',function(){
						this.folowlink();
						}.bind(this));
					div.setStyle('cursor','pointer');
				}
				
				el.adopt(div);
				
			}			
			
			
			var tempsaffiche = "";
			if(!images[i].tempsaffiche){
				images[i].tempsaffiche = this.defaulttemps;
			}
		}
		
		
		this.conteneur = el;
		this.nbimage = el.getChildren().length;
		this.compteur = 0;
		this.images = images;
		
		this.conteneur.getChildren().setOpacity(0);
		this.conteneur.getChildren()[this.compteur].setOpacity(1)
		
		
		
		
		
	},
	start:function(){
		
	},
	imageLoad:function(){
		
		
		this.imageLoaded++;		
		
		
		if(this.imageLoaded == this.imageToLoad)
			this.allImageLoad();
	},
	allImageLoad:function(){

		this.element.empty();
		this.element.adopt(this.conteneur);
		
		
		
		if(this.nbimage > 1)
		{
			var delai = this.images[this.compteur].tempsaffiche;
			this.period = this.next.bind(this).delay(delai);
		}
		
		if(this.intrafadetime)
		{
			this.intraFade.delay(this.intrafadestart,this);
		}
	},
	folowlink:function()
	{
		
		var link = this.linktab[this.compteur];
		
		if($defined(link))
		window.location.href=link;
		
	},
	
	next: function(){
		
		if(!this.animencour)
		{
		
			var indextohide = this.compteur;
			
			var indextoshow = this.compteur +1 ;
			
			if(indextoshow > this.nbimage-1)
				var indextoshow = 0;
		
		
			this.animencour = true;
			
			switch (this.typetrans) {
				case 1: this.fade(indextoshow,indextohide);			
					break;
				case 2: this.translation(indextoshow,indextohide,'left',1);			
					break;
				case 3: this.translation(indextoshow,indextohide,'top',-1);			
					break;	
				default: this.totalFade(indextoshow,indextohide);
					break;
			}
			
			this.compteur = indextoshow;
			this.fireEvent('change',this.compteur);
			
			if(this.intrafadetime)
			{
				
				this.intraFade.delay(this.intrafadestart,this);
			}
			
		}
		
		
	},
	setIndex : function(idx){
		

		if(!this.animencour && idx < this.nbimage && idx >= 0 && idx != this.compteur)
		{
		
			var indextohide = this.compteur;
			
			var indextoshow = idx ;
			
			if(indextoshow > this.nbimage-1)
				var indextoshow = 0;
		
		
			this.animencour = true;
			
			switch (this.typetrans) {
				case 1: this.fade(indextoshow,indextohide);			
					break;
				case 2: this.translation(indextoshow,indextohide,'left',1);			
					break;
				case 3: this.translation(indextoshow,indextohide,'top',-1);			
					break;	
				default: this.totalFade(indextoshow,indextohide);
					break;
			}
			
			this.compteur = indextoshow;
			this.fireEvent('change',this.compteur);
			
			if(this.intrafadetime)
			{
				
				this.intraFade.delay(this.intrafadestart,this);
			}
			
			
			
		}
		
	},
	previous: function(){
		
		//console.log("previous :" + this.typetrans);
		if(!this.animencour)
		{
			
			var indextohide = this.compteur;
			
			var indextoshow = this.compteur -1 ;
			
			if(indextoshow < 0)
				var indextoshow = this.nbimage-1;
			
			this.animencour = true;
		
			switch (this.typetrans) {
				case 1: this.fade(indextoshow,indextohide);			
					break;
				case 2: this.translation(indextoshow,indextohide,'left',-1);			
					break;
				case 3: this.translation(indextoshow,indextohide,'top',1);			
					break;		
				default: this.totalFade(indextoshow,indextohide);
					break;
			}
			
			this.compteur = indextoshow;
			this.fireEvent('change',this.compteur);

			if(this.intrafadetime)
			{
				
				this.intraFade.delay(this.intrafadestart,this);
			}
			
		}
	},
	intraFade: function(){		
			
			
			var div = this.conteneur.getChildren()[this.compteur];	
		
			this.intraFadeeffect = div.getLast().effect('opacity', {duration: this.intrafadetime, transition: Fx.Transitions.linear}).start(0, 1);	
		
		
	},
	initintraFade: function(previousidx){		
		
		if(this.intraFadeeffect)
			this.intraFadeeffect.stop();
		var div = this.conteneur.getChildren()[previousidx];			
		div.getLast().setOpacity(0);
	},
	pause: function(){
		$clear(this.period);
	},
	play: function(){
		this.pause();
		var delai = this.images[this.compteur].tempsaffiche;
		this.period = this.next.bind(this).delay(delai);
		this.animencour = false;
	},
	endAnimation : function(previousidx){
		if(this.intraFadeeffect)
			this.initintraFade(previousidx);
		this.play();
	},
	translation : function(indextoshow,indextohide,type,sens){
		this.pause();
		
		
		var toshow = this.conteneur.getChildren()[indextoshow];
		var tohide = this.conteneur.getChildren()[indextohide];
	
		
		toshow.setStyle(type.toString(), (100*sens)+ '%');
		toshow.setOpacity(1);
		
		
		var fx = new Fx.Elements($$($(toshow),$(tohide)), {duration: this.animtemps, transition: this.transition,unit:'%'});
		
		var o = {};
		
		eval("o[0] = {"+type+" : [ "+sens*100+" , 0 ]};");
		
		eval("o[1] = {"+type+" : [ 0 , "+sens*100*(-1)+" ]};");
		
				
		fx.start(o);
		
		fx.addEvent('onComplete', function() {
			
			tohide.setOpacity(0);
			tohide.setStyle(type, '0%');
			this.endAnimation(indextohide);
		}.bind(this));
		
		
	},
	
	
	totalFade : function(indextoshow,indextohide)
	{		
		var fx = this.conteneur.getChildren()[indextohide].effect('opacity',{duration: this.animtemps, transition: this.transition}).start(1, 0);
		
		fx.addEvent('onComplete', function() {
			(function() {
				var myEffects = this.conteneur.getChildren()[indextoshow].effect('opacity', {duration: this.animtemps, transition: this.transition}).start(0,1);
				myEffects.addEvent('onComplete', function() {
					this.endAnimation(indextohide);
				}.bind(this));
			}).delay(this.transparentdelay,this);				
		}.bind(this));
		
	},
	fade : function(indextoshow,indextohide)
	{			
		if(indextoshow > indextohide)
		{
				
			var myEffects = this.conteneur.getChildren()[indextoshow].effect('opacity', {duration: this.animtemps, transition: this.transition}).start(0, 1);
		}
		else
		{
			this.conteneur.getChildren()[indextoshow].setOpacity(1);
			var myEffects = this.conteneur.getChildren()[indextohide].effect('opacity', {duration: this.animtemps, transition: this.transition}).start(1, 0);
		}		
		
		myEffects.addEvent('onComplete', function() {
			this.conteneur.getChildren()[indextohide].setOpacity(0);
			this.endAnimation(indextohide);
		}.bind(this));		
		
	}	
});
DrCustomGalerie.implement(new Events);



