window.onload=init_menu;

function init_menu(){
hidesousactions();
hide_rub();
hide_sousrub();
hide_pic();
hide_photo();
hide_all('gestion_sousrub');
}

var xhr = null;
// Fonction de creation de l'objet XMLHttpRequest qui resservira pour chaques fonctions AJAX
function getXhr()
 {
  if(window.XMLHttpRequest) xhr = new XMLHttpRequest(); 
  else if(window.ActiveXObject)
   {  
    try
     {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
     }
    catch (e)
     {
      xhr = new ActiveXObject("Microsoft.XMLHTTP");
     }
   }
  else 
   { 
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest, veuillez le mettre à jour"); 
    xhr = false; 
   } 
 }

function getImg(idrub,type){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		 document.getElementById('vignettes').innerHTML = xhr.responseText;
		 if(type=="admin"){
			document.forms['ajout_photo'].action = 'index.php?idrub='+idrub;
			document.getElementById('submit_srub').onclick=function() { addsousrub(idrub);};
			document.getElementById('supp_rub').onclick=function() { supprub(idrub);};
			document.getElementById('supp_srub').onclick=function() { supprub(idrub);};
			document.getElementById('mod_rub_sub').onclick=function() { modrub(idrub);};
			document.getElementById('mod_srub_sub').onclick=function() { modsrub(idrub);};
		 }
		 return myLightbox = new Lightbox();

		}
	}
	
	xhr.open("POST",'get_image.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("idrub="+idrub+"&type="+type);
}
function hidenshow(elementid){
	var eltVisible=document.getElementById(elementid).style.visibility;
	if(eltVisible == 'visible'){
		hide_all(elementid);
	}
	else{
		show_all(elementid);
	}
}

function hide_all(elementid){
	window.document.getElementById(elementid).style.visibility = 'hidden';
	window.document.getElementById(elementid).style.display = 'none';
}

function show_all(elementid){
	window.document.getElementById(elementid).style.visibility = 'visible';
	window.document.getElementById(elementid).style.display = 'block';
}

function img_selec(idphoto,idrub,type){
	getInfo(idphoto,idrub);
	if(type=="admin"){
	document.getElementById('supp_img_sub').onclick=function() { suppPic(idphoto,idrub);};
	}
	var thisid='photo'+idphoto;
	document.getElementById('photo'+idphoto).style.backgroundColor = '#FFFFFF';
	var all_pic=document.getElementById('list_photo').getElementsByTagName('div');
	var nb_img=all_pic.length;
	for(var i=0;i<nb_img;i++){
		if(all_pic[i].id.substr(0, 5)=='photo' && all_pic[i].id != thisid){
			all_pic[i].style.backgroundColor = '#d9f1fe';
		}
	}
}

function show_rub(){
	show_all('mod_rub_title');
	show_all('supp_rub_title');
}

function show_sousrub(){
	show_all('mod_srub_title');
	show_all('supp_srub_title');
}

function show_photo(){
	show_all('mod_pic_title');
	show_all('supp_pic_title');
}

function hide_rub(){
	hide_all('mod_rub_title');
	hide_all('supp_rub_title');
}

function hide_sousrub(){
	hide_all('mod_srub_title');
	hide_all('supp_srub_title');
}

function hide_pic(){
	hide_all('mod_pic_title');
	hide_all('supp_pic_title');
}

function hide_photo(){
	hide_all('gestion_photo');
}

function addrub(){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		 
			if(xhr.responseText!="Veuillez saisir un nom de rubrique."){
				reqMenuAd();
				init_menu();
				clearInputTxt();
				
			}
			else{
			alert(xhr.responseText);
			}
		}
	}
	
	xhr.open("POST",'addrub.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("nom_rub="+document.getElementById('nom_rub').value);
}

function reqMenu(){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		 document.getElementById('listrub').innerHTML = xhr.responseText;
		}
	}
	
	xhr.open("POST",'req_menu_noad.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send(null);
}

function reqMenuAd(){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		 document.getElementById('listrub').innerHTML = xhr.responseText;
		}
	}
	
	xhr.open("POST",'req_menu.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send(null);
}

function clearInputTxt(){	
	document.getElementById('suppRubTxt').innerHTML="";
	document.getElementById('suppsRubTxt').innerHTML="";
	document.getElementById('nom_rub').value="";
	document.getElementById('nom_sousrub').value="";
	document.getElementById('img_browse').value="";
	document.getElementById('titre').value="";
	document.getElementById('comment').value="";
	document.getElementById('modif_titre').value="";
	document.getElementById('modif_comment').value="";
	document.getElementById('mod_rub').value="";
	document.getElementById('mod_srub').value="";

}	

