﻿// najsDiaporama v1.1.0
// by Newton Applications - http://www.newtonapplications.com
// Create : 2009-06-12
// Last Change : 2009-12-13
// For more information on this script, visit: http://www.newtonapplications.com/
// Requirements : prototype.js (1.6.0) / scriptaculous.js (1.7.1)

var najsDiaporama=Class.create({
nameClass: 'najsDiaporama',version: '1.1.0',idInstance: '',
lastDate: '',
initialize: function(idOfInstance,idBaseDiapo,delay,images,transitionDefaut) { 
	this.idInstance=idOfInstance;
	this.images=images;
	this.idBaseDiapo=idBaseDiapo;
	this.delay=delay;
	this.layer='A';
	this.current=0;
	this.receiver=[];
	this.transitionDefaut=transitionDefaut;
	Event.observe(window, "load", this.start.bindAsEventListener(this));
},
start: function(){this.intervalle=new PeriodicalExecuter(this.next.bindAsEventListener(this), this.delay);},
stop: function(){this.intervalle.stop();},
next: function(){
	this.lastImage=this.current;
	this.current++;
	if(this.images.length==this.current)this.current=0;
	this.imageFade=this.idBaseDiapo+this.layer;
	if(this.layer=='A')this.layer='B'; else this.layer='A';
	
	if($(this.idBaseDiapo+this.layer)!=null) $(this.idBaseDiapo+this.layer).remove();
	
	if(this.images[this.current].url!=''){
		var newimage='<div class="zoneimage" id="'+this.idBaseDiapo+this.layer+'" style="position:absolute;top:0px;left:0px;"><a href="'+this.images[this.current].url+'"><div class="masqueSurDiaporama"></div><img  id="'+this.idBaseDiapo+this.layer+'Image" src="'+this.images[this.current].image+'" /></a></div>';
	}else{
		var newimage='<div class="zoneimage" id="'+this.idBaseDiapo+this.layer+'" style="position:absolute;top:0px;left:0px;"><div class="masqueSurDiaporama"></div><img  id="'+this.idBaseDiapo+this.layer+'Image" src="'+this.images[this.current].image+'" /></div>';
	}
	$(this.idBaseDiapo).insert({top:newimage});
	Event.observe($(this.idBaseDiapo+this.layer+"Image"), "load", this.imageok.bindAsEventListener(this));
	this.stop();
},
imageok:function(){
	var transitionDemande=this.transitionDefaut;
	if(Object.isUndefined(transitionDemande)) var transitionDemande={nom:"Fade"};
	
	if(transitionDemande.nom=='Puff'){
		Effect.Puff(this.imageFade+"Image",{afterFinish:this.start.bindAsEventListener(this)});
	}else if(transitionDemande.nom=='Fold'){
		Effect.Fold(this.imageFade+"Image",{afterFinish:this.start.bindAsEventListener(this)});
	}else if(transitionDemande.nom=='Mosaique'){
		this.transition_mosaique(transitionDemande);
	}else if(transitionDemande.nom=='Cut'){
		$(this.imageFade+"Image").hide();
		this.start();
	}else{
		Effect.Fade(this.imageFade,{afterFinish:this.start.bindAsEventListener(this)});
	}
	
},

//

transition_mosaique:function(transitionDemande){
	if(!Object.isUndefined(transitionDemande.param)){
		var styleTransition=transitionDemande.param;
	}else{
		var styleTransition={nLargeur:4,nHauteur:4,delay:0.1,vitesse:0.3}; // Simple
		//var styleTransition={nLargeur:1,nHauteur:10,delay:0.1,vitesse:0.3}; // Digital
	}

	var largeurImage=$(this.imageFade+"Image").getWidth();
	var hauteurImage=$(this.imageFade+"Image").getHeight();
	var largeurBloc=Math.round(largeurImage/styleTransition.nLargeur);
	var hauteurBloc=Math.round(hauteurImage/styleTransition.nHauteur);
	var listeBloc=[];
	
	var styleBloc='width:'+largeurBloc+'px;height:'+hauteurBloc+'px;overflow:hidden;position:absolute;';
	var newimage='<div style="position:relative;top:0px;left:0px;" >';
	for (var y=0; y<styleTransition.nHauteur; ++y) {
		var positionBlocHauteur=y*hauteurBloc;
		for (var i=0; i<styleTransition.nLargeur; ++i) {
			listeBloc.push(this.idBaseDiapo+this.layer+'Image_N'+y+i);
			var positionBlocLargeur=i*largeurBloc;
			var baseImage='<img style="position:absolute;top:-'+positionBlocHauteur+'px;left:-'+positionBlocLargeur+'px;" src="'+this.images[this.lastImage].image+'" />';
			newimage+='<div style="'+styleBloc+'top:'+positionBlocHauteur+'px;left:'+positionBlocLargeur+'px;" id="'+this.idBaseDiapo+this.layer+'Image_N'+y+i+'">'+baseImage+'</div>';
		}
	}
	newimage+='</div>';
	$(this.imageFade+"Image").hide();
	$(this.imageFade+"Image").insert({before:newimage});
	
	this.stop();
	
	if(!Object.isUndefined(styleTransition.ordre)){var ordreDefini=styleTransition.ordre.split(",");}
	else{var ordreDefini=new Array();}

	for (var i=0; i<listeBloc.length; ++i) {
		var delayDuBloc=i*styleTransition.delay;
		if(!Object.isUndefined(ordreDefini[i])){var y=ordreDefini[i]-1;}
		else{var y=i;}
		if(i==(listeBloc.length-1)){
			new Effect.Opacity(listeBloc[y], { from: 1.0, to: 0.0, duration: styleTransition.vitesse, transition: Effect.Transitions.linear, delay: delayDuBloc, afterFinish:this.start.bindAsEventListener(this)});
		}else{
			new Effect.Opacity(listeBloc[y], { from: 1.0, to: 0.0, duration: styleTransition.vitesse, transition: Effect.Transitions.linear, delay: delayDuBloc });
		}
	}
}

});
