/* 
Simple JQuery menu.
HTML structure to use:

Notes: 

1: each menu MUST have an ID set. It doesn't matter what this ID is as long as it's there.
2: each menu MUST have a class 'menu' set. If the menu doesn't have this, the JS won't make it dynamic

Optional extra classnames:

noaccordion : no accordion functionality
collapsible : menu works like an accordion but can be fully collapsed
expandfirst : first menu item expanded at page load

<ul id="menu1" class="menu [optional class] [optional class]">
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
...
...
</ul>

Copyright 2008 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
*/

if( document.location.href != "http://www.polesantetravail.fr/nos-centres.html"
 && document.location.href != "http://www.polesantetravail.fr/liste-des-centres_antennes.html") {

	function initMenus() {
		var controle = true;
		var initMenusBoo = false;
		$('ul.menu ul').hide();
		$.each($('ul.menu'), function(){
			$('#' + this.id + '.expandfirst ul:first').show();
		});
		$('ul.menu li a').mouseover(
			function() {
				var checkElement = $(this).next();
				var parent = this.parentNode.parentNode.id;

				if($('#' + parent).hasClass('noaccordion')) {
					$(this).next().slideToggle('normal');
					return false;
				}
				/*
				if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
					if($('#' + parent).hasClass('collapsible')) {
							$('#' + parent + ' ul:visible').slideUp('normal');
					}
					return false;
				}*/
				if((checkElement.is('ul')) && (!checkElement.is(':visible'))) 
				{
						checkElement.slideDown('normal');
						//$('#' + parent + ' ul:visible').slideUp('normal');
				}
			}
		);
		var kiki;
		$('ul.menu').mouseover( function ()
		{
			controle = true;
			var initMenusBoo = true;
			$(this).hover(
				function() {
					initMenusBoo = true;
				},
				function()  {
					initMenusBoo = false;
					var checkElement = $(this);//.children('ul')
					var count = $(this).parents('ul').size();
					if(count == 0)
					{
						
						kiki = setTimeout(function() 
						{
							if(initMenusBoo == false)
							{
								if (controle == true)
								{	
									$.each($('#navigation ul'), function(){
									if($(this).parents('ul').size() == 1)
										{
											var verif = false;
											$.each($(this).children('li').children('a'), function(){
													if($(this).hasClass('menuon'))
													{	
															verif = true;	
															//$(this).next('ul').slideUp('normal');
													}	
													else
													{
														var sous_verif = false;
														$.each($(this).parent('li').children('ul').children('li').children('a'), function(){
														if($(this).hasClass('menuon'))
															{	
																	verif = true;	
																	sous_verif = true;
															}
														});
														if (sous_verif == false)
														{
															$(this).next('ul').slideUp('normal');
														}
														/*
														if (verif == false  && fermer == true)
														{
															this.parent('li').parent('ul').slideUp('normal');
														}
														*/
													}
											});
											if (verif == false)
											{
												$(this).slideUp('normal');
											}
										}
									controle = false;
									});
								}
							}
						}, 3000);
					}
				}
			);
		});
	}
	$(document).ready(function() {initMenus();});

	selecnavigationsboo = true;
	function selecnavigations(arg_url) {
		$.each($('#navigation ul li a'), function(){
			var count = $(this).parents('ul').size();
			if($(this).next().is('ul'))
			{
				//$(this).html($(this).text()+"  &darr;");
				if(count == 2)
				{
					$(this).addClass("menu_flechebas_niveau_2");
				}
				else
				{
					$(this).addClass("menu_flechebas");
				}
			}
			if(this.href == arg_url && selecnavigationsboo == true)
			{
				if ($(this).next().is('ul'))
				{
					$(this).next().show();
				}
					$(this).parents('ul').show();
				if(count == 1)
				{
					$(this).addClass("menuon_niveau_1");
					$(this).removeClass("menu_flechebas");
				}
				else
				{
					$(this).addClass("menuon");
				}
				selecnavigationsboo = false;
			}
		}); 
	}

}