function addsousrub(idrub){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		 reqMenuAd();
		 clearInputTxt();
		}
	}
	
	xhr.open("POST",'addrub.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("nom_rub="+document.getElementById('nom_sousrub').value+"&id_rub="+idrub);
}

function nameRub(nameRub,folder){
	var sub='';
	if(folder=='sousrub'){
		sub='s';
	}
	document.getElementById('supp'+sub+'RubTxt').innerHTML=nameRub;
	document.getElementById('mod_'+sub+'rub').value=nameRub;
}

function supprub(idrub){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		  if(xhr.responseText != "Une erreur est survenue."){
				document.getElementById('vignettes').innerHTML = "";
				reqMenuAd();
				init_menu();
				clearInputTxt();

			}
			else{
				alert(xhr.responseText);
			}
		}
	}
	
	xhr.open("POST",'supprub.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("idrub="+idrub);
}

function modrub(idrub){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		 if(xhr.responseText.length<=3){
				reqMenuAd();
				init_menu();
				clearInputTxt();
			}
			else{
				alert(xhr.responseText);
			}
		}
	}
	xhr.open("POST",'modifrub.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("new_name="+document.getElementById('mod_rub').value+"&idrub="+idrub); 
}

function modsrub(idrub){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		 if(xhr.responseText.length<=3){
				reqMenuAd();
				init_menu();
				clearInputTxt();
			}
			else{
				alert(xhr.responseText);
			}
		}
	}
	
	xhr.open("POST",'modifrub.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("new_name="+document.getElementById('mod_srub').value+"&idrub="+idrub);
}

function getInfo(idphoto,idrub){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		 document.getElementById('modimg').innerHTML = xhr.responseText;
		}
	}
	
	xhr.open("POST",'getInfoPic.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("idphoto="+idphoto+"&idrub="+idrub);
}

function modpic(idphoto,idrub){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		  if(xhr.responseText.substr(0, 2) != "ok"){
		  alert(xhr.responseText);
		  }
		  else{
		  getImg(idrub);
		  clearInputTxt();
		  }
		}
	}
	xhr.open("POST",'modifpic.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("new_titre="+document.getElementById('modif_titre').value+"&new_comm="+document.getElementById('modif_comment').value+"&idphoto="+idphoto);
}

function suppPic(idphoto,idrub){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		 getImg(idrub);
		 clearInputTxt();
		}
	}
	xhr.open("POST",'supppic.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("idphoto="+idphoto);
}

function isRed(idName){
	document.getElementById(idName).style.color = '#ff0000';
	for(var i=0;i<=document.getElementsByName('rubrique').length;i++){
		if(document.getElementsByName('rubrique')[i].id != idName){
			document.getElementsByName('rubrique')[i].style.color = '#003366';
			initColorSRub();
		}
	}
	
}

function isFadeRed(idName){
	document.getElementById(idName).style.color = '#ff0000';
	for(var i=0;i<=document.getElementsByName('sousrub').length;i++){
		if(document.getElementsByName('sousrub')[i].id != idName){
			document.getElementsByName('sousrub')[i].style.color = '#003366';
			initColorRub();
		}
	}
	
}

function initColorSRub(){
	for(var j=0;j<=document.getElementsByName('sousrub').length;j++){
		document.getElementsByName('sousrub')[j].style.color = '#003366';
	}
}

function initColorRub(){
	for(var j=0;j<=document.getElementsByName('rubrique').length;j++){
		document.getElementsByName('rubrique')[j].style.color = '#003366';
	}
}

function hidesousactions(){
	var all_span=document.getElementById('admin').getElementsByTagName('span');
	
	for(var i=0;i<all_span.length;i++){
		hide_all(all_span[i].id);
	}
	
}

function search_photo(){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		  document.getElementById('vignettes').innerHTML = xhr.responseText;
		   return myLightbox = new Lightbox();
		}
	}
	
	xhr.open("POST",'search_image.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("motscle="+document.getElementById('motscle').value);
}

function auth(){
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
		var rep=xhr.responseText;
		if(rep.substring(0,2)=="<s"){
			document.getElementById('admin').innerHTML = xhr.responseText;
			document.goToAdmin.submit(); 
		}

		  document.getElementById('admin').innerHTML = xhr.responseText; 
		}
	}
	
	xhr.open("POST",'auth.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("login="+document.getElementById('login').value+"&pass="+document.getElementById('pass').value);
}

