var fondDiv = false;
var dataDiv = false;
// Ajout a la newsletter
function addNewsletter(idForm) {
	
	if(!fondDiv){
		fondDiv = document.createElement('div');
		fondDiv.style.position = 'absolute';
		document.body.appendChild(fondDiv);
	}
	fondDiv.innerHTML = '';
	//alert(getHeight());
	
	$(fondDiv).setStyles({
        backgroundColor: '#000000',
        width: '100%',
        height: getScrollHeight(),
        position: 'absolute',
        opacity: '0.5',
        overflow: 'hidden',
        top: '0px',
        left: '0px',
        zIndex: '1500',
        visibility: 'hidden'
    });

	$(fondDiv).onclick = function() { removeDiv(); };

	if(!dataDiv){
		dataDiv = document.createElement('div');
		dataDiv.style.position = 'absolute';
		document.body.appendChild(dataDiv);
	}

	$(dataDiv).setStyles({
	   backgroundColor: 'transparent',
	   width: '500px',
	   height: '300px',
	   position: 'absolute',
	   top: '50%',
	   left: '44%',	   
	   marginLeft: '-150px',	   
	   marginTop: (getScrollTop()-150) + 'px',	   	   	   
	   zIndex: '1600',
	   visibility: 'hidden'
	});	

	new Ajax(
		'/js/ajax/newsletter.php',
		{
			update: dataDiv,
			data:$(idForm).toQueryString(),
			onSuccess: function(req) {

					$$(fondDiv).setStyles({visibility: 'visible' });
					$$('select').setStyles({visibility: 'hidden' });
					$$(dataDiv).setStyles({visibility: 'visible' });
	
			},
			evalScripts: true
		}
	).request();
}


function openNewsletter() {
	
	if(!fondDiv){
		fondDiv = document.createElement('div');
		fondDiv.style.position = 'absolute';
		document.body.appendChild(fondDiv);
	}
	fondDiv.innerHTML = '';
	//alert(getHeight());
	
	$(fondDiv).setStyles({
        backgroundColor: '#000000',
        width: '100%',
        height: getScrollHeight(),
        position: 'absolute',
        opacity: '0.5',
        overflow: 'hidden',
        top: '0px',
        left: '0px',
        zIndex: '1500',
        visibility: 'hidden'
    });

	$(fondDiv).onclick = function() { removeDiv(); };

	if(!dataDiv){
		dataDiv = document.createElement('div');
		dataDiv.style.position = 'absolute';
		document.body.appendChild(dataDiv);
	}

	$(dataDiv).setStyles({
	   backgroundColor: 'transparent',
	   width: '500px',
	   height: '300px',
	   position: 'absolute',
	   top: '50%',
	   left: '44%',	   
	   marginLeft: '-150px',	   
	   marginTop: (getScrollTop()-150) + 'px',	   	   	   
	   zIndex: '1600',
	   visibility: 'hidden'
	});	

	new Ajax(
		'/js/ajax/openNewsletter.php',
		{
			update: dataDiv,
			// data:$(idForm).toQueryString(),
			onSuccess: function(req) {

					$$(fondDiv).setStyles({visibility: 'visible' });
					$$('select').setStyles({visibility: 'hidden' });
					$$(dataDiv).setStyles({visibility: 'visible' });
	
			},
			evalScripts: true
		}
	).request();
}


function openIntervenant(id) {
	
	if(!fondDiv){
		fondDiv = document.createElement('div');
		fondDiv.style.position = 'absolute';
		document.body.appendChild(fondDiv);
	}
	fondDiv.innerHTML = '';
	//alert(getHeight());

	$(fondDiv).setStyles({
	   backgroundColor: '#000000',
	   width: '100%',
	   height: getScrollHeight(),
	   position: 'absolute',
	   opacity: '0.5',
	   overflow: 'hidden',
	   top: '0px',
	   left: '0px',
	   zIndex: '1500',
	   visibility: 'hidden'
	});

	$(fondDiv).onclick = function() { removeDiv(); };

	if(!dataDiv){
		dataDiv = document.createElement('div');
		dataDiv.style.position = 'absolute';
		document.body.appendChild(dataDiv);
	}

	$(dataDiv).setStyles({
	   backgroundColor: '#ffffff',
	   width: '550px',
	   height: '400px',
	   position: 'absolute',
	   top: '50%',
	   left: '44%',	   
	   marginLeft: '-160px',	   
	   marginTop: (getScrollTop()-200) + 'px',	   	   	   
	   zIndex: '1600',
	   visibility: 'hidden'
	});	

	new Ajax(
		'/js/ajax/intervenant.php',
		{
			update: dataDiv,
			data:"intervenant_id="+id,
			onSuccess: function(req) {

					$$(fondDiv).setStyles({visibility: 'visible' });
					$$('select').setStyles({visibility: 'hidden' });
					$$(dataDiv).setStyles({visibility: 'visible' });
	
			},
			evalScripts: true
		}
	).request();		
	
	
	
}



	

