/***********************************************************
Création des balises html5 dans ie
***********************************************************/

document.createElement("header");
document.createElement("section");
document.createElement("nav");
document.createElement("footer");

/***********************************************************
Placement et/ou redimentionnement des objets
***********************************************************/

function moveElements(){
	var pageHeight = document.documentElement.clientHeight;
	var pageWidth = document.documentElement.clientWidth;
	var coef;
	
	if(pageHeight > pageWidth){
		coef = pageHeight/1600;
		
		window.document.getElementById("imgBackground").style.height = pageHeight <= 1600 ? pageHeight + "px" : "1600px";
		window.document.getElementById("imgBackground").style.width = 2500*coef + "px";
		window.document.getElementById("imgBackground").style.paddingTop = "0px";
		
	}else{
		coef = pageWidth/2500;
		
		window.document.getElementById("imgBackground").style.width = pageWidth <= 2500 ? pageWidth + "px" : "2500px";
		window.document.getElementById("imgBackground").style.height = Math.round(1600*coef) + "px";
		window.document.getElementById("imgBackground").style.paddingTop = pageHeight-Math.round(1600*coef) + "px";
		
	} // end if
} // end function moveElements

/*****************************************
fonction qui ouvre un sous menu
*****************************************/

var tab_menu = ["agencyMenu", "prestationMenu", "bookMenu"]

function openMenu(id){
	var obj = document.getElementById(tab_menu[id]);
	
	for(i = 0; i< tab_menu.length; i++){
		if(i != id){
			if(document.getElementById(tab_menu[i]).style.display == "block"){
				document.getElementById(tab_menu[i]).style.display = "none";
				
			} // end if	
		}// end if
	} // end for i
	
	if(obj.style.display == "block") {
		obj.style.display = "none";
		
	}else{
		obj.style.display = "block";
		
	} // end if
} // end function openMenu

/*****************************************
Action des boutons en rollOver
*****************************************/

function buttonUp(object){
	var tab_temp = object.src.split(".");
	var tab_length = tab_temp.length;
	var str = "";
	
	if(tab_length > 2){
		for(var i = 0; i < tab_length-2; i++){
			str += tab_temp[i] + ".";
			
		} // end for i
	} // end if
	
	var url = str + tab_temp[tab_length-2] + "_up." + tab_temp[tab_length-1];
	
	object.src = url;
	
} // end function buttonUp

/*****************************************
Action des boutons en rollOut
*****************************************/

function buttonNormal(object){
	var tab_temp = object.src.split(".");
	var tab_length = tab_temp.length;
	var str = "";
	
	if(tab_length > 2){
		for(var i = 0; i < tab_length-2; i++){
			str += tab_temp[i] + ".";
			
		} // end for i
	} // end if
	
	var url = str + tab_temp[tab_length-2].substr(0, tab_temp[tab_length-2].length-3) + "." + tab_temp[tab_length-1];
	
	object.src = url;
	
} // end function buttonNormal

/*****************************************
Affichafe / désaffichage de la galerie
*****************************************/

function masqueMenu(){
	var galery = document.getElementById("galery");
	var bouton = document.getElementById("btGalery");
	
	if(galery.style.bottom == "" || galery.style.bottom == "35px"){
		galery.style.bottom = "-165px";
		bouton.innerHTML = "Afficher";
		
	}else{
		galery.style.bottom = "35px";
		bouton.innerHTML = "Masquer";
		
	} // end if
} // end function masqueMenu

/*****************************************
Initialisation d'un champ de texte
*****************************************/

function deleteValue(obj, str){
	if(obj.value == str){
		obj.value = "";
	
	} // end if
} // end function deleteValue

/*****************************************
Si le champs de texte est vide on le
remplit avec sa valeur par défaut
*****************************************/

function setDefaultValue(obj, str){
	if(obj.value == ""){
		obj.value = str;
	
	} // end if
} // end function setDefaultValue

/*****************************************
Affichafe de l'espace connexion
*****************************************/

function displayLogin(){
	var login = document.getElementById("login");
	
	login.style.top = "0px";
	
} // end function displayLogin

/*****************************************
Désaffichage de l'espace connexion
*****************************************/

