// JavaScript Document

var thisDiaporama; 	// Cet objet même
var valBase = 1; 	// Lang de base


var a = 0 ; 		// Variable de timer prédefinie 
var b = 0 ; 		// Variable de timer prédefinie 
var alpha = 100;
var nav;  			// Le navigateur;
var larg_bande = 0; // Largeur du badeau d'image
var active_img = 0; // Image active affichée. Pour le mode auto

var larg_max = 516; // largeur maxi du diaporama

function Diaporama(){
	
}

/**********************************************************/
/**********************************************************/
//
// Partie Affichage "public"
//
/**********************************************************/
/**********************************************************/



/**********************************************************/
Diaporama.prototype.calcul_largeur_bande = function(){
	imgs = document.getElementById('list_images').getElementsByTagName("li");	
	for(i = 0; i< imgs.length; i++){
		larg_bande += imgs[i].offsetWidth+4;
	}
	larg_bande -= parseInt(document.getElementById('sel1').offsetWidth) - 2;
}

// Evenement déclencheur
/**********************************************************/
var t1;
Diaporama.prototype.stop_roll = function(){
	clearTimeout(t1);
}

/**********************************************************/
Diaporama.prototype.roll_left = function(){
	
	if(!larg_bande){
		thisDiaporama.calcul_largeur_bande();	
	}
	bande = document.getElementById("list_images");
	if(!bande.style.left) bande.style.left =0;
	if(parseInt(bande.style.left.replace(/[^0-9\-]/g,'')) > -1*larg_bande){
		bande.style.left = (bande.style.left.replace(/[^0-9\-]/g,'') - 20) +"px";
		t1 = setTimeout(myDiaporama.roll_left, 50);
	}
}

//
/**********************************************************/
Diaporama.prototype.roll_right = function(){
	
	bande = document.getElementById("list_images");
	if(!bande.style.left) bande.style.left =0;
	if(parseInt(bande.style.left.replace(/[^0-9\-]/g,'')) < 0){
		bande.style.left = (parseInt(bande.style.left.replace(/[^0-9\-]/g,'')) + 20) +"px";
		t1 = setTimeout(myDiaporama.roll_right, 50);
	}
	
}

//
// Récupérer la liste juste pour la page (pas en admin... )
/**********************************************************/
Diaporama.prototype.get_diaporama_images_visu = function(ajax){
	
	//alert(ajax.responseText);
	// Mettre à jour la suit d'image
	document.getElementById('sel1').innerHTML=unescape(ajax.responseText);
	// On est en mode visualisation on créer les comportements
	tabImg = document.getElementById("sel1").getElementsByTagName("img");
	
	for(i = 0; i < tabImg.length; i++){
	
		tabImg[i].onmouseover = function(){
			this.style.border="1px solid #c00";
			this.style.padding = "0px";
	
		}
		tabImg[i].onmouseout = function(){
			this.style.border="1px solid black";
			this.style.height ="60px";
			this.style.padding = "0";
		}
		tabImg[i].onmouseup = function(){
			
			// Création d'un objet de requete 
			thisDiaporama.show_picture(this.src.replace(/vv_/,""));
			
			if(this.nextSibling.hasChildNodes()){
				document.getElementById('commentaire').innerHTML = this.nextSibling.firstChild.data;
			 }else{
				 document.getElementById('commentaire').innerHTML = "";
			 }
			 
		}
	}
	// Afficher la première image
	if(tabImg.length){
		thisDiaporama.show_picture(tabImg[0].src.replace(/vv_/,""));
		if(document.getElementById('list_images').getElementsByTagName("img")[0].nextSibling.hasChildNodes()){
			document.getElementById('commentaire').innerHTML = document.getElementById('list_images').getElementsByTagName("img")[0].nextSibling.firstChild.data;
		}
	}


}