function openIntervenant2(id,jour,horraire,debat) {

	new Ajax(
		'/js/ajax/intervenant.php',
		{
			update: $('intervenant_popup'),
			data:"intervenant_id="+id,
			onSuccess: function(req) {

			},
			evalScripts: true
		}
	).request();		
	
	document.getElementById('interv_'+jour+"_"+horraire+"_"+debat+"_"+id).onclick=function(event) {
	  if( window.event)
	    event = window.event;
		
		var x = event.clientX;
		var y = event.clientY;	
		
		x = x+10;
		y = y+10;
		$('intervenant_popup').style.top = y+"px";
		$('intervenant_popup').style.left = x+"px";
		
	}

	$('intervenant_popup').style.display = "block";
}


function addLogin(idForm) {
	
	if(!fondDiv){
		fondDiv = document.createElement('div');
		fondDiv.style.position = 'absolute';
		document.body.appendChild(fondDiv);
	}
	fondDiv.innerHTML = '';
	//alert(getHeight());

	$(fondDiv).setStyles({
	   backgroundColor: '#000000',
	   width: '100%',
	   height: getScrollHeight(),
	   position: 'absolute',
	   opacity: '0.5',
	   overflow: 'hidden',
	   top: '0px',
	   left: '0px',
	   zIndex: '1500',
	   visibility: 'hidden'
	});

	if(!dataDiv){
		dataDiv = document.createElement('div');
		dataDiv.style.position = 'absolute';
		document.body.appendChild(dataDiv);
	}

	$(dataDiv).setStyles({
	   backgroundColor: '#000000',
	   width: '300px',
	   height: '300px',
	   position: 'absolute',
	   border: '1px solid #000',
	   top: '50%',
	   left: '50%',	   
	   marginLeft: '-150px',	   
	   marginTop: (getScrollTop()-150) + 'px',	   	   	   
	   zIndex: '1600',
	   visibility: 'hidden'
	});	

	new Ajax(
		'/js/ajax/login.php',
		{
			update: dataDiv,
			data:$(idForm).toQueryString(),
			onSuccess: function(req) {

					$$(fondDiv).setStyles({visibility: 'visible' });
					$$('select').setStyles({visibility: 'hidden' });
					$$(dataDiv).setStyles({visibility: 'visible' });
	
			},
			evalScripts: true
		}
	).request();		
	
	
	
}



function removeDiv()
{
	if(fondDiv){
		$(fondDiv).remove();
		fondDiv = false;
	}	
	if(dataDiv){
		$(dataDiv).remove();
		dataDiv = false;
	}
	$$('select').setStyles({visibility: 'visible' });		
}

function changeMenu(menu_id) {
	

	// On réinitialise tous les menus
	var main_menu_items = $$(".grandmenu");

	main_menu_items.each(function(item){
		var old_id = item.id;		
		
		$(old_id).style.backgroundColor="transparent";
		$(old_id).style.backgroundImage="url(/styles/images/header/onglet.png)";
		$(old_id).style.backgroundRepeat="no-repeat";
		$(old_id).style.backgroundPosition="bottom left";
		$(old_id).style.color="#55b0db";
		
		data = item.id.split('_');
		$("supermenu_a_"+data[1]).style.color="#55b0db";
	})
	

	// On active que celui qui nous interesse	
	if($("supermenu_"+menu_id)) {
		$("supermenu_"+menu_id).style.backgroundColor="transparent";
		$("supermenu_"+menu_id).style.backgroundImage="url(/styles/images/header/onglet_hover.png)";
		$("supermenu_"+menu_id).style.backgroundRepeat="no-repeat";
		$("supermenu_"+menu_id).style.backgroundPosition="bottom left";		
		
		$("supermenu_a_"+menu_id).style.color="#dfdfdf";
	}
	
	
	// Cas des sous menus
	// On initialise
	var main_menu_items = $$('.niveau2');
	
	main_menu_items.each(function(item){
		var old_id = item.id;		
		$(old_id).style.display="none";
	})		
	if($("sub_"+menu_id)) {
		$('noss').style.display="none";
		$('sub_'+menu_id).style.display="block";
	}else{
		$('noss').style.display="none";
	}
}

function change_reference(obj, etat) {
	if(etat==1) {
		$(obj).style.backgroundColor="#fff";
		$(obj).style.backgroundImage="url(/styles/images/reference/bordure.png)";
		$(obj).style.backgroundRepeat="no-repeat";
		$(obj).style.backgroundPosition="top left";			
	}else{
		$(obj).style.background="none";
	}
}




 

function infobulle(id, event){ 

		  if( window.event)
		    event = window.event;
			
			var x = event.clientX;
			var y = event.clientY;


document.getElementById('bulle').style.visibility = 'visible'; 
document.getElementById('bulle').style.left = (x+10)+"px"; 
document.getElementById('bulle').style.top = (y+20+getScrollTop())+"px"; 

txt = $("txt_"+id).innerHTML;
document.getElementById('bulle').innerHTML = txt;


//

} 