function maskLogin(){
	if(navigator.appName != "Microsoft Internet Explorer" || (Math.round(navigator.appVersion.substring(0,1)) > "8" && navigator.appName == "Microsoft Internet Explorer")){
		var login = document.getElementById("login");
		login.style.top = -(login.offsetHeight-20) + "px";
	
	}else{
		login.style.top = "0px";
	} // end if
} // end function displayLogin

/*****************************************
Vérification du formulaire login
*****************************************/

function verifLoginForm(){
	new AjaxConnector('ressources/php/login.php', ['login='+window.document.getElementById("chLogin").value, 'pswd='+window.document.getElementById("chPwsd").value], login, ajaxError);
	
	return false;
	
} // end function verifLoginForm

/*****************************************
Identification
*****************************************/

function login(data){
	if(data != "error"){
		var location = window.location + "";
		var url = location.split("/");
		var page = url[url.length-1];
		
		window.location = page;
	
	}else{
		document.getElementById("error").innerHTML = "Mauvais identifiant.";
		
	} // end if
} // end function login

/*****************************************
Déconnexion
*****************************************/

function logout(){
	new AjaxConnector('ressources/php/logout.php', [], logoutClean, ajaxError);
	
} // end function logout

/*****************************************
Déconnexion achevée
*****************************************/

function logoutClean(date){
	var location = window.location + "";
	var url = location.split("/");
	var page = url[url.length-1];
	
	window.location = page;
		
} // end function logoutClean

/*****************************************
Récupérer position souris sur l'écran
*****************************************/

function mousePosition(e){
	if(e){
		window.document.getElementById("div_title").style.top = (e.pageY+20) + "px";
		window.document.getElementById("div_title").style.left = (e.pageX-70) + "px";
		
	}else{
		window.document.getElementById("div_title").style.top = (event.y+20) + "px";
		window.document.getElementById("div_title").style.left = (event.x-70) + "px";
	
	} // end if
} // end function mousePosition

/*****************************************
Affichage du titre du projet
*****************************************/

var div_title;

function displayTitle(title){
	div_title = document.createElement("div");
	div_title.id = "div_title";
	div_title.className = "textebleu";
	div_title.style.background = "url(ressources/img/background_white.png)";
	div_title.style.height = "auto";
	div_title.style.width = "140px";
	div_title.style.padding = "5px";
	div_title.style.position = "absolute";
	div_title.style.zIndex = 10;
	div_title.innerHTML = title;
	
	document.body.appendChild(div_title);
	document.body.onmousemove = mousePosition;
	
} // end function displayTitle

/*****************************************
Déssafichage du titre du projet
*****************************************/

function maskTitle(){
	document.body.removeChild(div_title);
	document.body.onmousemove = "";
	
} // end function maskTitle

/*****************************************
Validation du formulaire de contact
*****************************************/

function validateFormContact(){
	if(document.getElementById("chNAME").value != "" && document.getElementById("chFNAME").value != "" && document.getElementById("chMAIL").value != "" && document.getElementById("chSUBJECT").value != "" && document.getElementById("chMESSAGE").value != ""){
		document.getElementById("formerror").innerHTML = "";
		
		new AjaxConnector('ressources/php/contactPerMail.php', ['name='+document.getElementById("chNAME").value, 'fname='+document.getElementById("chFNAME").value, 'phone='+document.getElementById("chPHONE").value, 'mail='+document.getElementById("chMAIL").value, 'subject='+document.getElementById("chSUBJECT").value, 'message='+document.getElementById("chMESSAGE").value], mailSended, ajaxError);
	
	}else{
		document.getElementById("formerror").innerHTML = "Veuillez remplir tous les champs obligatoires (marqués d'un *)."
		
	} // end if
} // end function validateFormContact

/*****************************************
Réception des données PHP pour savoir
si le message a été envoyé
*****************************************/

function mailSended(data){
	if(data != "error"){
		document.getElementById("formerror").innerHTML = "Votre message a été envoyé."
		document.getElementById("chNAME").value = "";
		document.getElementById("chFNAME").value = "";
		document.getElementById("chMAIL").value = "";
		document.getElementById("chPHONE").value = "";
		document.getElementById("chSUBJECT").value = "";
		document.getElementById("chMESSAGE").value = "";
	
	}else{
		document.getElementById("formerror").innerHTML = "Votre message n'a pas être envoyé. Veuillez recommencer ultérieurement ou nous contacter directement à <a href='mailto:contact@egami-creation.com'>contact@egami-creation.com</a>."
		
	} // end if
} // end function mailSended
