// JavaScript Document
//Variables globales
//------------------
	var timeout;	
	var page;
	var projet;
	var img;
	var modele;
//	
	function verif(){
	
		var url = 'validation.php';
		var parms = '';
		for (var i = 1; i <= $('item1').value; i++)
		{
			if ($('libelle_fr'+i).value != '' && $('del'+i).checked == false )
				{
					parms = parms+'libelle_fr'+i+'='+$('libelle_fr'+i).value+'&ordre'+i+'='+$('ordre'+i).value+'&page'+i+'='+$('page'+i).value+'&item'+i+'='+$('item'+i).value+'&libelle_en'+i+'='+$('libelle_en'+i).value+'&';
				}
		}
		/*var parms = 'libelle1=coucou';*/
		/*alert($('ordre1').value+'/'+document.getElementById('ordre1').value);*/
		/*alert(parms);*/
		new Ajax.Request(url,{  postBody : parms, 
								method: 'post',
								onComplete:retour_menu1
								});		
		
			function retour_menu1(xhr)
			{
				if (xhr.status == 200)
				{
					if (xhr.responseText == 0)
					{
						var texte='Les modifications ont &eacute;t&eacute; prises en compte';
						dialogue(texte);
						affichage('admin','administration.php');
					}
					else
					{
						var texte='Un probl&egrave;me est survenu lors de la mise &agrave; jour';
						dialogue(texte+xhr.responseText);			
					};
				}
				else
				{
					$('resultat').innerHTML = xhr.status
								+ ' : ' + xhr.statusText;
				}
			}

	}

	function verif_projet(i){
		var ret_sub = true;	

		// Si il y a un fichier à telecharger
		if ($('fichier'+i).value != ''){
			var chemin = $('fichier'+i).value;
			var nom_fic = chemin.substring(chemin.lastIndexOf("\\"));
			$('myform'+i).submit();
		}
		else
		{
			var nom_fic = '';
			}
		
		if (ret_sub)
		{
			var url = 'validation_projet.php';
			//var j = i+1;
			parms = 'titre_fr='+$('titre_fr'+i).value+'&id_projet='+i+'&fichier='+nom_fic+'&libelle_fr='+$('libelle_fr'+i).value+'&template='+$('template'+i).value+'&projval='+$('projval'+i).value+'&idphoto='+$('idphoto'+i).value+'&titre_en='+$('titre_en'+i).value+'&libelle_en='+$('libelle_en'+i).value;
			/*var parms = 'libelle1=coucou';*/
			/*alert($('titre'+i).value+'/'+$('libelle'+i).value+'/'+$('template'+i).value+'/'+$('fichier'+i).value+'identifiant='+i);*/
			//alert(parms);
			if ($('titre_fr'+i).value == '' || $('titre_en'+i).value == '' || $('template'+i).value.substring(0,1) != 'm' )
			{
				alert ('Vous avez oublié certains champs nécessaires');
			}
			else
			{
				new Ajax.Request(url,{  postBody : parms, 
										method: 'post',
										onComplete:gestionReponse
										});		
			}
		}
		else
		{
			alert ('ouyeouyeouye');
			}
	}
	
	function gestionReponse(xhr)
	{
		if (xhr.status == 200)
		{
			//dialogue(xhr.responseText);	
			var code_retour = xhr.responseXML.documentElement.getElementsByTagName("code_retour")[0].firstChild.nodeValue;		
			if (code_retour == 0)
			{
				img = xhr.responseXML.documentElement.getElementsByTagName("image")[0].firstChild.nodeValue;
				projet = xhr.responseXML.documentElement.getElementsByTagName("id_projet")[0].firstChild.nodeValue;
				modele = xhr.responseXML.documentElement.getElementsByTagName("modele")[0].firstChild.nodeValue;				
				var texte='Les modifications ont &eacute;t&eacute; prises en compte'//+code_retour+img+projet;
				//dialogue(texte);
				page = 'PROJ';
				openInfoDialog();
				/*Dialog.alert(texte, {windowParameters: {className: "alphacube", width:300, height:100}, 
								okLabel: "Fermer",
		                        ok:function() {	
												affichage('img'+projet,'image.php?image='+img);
												affichage('parametrer'+projet,'param.php?valid=1&template='+modele+'&num_projet='+projet);
												return true;}});	*/
			}
			else
			{
				var texte='Un probl&egrave;me est survenu lors de la mise &agrave; jour'+xhr.responseText;
				dialogue(texte);			
			};
		}
		else
		{
			$('resultat').innerHTML = xhr.status
						+ ' : ' + xhr.statusText;
		}
	}

	
