

window.addEvent('domready', function() {
	

	
	
	var simpleDiaporamaInstance = new simpleDiaporama({

	});	

	
	
	
	
});


var simpleDiaporama = new Class({
	

	
	
	
	Implements: Options,
	
	options: {
		repCible: '../../deploy',
		fullPath: '',
		simpleDiaporamaIdLocal: ''
	},
	
	initialize: function(options){
	
					////console.log("canvas1");	
					//	 	initCanvas();
		this.setOptions(options);
		this.repCible = '../../deploy';
		this.play=true;
		this.indexDiap = 0;
		this.tabImage = new Array();
		this.loaderActif = "";
		this.listeDiapos = new Object();
		this.liste_id = new Object();
		this.xImg = new Element('img');
		this.transitionPlay = true;
		this.viewA=getChaine();
		this.viewB=getChaine();
		this.loaderA=getChaine();
		this.loaderB=getChaine();
		
				this.viewActive = this.viewB;
	 	//this.init();

 	if(typeof(simpleDiaporamaId) != "undefined"){
	this.init();
 	};
			
			
	},







 	
 	 activeLect: function(){
 			$('DiapLect').setAttribute('data-selected','true');
		$('DiapLect').setStyle('opacity','1');
			$('DiapPause').setAttribute('data-selected','false');
		$('DiapPause').setStyle('opacity','0.5');
$('DiapDiapos').setAttribute('data-selected','true');
		$('DiapDiapos').setStyle('opacity','1');
 	},
 	
 activePause: function(){
 			$('DiapLect').setAttribute('data-selected','false');
		$('DiapLect').setStyle('opacity','0.5');
			$('DiapPause').setAttribute('data-selected','true');
		$('DiapPause').setStyle('opacity','1');
 	},
 	
avanceDiapos: function(){
			//console.log("avanceDiapos--");
 			if((this.indexDiap+1)<this.listeDiapos.length){
			this.indexDiap++;
			}else{
			this.indexDiap = 0;
			};
			if(this.listeDiapos.length >1){
			this.afficherContenuDiapos();
			};

 	},
 	
init: function(){
	

this.idRec = $(simpleDiaporamaId).getAttribute("data-memrec");

this.activeLect();


 
$('DiapPause').addEvent('mousedown', function(e) {
				e.stop();	
			this.play=false;
			this.activePause();
					}.bind(this)
					);					
$('DiapMosaique').addEvent('mousedown', function(e) {
				e.stop();	
				window.location.href ="http://www.latitudegps.com/deploy2/php/readPageId.php?id=181";
					}.bind(this)
					);
$('DiapDiapos').addEvent('mousedown', function(e) {
				e.stop();	
			
					}.bind(this)
					);
$('DiapLoupe').addEvent('mousedown', function(e) {
				e.stop();	
			this.play=false;
				this.activePause();
					}.bind(this)
					);
					
					$('DiapLect').addEvent('mousedown', function(e) {
				e.stop();	
			this.play=true;
				this.swapDiv();
			this.activeLect();
					}.bind(this)
					);		
					
					
$('DiapRecule').addEvent('mousedown', function(e) {
				e.stop();	
			this.play=false;
			
			////console.log(indexDiap);
			if(this.indexDiap>0){
	this.indexDiap--;
	this.afficherContenuDiapos();
		}else{
		this.indexDiap = 	this.listeDiapos.length-1;
		this.afficherContenuDiapos();
		};
		
	this.activePause();

		var info= "Image "+(parseInt(this.indexDiap)+1)+"/"+this.listeDiapos.length
	  $('DiapInfos').set("text",info);
					}
					.bind(this));
					
$('DiapAvance').addEvent('mousedown', function(e) {
				e.stop();	
			this.play=false;
		this.avanceDiapos();
			
			this.activePause();


			var info= "Image "+(parseInt(this.indexDiap)+1)+"/"+this.listeDiapos.length
	 		 $('DiapInfos').set("text",info);
					}.bind(this)
					
					);

 
 
 
 
 
 
 
 
 
 
 var self = this;
 
 
 		
this.xImg.addEvent('load',function(){

var H= this.height;
var L= this.width;
var T="";
var coef = H/L;

//ajusteTaille(L,H)
if(H>=L){
	t="V";
	H = sdH-86;
	L = H*coef;
} else 
{
	
	t="H";
	L = sdW-86;
	H = L*coef;
////console.log("--"+L+"-/"+H+"--"+coef);
};
////console.log(t);

if(self.loaderActif == self.loaderA){

/*$("loaderA").setProperty("src",this.src);
$("loaderA").setProperty("height",H);


	$(self.loaderActif).setStyle("opacity", 0);
	$("viewA").setStyle("z-index",1);
	$("viewB").setStyle("z-index",0);
	self.changer2();	
	*/
	
	
}else{

$(self.loaderB).setProperty("src",this.src);

$(self.loaderB).setProperty("height",H);

	$(self.loaderActif).setStyle("opacity", 0);
	$(self.viewB).setStyle("z-index",1);
	$(self.viewA).setStyle("z-index",0);
	
	self.changer2();		
	
	
	
};









});




this.creer_structure(simpleDiaporamaId);
this.indexDiap = 0;
 	},
 	


swapDiv: function() {
	//	//console.log(this.play);
	if(this.play){
		this.avanceDiapos();
		var info= "Image "+(parseInt(this.indexDiap)+1)+"/"+this.listeDiapos.length
	  $('DiapInfos').set("text",info);
	};
},

 	
precharger_image: function(url)
{
var 	$imageSrc = url

	
	if(this.repCible  !=""){

$imageSrc = $imageSrc.replace("./",this.repCible+"/");
}else{
$imageSrc= "."+url;
};
$imageSrc = $imageSrc.replace("_i","_z");
////console.log($imageSrc);
$(this.xImg).setProperty("src",$imageSrc);
},
 	
creer_structure: function(p){
 // JSON request with data
	////console.log('auto');
	
	   //self is this
        var self = this;
	//ou
	//
    var jsonReq = new Request.JSON({
      url: '../php/simpleDiaporama.php',
      method: 'post',
      data: {
        json: 'yes'
      },  
      onComplete: function(r){
     self.diaporamaPanel = $(p);
	self.suite(r);
      }
      
      //ou
      /* onComplete: function(r){
    this.diaporamaPanel = $(p);
	this.suite(r);
      }.bind(this)
*/
      
      
    });
    
	jsonReq.send();
	
},

suite: function(r){
	   this.listeDiapos=r.image;
		
var info= "Image "+(parseInt(this.indexDiap)+1)+"/"+this.listeDiapos.length
	  $('DiapInfos').set("text",info);
this.diaporamaPanel.innerHTML = '<div id="'+this.viewA+'" style="position:absolute; visibility:hidden; z-index:1" ><table  style="height:'+(sdH-26)+'px; width:'+sdW+'px;text-align: center;"><tr><td><img id="'+this.loaderA+'" style="opacity:0" /></td></tr></table></div><div id="'+this.viewB+'" style="position:absolute; z-index:0" ><table   style="height:'+(sdH-26)+'px; width:'+sdW+'px;    text-align: center; vertical-align:middle;"><tr><td><img id="'+this.loaderB+'"  /></td></tr></table></div>';
	   
	   
	   
 // diaporamaPanel.innerHTML =	'<div id="viewB" style="position:absolute; z-index:1" ><img id="loaderB" style="position:absolute" /><div id="modeleB" style="position:absolute"></div></div><div id="viewA" style="position:absolute; border:0px solid #FF0000 ; opacity:1;z-index:0" ><img id="loaderA" style="position:absolute"/><div id="modeleA" style="position:absolute"></div></div>';
	   
//this.diaporamaPanel.style.overflow = "hidden";
this.viewActive = this.viewB;
this.loaderActif =this.loaderB;
this.afficherContenuDiapos();

/*if(this.idRec){
	window.scrollTo (0, 400 );
	//console.log("idRec"+this.idRec)
//si this.idRec != null;
//recherche la diapos par rapport à son index sur le tableau et l'affiche
//sinon joue automatiquement
for(i=0;i<r.id.length;i++){
	if(r.id[i] == this.idRec){
		this.play=false;
		this.indexDiap  = i;
		this.afficherContenuDiapos();
		break;
	}
}


	
}//fin if this.idRec*/

},
//changement de diapos	
changer2: function() {
	
	 if(this.play){
	
	
	var myEffect = new Fx.Morph(this.loaderActif, {
  //  duration:listeDiapos[indexDiap].dureeApparition*1000,
  duration:2000,
    onComplete: function(){
var info= "Image "+(parseInt(this.indexDiap)+1)+"/"+this.listeDiapos.length
	  $('DiapInfos').set("text",info);	

	if(this.play){
			////console.log(this.play+this.swapDiv);
			//second parametre de delay fixe la cible
var sw = this.swapDiv.delay(5000,this);	
	};

	}.bind(this),
    transition: Fx.Transitions.Sine.easeOut
});
 
myEffect.start({
    'opacity': [0, 1]
    
});
	
	 }else{
	 		 			  $(this.loaderActif).setStyle("opacity",1);	
var info= "Image "+(parseInt(this.indexDiap)+1)+"/"+this.listeDiapos.length
	  $('DiapInfos').set("text",info);	
	 };
},	
	
//changement de diapos	
fonduSortie: function() {


	var myEffect = new Fx.Morph(this.loaderActif, {
  //  duration:listeDiapos[indexDiap].dureeApparition*1000,
  duration:1000,
  onComplete: function(){
	//console.log("fini");
	this.afficherContenuDiaposSuite();
	}.bind(this),

    transition: Fx.Transitions.Sine.easeOut
});
 
myEffect.start({
    'opacity': [1,0]
    
});
},


afficherContenuDiapos: function(id){
	 this.activeLect();
	 if(this.play){
this.fonduSortie();
	 }else{
	 			  $(this.loaderActif).setStyle("opacity",0);	
		  	this.afficherContenuDiaposSuite();
	 };
},

afficherContenuDiaposSuite: function(){
var 	inner = "";
var im="";


	if(this.viewActive == this.viewA){
	this.viewActive = this.viewB;
	this.loaderActif =this.loaderB;

//	$("modeleB").set("html","");
	im =this.listeDiapos[this.indexDiap];

	this.precharger_image(im)	

}else
{	
	//this.viewActive = "viewA";
	//this.loaderActif ="loaderA";

	//$("modeleA").set("html","");
	im =this.listeDiapos[this.indexDiap];

	this.precharger_image(im)

	}	

}


	

//fin de la classe
});


pageListID ="";

function getChaine(){ 
alphabet = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
caracteres = 10;
var longueur = alphabet.length;
var word = "";

for(i = 0; i < caracteres; i++)
{
y = Math.floor(Math.random()*2);
x = Math.floor(Math.random()*longueur);
res = alphabet[x];
	if(y == 0)
	{
	res2 = res.toUpperCase();
	}
	else
	{
	res2 = res.toLowerCase();
	}
word +=res2;
}

if(pageListID.indexOf(word)>=0){
	return getChaine();
}else{
	pageListID+="/"+word;
return word;
}
};