function infobulle_cache(){
	document.getElementById('bulle').style.visibility = 'hidden';
}


function changebordertext(name,etat) {
	if(etat) {
		name.style.border="green 1px solid";
	}else{
		name.style.border="red 1px solid";
	}
}

function VerifMail(value)
	{
	a = value;
	valide1 = false;
	
	for(var j=1;j<(a.length);j++){
		if(a.charAt(j)=='@'){
			if(j<(a.length-4)){
				for(var k=j;k<(a.length-2);k++){
					if(a.charAt(k)=='.') valide1=true;
				}
			}
		}
	}
	if(valide1==false) return false;
	return true;
	}




function verifForm(name, type, length) {	
	// TEXT
	if(type=="text") {		
		if(length==0 && name.value.length != 0) {
			changebordertext(name,true);
		}else{
			if(length > name.value.length || name.value.length == 0) {
				changebordertext(name,false);
			}else{
				changebordertext(name,true);
			}
		}
	}
	
	// MAIL
	if(type=="email") {		
		if(length==0 && name.value.length != 0) {
			if(VerifMail(name.value)) {
				changebordertext(name,true);
			}else{
				changebordertext(name,false);
			}
		}else{
			if(length > name.value.length || name.value.length == 0) {
				if(VerifMail(name.value)) {
					changebordertext(name,true);
				}else{
					changebordertext(name,false);
				}
			}else{
				changebordertext(name,true);
			}
		}		
	}
	
	
}


function init_menu(selected_section){
	
	var main_menu_items = $$('.sousmenu');
	//console.log(main_menu_items.length);
	
	main_menu_items.each(function(item){
			
			//console.log(item.id);		
			data = item.id.split('_');	
			//console.log('categorie_id = ' + data[1]);		
			
			if($('sub_' + data[1]))
			{
				submenu = $('sub_' + data[1]);
				
				var height_fx = new Fx.Style($('sub_' + data[1]),'height')
				var nb = $('sub_' + data[1]).getChildren().length
				
				//var Pos = $('supermenu_' + data[1]).getPosition();
				//$('submenuw_' + data[1]).style.left = Pos.x - 2 + 'px';
				
				//console.log('nb = ' + nb);		

			

				//--deb
				item.addEvent('mouseenter',function(e){
					//$$('input').setStyles({visibility: 'hidden' });
					item.addClass('active');

					e = new Event(e)
					
					// calculate the submenu new item
					height_fx.stop()
					height_fx.start(nb*29)
					//console.log('sdsd'+submenu.id);
					submenu.getChildren().each(function(submenu_item){
						//console.log('pwet');
						submenu_item.addEvent('mouseover',function(e){
							// change color
							//submenu_item.addClass('active');
							// end of submenu_item.addEvent('mouseover')
						})
						submenu_item.addEvent('mouseout',function(e){
							// change color
							//submenu_item.removeClass('active');
							// end of submenu_item.addEvent('mouseout')
						})
					})

					e.stop()
					// end of item.addEvent('mouseenter')
				})
				//--deb

				
				//--deb
				item.addEvent('mouseleave',function(e){
					item.removeClass('active');
					document.getElementById('trait_'+data[1]).style.display='none';

					e = new Event(e)
					height_fx.stop()
					height_fx.start(0)
					// change color
					e.stop()

					//$$('input').setStyles({visibility: 'visible' });
					
					// end of item.addEvent('mouseleave')
				})
				//--deb


			} //ifsubmenu


			
		


	// end of each
	})
	
// end of function
}

function hideMenu(id) {
	if(document.getElementById('trait_'+id)) document.getElementById('trait_'+id).style.display='none';
	if(document.getElementById('sub_'+id)) document.getElementById('sub_'+id).style.display='none';
}

function showMenu(id) {
	if(document.getElementById('trait_'+id)) document.getElementById('trait_'+id).style.display='block';
	if(document.getElementById('sub_'+id)) document.getElementById('sub_'+id).style.display='block';
}

var Interval;
//window.onload = wl;
var iScroll=0;
function defileBas(param_scroll) {
	
	
	if(param_scroll==1)	iScroll=param_scroll;
	if(iScroll)
	{
		$('contenu_page').scrollTop+=2;
		setTimeout(defileBas, 10);
	}
}

function defileHaut(param_scroll) {
	if(param_scroll==1)	iScroll=param_scroll;
	if(iScroll)
	{
		$('contenu_page').scrollTop-=2;
		setTimeout(defileHaut, 10);
	}	
}

function defilePause() {
	iScroll=0;
}

function checknewsletter(on) {
	new Ajax(
		'/js/ajax/newsletter.php',
		{
			method:'post',
			update: dataDiv,
			data:$('formnewsletter').toQueryString(),
			onSuccess: function(req) {
				update:$('arrDebug')
			},
			evalScripts: true
		}
	).request();		
	
	
	
	

}
	