//
// Validation des informations de la reference ajoutee ou modifiee
//------------------------------------------------------------------
function openInfoDialogModification() 
{ 
	//Dialog.info("Test of info panel, it will close <br>in 3s ...", {width:250, height:100, showProgress: true});
	Dialog.info("Modification en cours ...",{className: "alphacube", width:250, height:100, showProgress:true});
	timeout=3; 
	// Appelle la fonction infoTimeout toutes les 1000 millisecondes donc 1 seconde
	//setTimeoutModification(infoTimeoutModification, 1000) 
} 

function infoTimeoutModification() 
{ 
	timeout--; 
	if (timeout >= 0) 
	{ 
		if (timeout == 0) 
		{
			Dialog.setInfoMessage("Modification effectu&eacute;e");
		}
		setTimeout(infoTimeoutModification, 1000) 
	} 
	else
	{ 
		Dialog.closeInfo();
	} 
}

function modif_rubrique_accueil(identifiant){
	
		openInfoDialogModification();
		
		var url = 'modification_accueil.php';
		
		var parms = 'identifiant='+identifiant+'&titre_rubrique='+$('titre_rubrique'+identifiant).value+'&texte_rubrique='+$('texte_rubrique'+identifiant).value;
		//alert(parms);
		new Ajax.Request(url,{  
								parameters : parms, 
								method: 'get',
								onComplete:ReponseRef
								});
	}

function modif_galerie_commentaire(identifiant){
	
		openInfoDialogModification();
		
		var url = 'modification_commentaire_photo.php';
		//modale('images/loading.gif');
		var parms = 'identifiant='+identifiant+'&commentaire='+$('commentaire').value;
		//alert(parms);
		new Ajax.Request(url,{  
								parameters : parms, 
								method: 'get',
								onComplete:ReponseRef
									});
	}

	//----------------------------------------------
	
function ReponseRef(xhr)
	{
		//alert (xhr.status);
		if (xhr.status == 200)
		{
			//dialogue(xhr.responseText);	
			//alert (xhr.responseText);
			var code_retour = xhr.responseXML.documentElement.getElementsByTagName("code_retour")[0].firstChild.nodeValue;	
			//alert (code_retour);
			if (code_retour == 0)
			{
				//var img1 = xhr.responseXML.documentElement.getElementsByTagName("image1")[0].firstChild.nodeValue;
				//var img2 = xhr.responseXML.documentElement.getElementsByTagName("image2")[0].firstChild.nodeValue;				
				var rep = xhr.responseXML.documentElement.getElementsByTagName("rep")[0].firstChild.nodeValue;								
				//var image1 = xhr.responseXML.documentElement.getElementsByTagName("image1")[0].firstChild.nodeValue;												
				//var texte='Les modifications ont &eacute;t&eacute; prises en compte'+code_retour+img1+img2+rep;
								
				//if (image1 == 'S')
				//{
				//	affichage('admin','reference.php');
					//dialogue(rep);
					infoTimeoutModification();
				//}
				//else
				//{
				//	page = 'reference.php';
				//	openInfoDialog();
				//}	
			}
			else
			{
				var texte='Un probl&egrave;me est survenu'+xhr.responseText;
				dialogue(texte);			
			}
		}
		else
		{
			$('resultat').innerHTML = xhr.status
						+ ' : ' + xhr.statusText;
		}
	}	

// Fonction appelée pour ajouter un lien
//  --> Appelée dans la page lien_ajout.php		
function ajout_lien()
	{
		openInfoDialogModification();

		var url = 'insert_lien.php';
		var parms = 'lien='+$('lien').value+'&libelle='+$('libelle').value+'&mail='+$('mail').value+'&descr='+$('descr').value;
		//alert(parms);
		new Ajax.Request(url,{  
								parameters : parms, 
								method: 'get',
								onComplete:ReponseLien
								});
	}
	
