var mouseX;
var mouseY;
var tempDef;

function getcords(e){
		mouseX = Event.pointerX(e);
		mouseY = Event.pointerY(e);
		//for testing put the mouse cords in a div for testing purposes
		//$('debug').innerHTML = 'mouseX:' + mouseX + '-- mouseY:' + mouseY;
}

function cacherDiv(){
	var div,div2;
	div=document.getElementsByTagName("div");
	for (i=0;i<div.length;i++) {
		var nom=div[i].id;
		if(nom.substr(0,7)=="cacher_"){
			div[i].style.display="none";	
		}
	}
	div2=document.getElementsByTagName("a");
	for (i=0;i<div2.length;i++) {
		var nom=div2[i].id;
		if(nom.substr(0,8)=="deplier_"){
			div2[i].innerHTML="+";
      div2[i].attributes.title.value="Déplier le bloc";		
		}
	}
	
}

function changerDiv(id){
	var nom="cacher_"+id;
	var div=document.getElementById(nom);
	if(div.style.display=="none"){
		Effect.toggle(nom,'slide', { duration: 0.5 });
		document.getElementById('deplier_'+id).innerHTML="-";
		document.getElementById('deplier_'+id).attributes.title.value="Replier le bloc";
	}else{
		Effect.toggle(nom,'slide', { duration: 0.5 });
		document.getElementById('deplier_'+id).innerHTML="+";
		document.getElementById('deplier_'+id).attributes.title.value="Déplier le bloc";
	}
}

function initForm(){
	
	var form=document.getElementById("form_required");
	if (form){
		form.onsubmit=function(){
			return verifForm();
		}
	}
}


function verifForm(){
	var form;
	form=document.getElementsByTagName("input");
	for (i=0;i<form.length;i++){
		if (form[i].className.search("required")!=-1){
			
			if (form[i].value==""){
				alert("Veuillez remplir le champ '"+form[i].attributes.name.value+"'");
				form[i].onfocus=function(){
					form[i].style.backgroundColor="#e79797";
				}
				form[i].focus();
				form[i].onblur=function(){
					form[i].onfocus=function(){
						form[i].style.backgroundColor="#e7e7e7";
					}
					form[i].style.backgroundColor="#ffffff";
				}
				return false;
			}
			
		}
		if (form[i].className.search("is_mail")!=-1){
			if(form[i].value!=""){
				var mail=form[i].value;
				var regexp = new RegExp("^[a-zA-Z0-9_\\-\\.]{1,}@[a-zA-Z0-9\\-_]{2,}\\.[a-zA-Z]{2,4}$", "g");
				if (!regexp.test(mail)){
					alert("L'adresse email choisie n'est pas valide");	
					form[i].onfocus=function(){
						form[i].style.backgroundColor="#e79797";
					}
					form[i].focus();
					form[i].onblur=function(){
						form[i].onfocus=function(){
							form[i].style.backgroundColor="#e7e7e7";
						}
						form[i].style.backgroundColor="#ffffff";
					}
					return false;
				}
			}
		}
	}
	form=document.getElementsByTagName("textarea");
	for (i=0;i<form.length;i++){
		if (form[i].className.search("required")!=-1){
			
			if (form[i].value==""){
				alert("Veuillez remplir le champ '"+form[i].attributes.name.value+"'");
				form[i].onfocus=function(){
					form[i].style.backgroundColor="#e79797";
				}
				form[i].focus();
				form[i].onblur=function(){
					form[i].onfocus=function(){
						form[i].style.backgroundColor="#e7e7e7";
					}
					form[i].style.backgroundColor="#ffffff";
				}
				return false;
			}
			
		}
	}
	return true;
}

function set_newLine(text,nbcarac){
	var tmpText="";
	var i=0;
	var temp=0;
	var tabText=text.split(" ");
	var tabHTML=new Array("<strong>","</strong>","<u>","</u>");
	for(i=0;i<tabText.length;i++){
		if (tabText[i]!="<strong>" && tabText[i]!="</strong>" && tabText[i]!="<u>" && tabText[i]!="</u>"){
			temp=temp+tabText[i].length;
		}
		tmpText=tmpText+" "+tabText[i];
		if(temp>=nbcarac){
			tmpText=tmpText+"<br>";
			temp=0;
		}
		if(tabText[i]=="<br>" || tabText[i]=="<br />"){
      temp=0;
    }
	}
	return tmpText;
}

function init_def(){
	var elmt;
	elmt=document.getElementsByTagName("a");
	for(i=0;i<elmt.length;i++){
		if (elmt[i].attributes.title){
			if(elmt[i].attributes.title.value.substr(0,4)=="def:"){
				Event.observe(document, 'mousemove', getcords);
				elmt[i].style.cursor="help";
				elmt[i].onmouseover=function(){
					activBulle(this,1);
				}
				elmt[i].onmouseout=function(){
					desactivBulle(this);	
				}
			}
			if(elmt[i].attributes.title.value.substr(0,6)=="Nodef:"){
				Event.observe(document, 'mousemove', getcords);
				elmt[i].style.cursor="help";
				elmt[i].onmouseover=function(){
					activBulle(this,0);
				}
				elmt[i].onmouseout=function(){
					desactivBulle(this);	
				}
			}
		}
	}
}

function activBulle(elmt,affichage_def){
	var text;
	if (elmt.attributes.title.value!="undefined" && affichage_def==1){
   		text="<u> <strong> Définition </strong> </u> : "+elmt.attributes.title.value.substr(4,elmt.attributes.title.value.length-4);
   	}else if(elmt.attributes.title.value!="undefined" && affichage_def==0){
		text=elmt.attributes.title.value.substr(6,elmt.attributes.title.value.length-6);
	}else{
    text="Probème de définition veuillez recharger la page";
  }
	tempDef=elmt.attributes.title.value;
	elmt.attributes.title.value="";
	var bulle=document.getElementById("bulle");
	if (bulle){
		bulle.style.display="block";
		bulle.style.marginLeft=mouseX+"px";
		bulle.style.marginTop=mouseY+2+"px";
		bulle.innerHTML=set_newLine(text,45);
	}
}

function desactivBulle(elmt){
	elmt.attributes.title.value=tempDef;
	var bulle=document.getElementById("bulle");
	if(bulle){
		bulle.style.display="none";	
	}
}

window.onload=function (){
	initForm();
	gstop();
	cacherDiv();
	init_def();
	init();
	
}

function init(e)
{
	//alert("test");
	Event.observe($("form_nl"),"submit",addNewsletter);
}

function addNewsletter(e)
{
	
	var regexp = new RegExp("^[a-zA-Z0-9_\\-\\.]{3,}@[a-zA-Z0-9\\-_]{2,}\\.[a-zA-Z]{2,4}$", "g");
	if(regexp.test($("input_newsletter").value)){
		new Ajax.Request("ajax_newsletter.php",{parameters:{mail:$("input_newsletter").value},onSuccess:finishNewsletter});
	}else{
		alert("Veuillez saisir une adresse email valide");	
	}
	Event.stop(e);
}

function finishNewsletter(retour){
	var message=retour.responseText;
	
	var tableau=message.split("!#");
	$("div_ajax_lettre").innerHTML=tableau[1];
	
	if(tableau[0]=="ok"){
	    pageTracker._trackPageview("/demande-nl.php");
	}
}

function goToLetter(){
	$("input_newsletter").style.backgroundColor="#65d104";
	$("input_newsletter").style.color="#ffffff";
	$("input_newsletter").focus();
}


