var fondDiv = false;
var dataDiv = false;


function show_menu(arg){
	if (arg != '1') {
			var Obj1 = document.getElementById('ssmenu1'); 
			if (Obj1) {Obj1.style.display = "none"; }
	}
	if (arg != '2') {
			var Obj2 = document.getElementById('ssmenu2'); 
			if (Obj2) {Obj2.style.display = "none"; }
	}
	if (arg != '3') {
			var Obj3 = document.getElementById('ssmenu3'); 
			if (Obj3) {Obj3.style.display = "none"; }
	}
	if (arg != '4') {
			var Obj4 = document.getElementById('ssmenu4'); 
			if (Obj4) {Obj4.style.display = "none"; }
	}
	if (arg != '5') {
			var Obj5 = document.getElementById('ssmenu5'); 
			if (Obj5) {Obj5.style.display = "none"; }
	}
	
	var Obj = document.getElementById('ssmenu'+arg); 
  if(Obj){
    Obj.style.display = "block"; 
  }
}


// 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.9',
	   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 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();		
	
	
	
	

}


function initSlideShow(){
	var showDuration = 4000;
	var container = $('slideshow-container');
	var images = container.getElements('img');
	var currentIndex = 0;
	var interval;
	var toc = [];
	var tocHeight = 0;
	var tocActive = 'toc-active';

	/* new: starts the show */
	var start = function() { interval = show.periodical(showDuration); };
	var stop = function() { $clear(interval); };
	/* worker */
	var show = function(to) {
		images[currentIndex].fade('out');
		toc[currentIndex].removeClass(tocActive);
		images[currentIndex = ($defined(to) ? to : (currentIndex < images.length - 1 ? currentIndex+1 : 0))].fade('in');
		toc[currentIndex].addClass(tocActive);
	};



	/* new: control: table of contents */
	images.each(function(img,i){
			
			var z = i+1;
			
			var csstoc = "toc";
			if(z==4) {
				csstoc = "toc_debug"
			}
			
			toc.push(new Element('a',{
				text: $('txt_'+z).innerHTML,
				href: '#',
				'class': csstoc + (i == 0 ? ' ' + tocActive : ''),
				events: {
					click: function(e) {
						if(e) e.stop();
						stop();
						show(i);
					}
				},
				styles: {
					top: ((tocHeight + 6 ))
				}
			}).inject(container));
			 if(i > 0) { img.set('opacity',0);  } 
			 
			 tocHeight = tocHeight + 71;
		}	);
		
	/* new: control: start/stop on mouseover/mouseout */
	/*container.addEvents({
		mouseenter: function() { stop(); },
		mouseleave: function() { start(); }
	});
	*/

	/* start once the page is finished loading */
	window.addEvent('load',function(){
		start();
	});
}
	


function initSlideShow2(){
	var showDuration = 4000;
	var container = $('slideshow-container2');
	var images = container.getElements('img');
	var currentIndex = 0;
	var interval;
	var toc = [];
	var tocHeight = 0;
	var tocActive = 'toc-active';

	/* new: starts the show */
	var start = function() { interval = show.periodical(showDuration); };
	var stop = function() { $clear(interval); };
	/* worker */
	var show = function(to) {
		images[currentIndex].fade('out');
		toc[currentIndex].removeClass(tocActive);
		images[currentIndex = ($defined(to) ? to : (currentIndex < images.length - 1 ? currentIndex+1 : 0))].fade('in');
		toc[currentIndex].addClass(tocActive);
	};



	/* new: control: table of contents */
	images.each(function(img,i){
			
			var z = i+1;
			
			var csstoc = "toc";
			if(z==4) {
				csstoc = "toc_debug"
			}
			
			
			toc.push(new Element('a',{
				text: $('txt_'+z).innerHTML,
				href: '#',
				'class': csstoc + (i == 0 ? ' ' + tocActive : ''),
				events: {
					click: function(e) {
						if(e) e.stop();
						stop();
						show(i);
					}
				},
				styles: {
					top: ((tocHeight + 6 ))
				}
			}).inject(container));
			 if(i > 0) { img.set('opacity',0);  } 
			 
			 tocHeight = tocHeight + 71;
		}	);
		
	/* new: control: start/stop on mouseover/mouseout */
	/*container.addEvents({
		mouseenter: function() { stop(); },
		mouseleave: function() { start(); }
	});
	*/

	/* start once the page is finished loading */
	window.addEvent('load',function(){
		start();
	});
}