// Fonction appelée pour modifier un lien
//  --> Appelée dans la page lien_ajout.php		
function modif_lien(identifiant)
	{
		openInfoDialogModification();
		
		var url = 'lien_modif.php';
		var parms = 'identifiant='+identifiant+'&lien='+$('lien').value+'&libelle='+$('libelle').value+'&mail='+$('mail').value+'&descr='+$('descr').value;
		//alert(parms);
		new Ajax.Request(url,{  
								parameters : parms, 
								method: 'get',
								onComplete:ReponseLien
								});
	}
		
	function sup_lien(identifiant)
	{
	
		var url = 'lien_suppression.php';
		
		var parms = 'identifiant='+identifiant;
		//alert(parms);
		Dialog.confirm("Supprimer le lien?",
			{windowParameters: {className: "alphacube", width:300},
			 id: "myDialogId",
			 okLabel: "Valider",  
			 cancelLabel: "Annuler",
			 ok:function(win){
			 		Dialog.closeInfo();
			 		
			 		openInfoDialogModification();
			 		
					new Ajax.Request(url,{  
								parameters : parms, 
								method: 'get',
								onComplete:ReponseLien
								});
							},
			 cancel:function(win){
					//alert('ko');
							}
			});
	}
			
	function ReponseLien(xhr)
	{
		if (xhr.status == 200)
		{
			//dialogue(xhr.responseText);	
			var code_retour = xhr.responseXML.documentElement.getElementsByTagName("code_retour")[0].firstChild.nodeValue;		
			var rep1 = xhr.responseXML.documentElement.getElementsByTagName("rep1")[0].firstChild.nodeValue;		
			if (code_retour == 0)
			{
				/*Dialog.alert("Traitement effectu&eacute;", {windowParameters: {className: "alphacube", width:300, height:100}, 
								okLabel: "Fermer",
		                        ok:function() {	
								
										affichage('admin','liens.php');
										return true;}});	*/
				infoTimeoutModification();
				affichage_simple('admin','liens.php');
			}
			else
			{
				var texte='Un probl&egrave;me est survenu lors de la mise &agrave; jour'+xhr.responseText;
				dialogue(texte);			
			};
		}
		else
		{
			$('resultat').innerHTML = xhr.status
						+ ' : ' + xhr.statusText;
		}
	}		
		
	
	function dialogue(texte){
		Dialog.alert(texte, {windowParameters: {className: "alphacube", width:300, height:100}, 
								okLabel: "Fermer",
		                        ok:function() {return 1;}});
	}
	
	function test_retour(){
	
		var inner = '<table><tr><td>yes</td></tr></table>';
/*		var inner = transport;*/
		//debug($('modal_window_content'))
		var win = new Window('modal', {className: "alphacube", title: "Référence",top:200, left:400,  width:300, height:200, zIndex:150, opacity:1, resizable: false})
		win.getContent().innerHTML = inner;
		win.setDestroyOnClose();
		win.show(true);	
	}
	
	function choix_modele(id,modele){	
		Dialog.alert({url: "choix_modele.php?id_modele="+modele, options: {method: 'get'}}, 
		{windowParameters: {className: "alphacube", width:240}, okLabel: "Choisir",
		ok:function(win){
						 for (var i = 0; i < $('tot').value; i++){
							if ($('m'+i).checked == true){
								$('template'+id).value = $('m'+i).value;
								$('projval'+id).value = 2;								
							}	
						 }
						 return true;
							}});
		}
	
	function confirme(id,modele){	
		Dialog.confirm({url: "choix_modele.php?id_modele="+modele, options: {method: 'get'}},
			{windowParameters: {className: "alphacube", width:300},
			 id: "myDialogId",
			 okLabel: "Valider",  
			 cancelLabel: "Annuler",
			 ok:function(win){
						 for (var i = 0; i < $('tot').value; i++){
							if ($('m'+i).checked == true){
								affichage('parametrer'+id,'param.php?valid=0&template='+$('m'+i).value+'&num_projet='+id);
								$('template'+id).value = $('m'+i).value;
								$('projval'+id).value = 2;
								i = 99;
							}	
						 }
						 return true;
							}
			});
		}