//
/**********************************************************/
Diaporama.prototype.aff_image_alpha = function(){
	clearTimeout(b);
	if(nav == "ie"){
		if(!document.getElementById('image_diapo_full').style.filter){
			document.getElementById('image_diapo_full').style.filter = "alpha(opacity=0)";
		}
		if(document.getElementById('image_diapo_full').style.filter == "alpha(opacity=100)"){
			return;
		}else if(document.getElementById("grande_image").firstChild.complete){
			document.getElementById("image_diapo_full").style.filter="alpha(opacity="+alpha+")";
			alpha += 20; 
		}

	}else{
		if(!document.getElementById('image_diapo_full').style.opacity){
			document.getElementById('image_diapo_full').style.opacity = 0;
		}
		if(document.getElementById('image_diapo_full').style.opacity == 1){
			return;
		}else if(document.getElementById("grande_image").childNodes[0].complete){
			document.getElementById("image_diapo_full").style.opacity=(alpha/100);
			alpha += 20; 
		}
	}	
	
	b = setTimeout(thisDiaporama.aff_image_alpha,30); 

}

//
// Retaille la boite "gande image" progessivement 
// (à la vitesse "vit") et mettre l'image "im"
/**********************************************************/
Diaporama.prototype.resize_block = function(im, depart_larg, depart_haut, final_larg, final_haut, vit){
	clearTimeout(a);
	coeff_reduc = 10;
	if(final_larg > larg_max){
		coef1 = larg_max/ final_larg;
		final_larg = larg_max;
		final_haut *= coef1;
	}
	document.getElementById("grande_image").style.height = final_haut +"px";
	document.getElementById("grande_image").style.width = final_larg +"px";
	
	thisDiaporama.load_new_image(im);
	
	// calcul de la différence de taille
	haut = parseInt(document.getElementById("grande_image").style.height);
	larg = parseInt(document.getElementById("grande_image").style.width);
	if(isNaN(haut)) haut = 0;
	if(isNaN(larg)) larg = 0;
	diff_haut = final_haut - depart_haut;
	diff_larg = final_larg - depart_larg;
	// Si on a retaillé on charge la nouvelle image
	if((Math.round(diff_haut/coeff_reduc + diff_larg/coeff_reduc) == 0 ) || 
		(Math.abs(final_haut - haut) <= Math.abs(diff_haut/coeff_reduc)   &&  Math.abs(final_larg - larg) <= Math.abs(diff_larg/coeff_reduc))){
		document.getElementById("grande_image").style.height = final_haut +"px";
		document.getElementById("grande_image").style.width = final_larg +"px";
		thisDiaporama.load_new_image(im);
	}else{
		var load_it = 1;
		
		if(Math.abs(final_larg - larg) >= Math.abs(diff_larg/coeff_reduc)){
			load_it = 0;
			document.getElementById("grande_image").style.width = (larg + Math.round(diff_larg/coeff_reduc)) +"px";
		}
		if(Math.abs(final_haut - haut) >= Math.abs(diff_haut/coeff_reduc)){
			load_it = 0;
			document.getElementById("grande_image").style.height = (haut + Math.round(diff_haut/coeff_reduc)) +"px";
		}
		
		
		if(load_it){
			document.getElementById("grande_image").style.height = final_haut +"px";
			document.getElementById("grande_image").style.width = final_larg +"px";
			thisDiaporama.load_new_image(im);
		}else{
			a = setTimeout("thisDiaporama.resize_block(im, "+depart_larg+", "+depart_haut+", "+final_larg+","+final_haut+","+vit+")", vit);
		}
		
	}

}

//
// Récupère la valeur de la hauteur de la fenêtre courante
/**********************************************************/
Diaporama.prototype.getHeight = function() {
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if(document.documentElement && document.documentElement.clientHeight) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  }
  return myHeight;
}