function modale(img)
{
		var inner = '<table><tr><td><img src='+img+' height=165 width=125 border="0"></td></tr></table>';
		//debug($('modal_window_content'))
		var win = new Window('modal_window', {className: "alphacube", 
							 				  title: "Référence",top:200, left:400,  width:300, height:200, zIndex:150,
											  opacity:1, 
											  resizable: false})
		win.getContent().innerHTML = inner;
		win.setDestroyOnClose();
		win.show(true);	

	}
	
function modale_url(rep)
{
		//alert ("livres/"+rep+"/obj_swf_"+rep+".php");
		var win = new Window({className: "alphacube", 
							 				  title: "Choix de l'image",
							 				  top:50, left:250,  width:950, height:650, zIndex:150,
											  url: "livres/"+rep+"/obj_swf_"+rep+".php",
											  resizable: false
											  })
		win.show(true);		
	}	

// Fonction appelée par l'AS3 de upload.swf pour mettre à jour la photo nouvellement chargée
// Photo des rubriques administrables de l'accueil
function maj_photo(div_id,picture)
{
	//alert (div_id+"/"+picture);
	elementHTML = document.getElementById(div_id);
	var url = "maj_photo_accueil.php?img="+picture;
	o_options = new Object();
	o_options = {method: 'get'};
	var laRequete = new Ajax.Updater(elementHTML,url,o_options); 
}

function maj_galerie(famille)
{
	//alert ("maj_galerie");
	elementHTML = document.getElementById('galerie');
	var url = "galerie_reference.php?famille="+famille;
	o_options = new Object();
	o_options = {method: 'get'};
	var laRequete = new Ajax.Updater(elementHTML,url,o_options); 
}

function maj_gde_photo(picture)
{
	var url = "maj_photo.php?img="+picture;
	o_options = new Object();
	o_options = {method: 'get'};
	var laRequete = new Ajax.Updater($('image'),url,o_options); 
}

function maj_pte_photo(picture,num)
{
	var url = "maj_pte_photo.php?img="+picture+"&identifiant="+num;
	o_options = new Object();
	o_options = {method: 'get'};
	var laRequete = new Ajax.Updater($('image'+num),url,o_options); 
}

function teste()
{
	alert ("comprends pas");
}

function fleche(identifiant,maxi)
{
	for (var i = 0; i <= maxi; i++)
		{
			if ('selection'+identifiant != 'selection'+i)
				{
					$('selection'+i).className = "hidden";
				}
		}
	if (identifiant != 9999)
	{
		$('selection'+identifiant).className = "show";
	}
}

	function sup_galerie(identifiant)
	{
	
		var url = 'galerie_suppression.php';
		
		var parms = 'identifiant='+identifiant;
		//alert(parms);
		Dialog.confirm("Supprimer la r&eacute;f&eacute;rence de la galerie ?",
			{windowParameters: {className: "alphacube", width:300},
			 id: "myDialogId",
			 okLabel: "Valider",  
			 cancelLabel: "Annuler",
			 ok:function(win){
			 		Dialog.closeInfo();
			 		openInfoDialogModification();
					new Ajax.Request(url,{  
								parameters : parms, 
								method: 'get',
								onComplete:ReponseGalerie
								});
							},
			 cancel:function(win){
					//alert('ko');
							}
			});
	}
			
	function ReponseGalerie(xhr)
	{
		if (xhr.status == 200)
		{
			//dialogue(xhr.responseText);	
			var code_retour = xhr.responseXML.documentElement.getElementsByTagName("code_retour")[0].firstChild.nodeValue;		
			var rep1 = xhr.responseXML.documentElement.getElementsByTagName("rep1")[0].firstChild.nodeValue;		
			var famille = xhr.responseXML.documentElement.getElementsByTagName("famille")[0].firstChild.nodeValue;					
			if (code_retour == 0)
			{
				/*Dialog.alert("Traitement effectu&eacute;", {windowParameters: {className: "alphacube", width:300, height:100}, 
								okLabel: "Fermer",
		                        ok:function() {	
								
										affichage('galerie','galerie_reference.php?famille='+famille);
										return true;}});	*/
				affichage_simple('galerie','galerie_reference.php?famille='+famille);						
				infoTimeoutModification();
			}
			else
			{
				var texte='Un probl&egrave;me est survenu lors de la mise &agrave; jour'+xhr.responseText;
				dialogue(texte);			
			};
		}
		else
		{
			$('resultat').innerHTML = xhr.status
						+ ' : ' + xhr.statusText;
		}
	}		

function affichage_simple(div_id,url)
{
	//Dialog.info("Chargement",{windowParameters: {className: "alphacube", width:250, height:100, showProgress:true}});

	elementHTML = document.getElementById(div_id);
	//var url = url;
	o_options = new Object();
	//o_options = {method: 'get',onComplete:function(){Dialog.closeInfo();}};
	o_options = {method : 'get', evalScripts : true};
	var laRequete = new Ajax.Updater($(''+div_id+''),url,o_options); 
}

function affichage(div_id,url)
{
	//Dialog.info("Chargement",{className: "alphacube", width:250, height:100, showProgress:true});
	openInfoDialog(); 
	//elementHTML = document.getElementById(div_id);
	//var url = url;
	//o_options = new Object();
	//o_options = {method: 'get',onComplete:function(){Dialog.closeInfo();}};
	//o_options = {method: 'get'};
	var laRequete = new Ajax.Updater($(''+div_id+''),url,
																						{method: 'get',
																						 onComplete:function()
																	 						{
																 								timeout--; 
																								if (timeout >0) 
																								{ 
																									//Dialog.setInfoMessage("Test of info panel, it will close <br>in " + timeout + "s ...");
																									setTimeout(infoTimeout, 1000) 
																								} 
																								else
																								{ 
																									Dialog.closeInfo();
																								} 
																	 						}
																						}); 
	//Dialog.closeInfo();
	
}

function openInfoDialog() 
{ 
	//Dialog.info("Test of info panel, it will close <br>in 3s ...", {width:250, height:100, showProgress: true});
	Dialog.info("Chargement en cours ...",{className: "alphacube", width:250, height:100, showProgress:true});
	timeout=2; 
	// Appelle la fonction infoTimeout toutes les 1000 millisecondes donc 1 seconde
	//setTimeout(infoTimeout, 1000) 
} 

function infoTimeout() 
{ 
	timeout--; 
	if (timeout >= 0) 
	{ 
		//Dialog.setInfoMessage("Test of info panel, it will close <br>in " + timeout + "s ...");
		setTimeout(infoTimeout, 1000);
	} 
	else
	{ 
		Dialog.closeInfo();
	} 
}

function change_source(numero)
{
	//var picture = img.substr(1);
	//var numero  = img.substr(0,1);
	var tab_menu = ['accueil','offre','mariage','portrait','professionnel','espace','contact','lien'];
	$('galerie_menu').className = "hidden";
	for (var i = 0; i <= 7; i++)
		{
			if (numero == i)
			{
				$('menu'+numero).src = 'images/menu_'+tab_menu[numero]+'_det.png';
			}
			else
			{
				$('menu'+i).src = 'images/menu_'+tab_menu[i]+'.png';	
			}
		}
}

function change_source_ssmenu(numero)
{
	var tab_menu = ['maternite','enfant','famille'];
	for (var i = 0; i <= 2; i++)
		{
			if (numero == i)
			{
				$('ssmenu'+numero).src = 'images/menu_'+tab_menu[numero]+'_det.png';
			}
			else
			{
				$('ssmenu'+i).src = 'images/menu_'+tab_menu[i]+'.png';	
			}
		}
}

function affiche_ssmenu()
{
	$('galerie_menu').className = "show";	
	change_source_ssmenu(0);
}

function chge_style(id,max)
{
	for (var i = 0; i <= max; i++)
	{
		if ( i == id)
		{
			$('pageid'+id).className = "gras";
		}
		else
		{
			$('pageid'+i).className = "simple";
		}
	}
}

function open_window(repertoire,largeur,hauteur)
{
	window.open("http://www.florencerossard.com/livres/"+repertoire+"/index.html",
       "NomDescriptifDeLaFenetre",
       "top=50,left=50,width="+largeur+",height="+hauteur+",resizable=no,scrollbars=no,status=no");
	
}

function MajHauteur(hauteur,couleur)
	{
		//alert($('affichage_galerie').offsetHeight);
		//$('menu').style.height = $('description').offsetHeight;
		//$('intro').style.height = 350 + $('description').offsetHeight;		
		$('admin').style.height = hauteur;
		$('admin').setStyle({backgroundColor: '#'+couleur});
	}