//
// Récupère la valeur de scroll vertical
/**********************************************************/
Diaporama.prototype.getScrollY = function() {
  var scrOfY = 0;
  if( document.body && document.body.scrollTop) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

//
// Arret du mode auto
/**********************************************************/
Diaporama.prototype.stop_auto = function(){
	clearTimeout(t2);
	document.getElementById('auto').disabled = false;
	document.getElementById('auto').className="";
	document.getElementById('stop').disabled = true;
	document.getElementById('stop').className="trans";
}

//
// Mode automatique d'affichage
/**********************************************************/
var t2; // Timer
Diaporama.prototype.affiche_auto = function(){
	active_img++;
	if(active_img >= document.getElementById('list_images').getElementsByTagName("img").length){
		active_img = 0;
	}
	 // trouver la src de l'iamge dont l'index est active_img
	 source = document.getElementById('list_images').getElementsByTagName("img")[active_img].src;
	 if(document.getElementById('list_images').getElementsByTagName("img")[active_img].nextSibling.hasChildNodes()){
	 	document.getElementById('commentaire').innerHTML = document.getElementById('list_images').getElementsByTagName("img")[active_img].nextSibling.firstChild.data;
	 }else{
		 document.getElementById('commentaire').innerHTML = "";
	 }
	 thisDiaporama.show_picture(source.replace(/vv_/,""));
	 
	 t2 = setTimeout(thisDiaporama.affiche_auto, 4500);
	 document.getElementById('auto').disabled = true;
	 document.getElementById('auto').className="trans";
	 document.getElementById('stop').disabled = false;
	 document.getElementById('stop').className="";
 }
 
//
// Affichage de l'image grande 
// Envoi une image au browser dont la source est "ajax"
// Dans l'élément "grande_image"
/**********************************************************/
Diaporama.prototype.affiche_grande_image = function(ajax){
	
	//alert(ajax.responseText);
	im = ajax.responseText;
	dataXML = ajax.responseXML;
	
	if(document.all){
		nav = "ie";
		thisDiaporama.clear_image_alpha();
		thisXml = dataXML.childNodes[1];
	}else{
		nav = "moz";
		thisDiaporama.clear_image_alpha();
		thisXml = dataXML.childNodes[0];
	}
	haut = parseInt(document.getElementById("grande_image").style.height);
	larg = parseInt(document.getElementById("grande_image").style.width);
	if(isNaN(haut)) haut = 0;
	if(isNaN(larg)) larg = 0;
	thisDiaporama.resize_block(im, larg, haut, thisXml.attributes[2].nodeValue, thisXml.attributes[3].nodeValue, 30);
}
 

// On envoie une requete pour connaitre la taille
// et paramétrer la boite de contenu 
/**********************************************************/
Diaporama.prototype.show_picture = function(source){

	source = source.substr((source.lastIndexOf("/")+1));
	method 		= "GET";
	data 		= "";
	url			= "/umaservices/umadiaporama/inc/utilities_diaporama.php?what=image_size&image_name="+source; 
	myAjax 		= new UmaAjax(method, data, url, thisDiaporama.affiche_grande_image);

}

//
/**********************************************************/
Diaporama.prototype.clear_image_alpha = function(){
	clearTimeout(b);
	if(document.getElementById("image_diapo_full")){
		if(nav == "ie"){
			if(!document.getElementById('image_diapo_full').style.filter){
				document.getElementById('image_diapo_full').style.filter = "alpha(opacity=100)";
			}
			if(document.getElementById('image_diapo_full').style.filter == "alpha(opacity=0)"){
				return;
			}else{
				document.getElementById("image_diapo_full").style.filter="alpha(opacity="+alpha+")";
				alpha -= 20;
			}
	
		}else{
			
			if(!document.getElementById('image_diapo_full').style.opacity){
				document.getElementById('image_diapo_full').style.opacity = 1;
			}
			if(document.getElementById('image_diapo_full').style.opacity == 0){
				return;
			}else{
				document.getElementById("image_diapo_full").style.opacity=(alpha/100);
				alpha -= 20; 
			}
		}	
	}
	b = setTimeout(thisDiaporama.clear_image_alpha,30);
}

//
/**********************************************************/
Diaporama.prototype.load_new_image = function(im){
	alpha = 0;
	
	document.getElementById("grande_image").innerHTML = im;
	// Redimentionner l'image à la largeur maxi 
	/*var coef2 = larg_max/document.getElementById("image_diapo_full").offsetWidth;
	if(document.getElementById("image_diapo_full").offsetWidth > larg_max){
		document.getElementById("image_diapo_full").style.width = larg_max+"px";
	}
	document.getElementById("image_diapo_full").style.height = (Math.floor(document.getElementById("image_diapo_full").offsetHeight * coef2))+"px";*/
	alpha = 0;
	if(document.all){
		nav = "ie";
		thisDiaporama.aff_image_alpha();	
	}else{
		nav ="moz";
		thisDiaporama.aff_image_alpha();	
	}

}



/**********************************************************/
/**********************************************************/
//
// Partie Admin
//
/**********************************************************/
/**********************************************************/

//
/**********************************************************/
Diaporama.prototype.clear_diaporama = function(){
	
		document.forms["formDiaporama"].diaporama_nom.value = "Diaporama sans Titre...";
		document.forms["formDiaporama"].diaporama_description.value = "Description du diaporama...";
		// L'id de la page courante
		document.forms["formDiaporama"].diaporama_page_id.value = window.parent.document.forms['formPage'].page_id.value;
		document.getElementById('sel1').innerHTML="Pas d'image...";
		
		
}

//
/**********************************************************/
Diaporama.prototype.send_datas_diaporama = function(ajax){

	//alert(ajax.responseText);
	location.replace("diaporama.php?diaporama_id="+ajax.responseText);
	
}

//
/**********************************************************/
Diaporama.prototype.version = function(val){

	if(val == 1){ 		// Version française
		
		if(thisDiaporama.valBase == 2){	
			// On vient de l'anglais  (valBase = 2)
			// On récupère le contenu du champs de description affiché et le titre affiché dans le titre espagnol
			document.forms['formDiaporama'].diaporama_description_lg1.value = document.forms['formDiaporama'].diaporama_description_aff.value;
			document.forms['formDiaporama'].diaporama_nom_lg1.value = document.forms['formDiaporama'].diaporama_nom_aff.value;
		}
		
		// On met valeur en français
		thisDiaporama.valBase = 1; 
		document.getElementById('dr_fr').className='lang_on';
		document.getElementById('dr_gb').className='lang_of';
		// On met les contenus français dans le champs description affiché et le titre
		document.forms['formDiaporama'].diaporama_nom_aff.value = document.forms['formDiaporama'].diaporama_nom.value;
		document.forms['formDiaporama'].diaporama_description_aff.value = document.forms['formDiaporama'].diaporama_description.value;


	}else if(val == 2){	// Version anglaise
	
		if(thisDiaporama.valBase == 1){	
			// On vient du français  (valBase = 1)
			// On récupère le contenu dans la description et le titre dans le titre anglais
			document.forms['formDiaporama'].diaporama_description.value = document.forms['formDiaporama'].diaporama_description_aff.value;
			document.forms['formDiaporama'].diaporama_nom.value = document.forms['formDiaporama'].diaporama_nom_aff.value;
			
		}
		
		// On met valeur en français
		thisDiaporama.valBase = 2; 
		document.getElementById('dr_fr').className='lang_of';
		document.getElementById('dr_gb').className='lang_on';
		// On met les contenus anglais dans le champs description affiché et le titre
		document.forms['formDiaporama'].diaporama_nom_aff.value = document.forms['formDiaporama'].diaporama_nom_lg1.value;
		document.forms['formDiaporama'].diaporama_description_aff.value = document.forms['formDiaporama'].diaporama_description_lg1.value;
		
	}
	
}



//
/**********************************************************/
Diaporama.prototype.get_diaporama = function(ajax){
	
	//alert( ajax.responseText);
	// Charger le formulaire
	myForm 		= new UmaForm;
	myForm.form_set_content(ajax.responseXML, "formDiaporama");
	
	// Mettre à jour les autres éléments
	document.getElementById('supprime_btn').className="visible";
	
	document.forms["formDiaporama"].MM_insert.value = "update";
	document.forms["formDiaporama"].submit_btn.value = "Mettre à jour";
	document.getElementById('ajout_image').className = 'visible';	
	document.getElementById('gestion_images').className = 'visible';
	
	thisDiaporama.version(1);
	thisDiaporama.get_diaporama_images();
	// Boucle pour sortir les participants...


}

//
/**********************************************************/
Diaporama.prototype.get_diaporama_images = function(ajax){
	
	//if(ajax) alert(ajax.responseText);
	
	// Création d'un objet de requete 
	method 		= "GET";
	data 		= "";
	url			= "/umaservices/umadiaporama/inc/utilities_diaporama.php?what=images_diaporama&diaporama_id="+document.forms["formDiaporama"].diaporama_id.value; 
	myAjax 		= new UmaAjax(method, data, url, thisDiaporama.affiche_liste_images);

}

//
/**********************************************************/
Diaporama.prototype.affiche_liste_images = function(ajax){

	// Mettre à jour la suite d'image
	//alert(ajax.responseText);
	document.getElementById('sel1').innerHTML=unescape(ajax.responseText);
	
	myTree1 = new MovableList();
	myTree1.treeId = "list_images";
	myTree1.init();

}

//
/**********************************************************/
Diaporama.prototype.get_image_infos = function(image){

	document.getElementById('image_ref').innerHTML = image.innerHTML;
	document.getElementById('formDiaporama').image_prop_id.value = image.childNodes[0].getAttribute("id");
	if(image.childNodes[1].innerHTML){
		document.getElementById('formDiaporama').image_prop_description.value = image.childNodes[1].innerHTML;
	}else{
		document.getElementById('formDiaporama').image_prop_description.value = "";
	}
	
}

//
/**********************************************************/
Diaporama.prototype.add_image = function(image_name, image_code, diaporama_id, image_description){

	// Création d'un objet de requete 
	method 		= "GET";
	data 		= "";
	// Les ../ sont important car on vient de l'iframe de la page 
	url			= "/umaservices/umadiaporama/inc/utilities_diaporama.php?what=add_image&image_diaporama_id="+diaporama_id+"&image_name="+image_name+"&image_code="+image_code+"&image_description="+image_description; 
	myAjax		= new UmaAjax(method, data, url, thisDiaporama.affiche_liste_images);

}

// Initialisation du process
/**********************************************************/
Diaporama.prototype.init = function(){
	
	thisDiaporama = this;
	
	// Fonction de déclenchement des événements
	/**********************************************************/
	
	// Vérifier qu'on est en mode edition et que les boutons existent..
	if(document.getElementById("submit_btn")){
		document.getElementById("submit_btn").onclick = function(){
			
			//remettre les variables de langues à jours
			if(thisDiaporama.valBase == 2){
				document.forms['formDiaporama'].diaporama_description_lg1.value = document.forms['formDiaporama'].diaporama_description_aff.value;
				document.forms['formDiaporama'].diaporama_nom_lg1.value = document.forms['formDiaporama'].diaporama_nom_aff.value;
			}else{
				document.forms['formDiaporama'].diaporama_description.value = document.forms['formDiaporama'].diaporama_description_aff.value;
				document.forms['formDiaporama'].diaporama_nom.value = document.forms['formDiaporama'].diaporama_nom_aff.value;
			}
				
			document.forms['formDiaporama'].diaporama_statut.value = "1";
	
			// Création d'un objet de requete 
			method 		= "POST";
			myForm 		= new UmaForm;
			data 		= myForm.form_get_content("formDiaporama");
			url			= "/umaservices/umadiaporama/inc/utilities_diaporama.php?what=update_diaporama"; 
			myAjax 		= new UmaAjax(method, data, url, thisDiaporama.send_datas_diaporama);
			
		}	
	
		document.getElementById("submit_att_btn").onclick = function(){
			
			//remettre les variables de langues à jours
			if(thisDiaporama.valBase == 2){
				document.forms['formDiaporama'].diaporama_description_lg1.value = document.forms['formDiaporama'].diaporama_description_aff.value;
				document.forms['formDiaporama'].diaporama_nom_lg1.value = document.forms['formDiaporama'].diaporama_nom_aff.value;
			}else{
				document.forms['formDiaporama'].diaporama_description.value = document.forms['formDiaporama'].diaporama_description_aff.value;
				document.forms['formDiaporama'].diaporama_nom.value = document.forms['formDiaporama'].diaporama_nom_aff.value;
			}
			
			document.forms['formDiaporama'].diaporama_statut.value = "0";
	
			// Création d'un objet de requete 
			method 		= "POST";
			myForm 		= new UmaForm;
			data 		= myForm.form_get_content("formDiaporama");
			url			= "/umaservices/umadiaporama/inc/utilities_diaporama.php?what=update_diaporama"; 
			myAjax 		= new UmaAjax(method, data, url, thisDiaporama.send_datas_diaporama);
				
		}
		
		document.getElementById("supprime_btn").onclick = function(){
			
			
			if(confirm("Voulez-vous vraiment supprimer ce diaporama ?")){
				
				document.forms['formDiaporama'].MM_insert.value='delete';
				
				// Création d'un objet de requete 
				method 		= "POST";
				myForm 		= new UmaForm;
				data 		= myForm.form_get_content("formDiaporama");
				url			= "/umaservices/umadiaporama/inc/utilities_diaporama.php?what=update_diaporama"; 
				myAjax 		= new UmaAjax(method, data, url, thisDiaporama.send_datas_diaporama);
			}
			
		}
		
		document.getElementById("fermer_btn").onclick = function(){
	
			window.parent.closeFrame();
			
		}
		
			// Bouton de modification d'image de la listes des images
		document.getElementById("modifier_image_btn").onclick = function(){
	
				
			image_id = document.forms["formDiaporama"].image_prop_id.value;
			image_prop = document.forms["formDiaporama"].image_prop_description.value;
			// Création d'un objet de requete 
			method 		= "GET";
			data 		= "";
			url			= "/umaservices/umadiaporama/inc/utilities_diaporama.php?what=update_image&image_id="+image_id+"&image_prop_description="+image_prop; 
			myAjax 		= new UmaAjax(method, data, url, thisDiaporama.get_diaporama_images);
			
				
		}
		
		
		// Bouton de suppression d'image de la listes des images
		document.getElementById("supprimer_image_btn").onclick = function(){
	
			if(confirm("Etes-vous sûr de vouloir supprimer cette image ?")){
				
				diaporama_id = document.forms["formDiaporama"].diaporama_id.value;
				image_id = document.forms["formDiaporama"].image_prop_id.value;
				// Création d'un objet de requete 
				method 		= "GET";
				data 		= "";
				url			= "/umaservices/umadiaporama/inc/utilities_diaporama.php?what=sup_image&diaporama_id="+diaporama_id+"&image_id="+image_id; 
				myAjax 		= new UmaAjax(method, data, url, thisDiaporama.get_diaporama_images);
			}
				
		}
		
		// Appel à la fonction de chargement de la page (dans diaporama.php)
		charge_diaporama();
		
			
		
		
	}
	
	
	// Si on est en mode visualisation on créer l'affichage des vignettes
	if(document.getElementById("grande_image")){
		startDiapo(); 	// Dans diapo_include.php
	}
	
	// Fin d'initialisation
}


/********************************************************************************/
/********************************************************************************/
//
// FIN
//
/********************************************************************************/
/********************************************************************************/

// Déclencher les événement au chargement de la page
/********************************************************************************/

function newContext(){
	myDiaporama = new Diaporama();
	myDiaporama.init();
}

if (window.addEventListener) {
	window.addEventListener('load', newContext, false);
}else if (window.attachEvent) {
	window.attachEvent('onload', newContext);	
}

