(function(window, $, undef) {
	(function(dep) {
		for(var i in dep) {
			if(dep[i] === undef || dep[i] === false) {
				throw 'Dépendence non satisfaite : '+i;
			}
		}
	})({
		jQuery						: $,
		'jQuery.hoverIntent'		: $ && $.fn && $.fn.hoverintent
	});

	var SousMenu = function() {
		var jElement = $(this),
		height,
		hidden = 0,
		hidding = 1,
		showing = 2,
		visible = 3,
		etat = hidden,
		parentReady = false,
		doDerouleParent = true,

		stop = function() {
			jElement
				.stop()
				.parent()
					.stop();
		},

		repliParent = function() {
			parentReady = false;
			jElement
				.parent()
					.animate(
						{ paddingBottom	: 0 },
						'fast',
						function() {
							etat = hidden;
						}
					);
		},

		derouleParent = function() {
			if(parentReady) {
				afficheItem();
				return;
			}
			jElement
				.parent()
					.animate(
						{ paddingBottom	: (height+6)+'px' },
						'fast',
						function() {
							parentReady = true;
							derouleParent();
						}
					);
		},

		afficheItem = function() {
			jElement
				.css({
					display		: 'block'
				})
				.animate(
					{ opacity: 1 },
					'fast',
					function() {
						etat = visible;
					}
				);
		},

		affiche = function() {
			if(etat >= showing) {
				return;
			}
			stop();
			etat = showing;
			if(doDerouleParent) {
				derouleParent();
			}
			else {
				afficheItem();
			}
		},

		masque = function() {
			if(etat <= hidding) {
				return;
			}
			stop();
			etat = hidding;
			jElement
				.animate(
					{ opacity: 0 },
					'fast',
					function() {
						$(this)
							.css('display', 'none');
						if(doDerouleParent) {
							repliParent();
						}
					}
				);
		},

		afficheDirect = function() {
			if(etat === visible) {
				return;
			}
			stop();
			if(doDerouleParent && !parentReady) {
				jElement
					.parent()
						.css('padding-bottom', (height+6)+'px');
				parentReady = true;
			}
			jElement
				.css({
					display		: 'block',
					opacity		: 1
				});
			etat = visible;
		},

		init = function() {
			jElement
				.css({
					display	: 'block',
					opacity	: 0
				})
				.each(function() {
					if($(this).parent().hasClass('autre')) {
						doDerouleParent = false;
					}
					else {
						height = $(this).height();
					}
				})
				.hide()
				.bind('affiche', affiche)
				.bind('masque', masque)
				.bind('afficheDirect', afficheDirect)
				.filter('.current > *')
					.each(afficheDirect);
		};

		init();
	},

	unselect = function() {
		$(this)
			.removeClass('visible')
			.children('ul.sous_menu')
				.trigger('masque');
	},

	select = function() {
		$(this)
			.children('ul.sous_menu')
				.trigger('affiche')
			.end()
			.addClass('visible')
			.siblings('.visible')
				.each(unselect);
	},

	hoverItem = function(e) {
		if(e.target !== $(this).children('a')[0]) {
			return;
		}
		$(this).each(select);
		return false;
	},

	houtMenu = function() {
		$(this)
			.children('.visible:not(.current)')
				.each(unselect)
			.end()
			.children('.current')
				.each(select);
	},

	houtMenuAutre = function() {
		$(this)
			.children('ul.sous_menu')
				.trigger('masque');
	},

	clickMenuAutre = function(e) {
		$(this)
			.children('ul.sous_menu')
				.trigger('affiche');
		if(e.target.tagName === 'A' && $(e.target).parent().hasClass('autre')) {
			return false;
		}
	},

	initMainMenu = function() {
		$(this)
			.mouseleaveintent(houtMenu)
			.children()
				.children('.sous_menu')
					.each(SousMenu)
				.end()
				.not('.autre')
					.mouseenterintent(hoverItem)
					.filter('.current')
						.addClass('visible')
					.end()
				.end()
				.filter('.autre')
					.click(clickMenuAutre)
					.mouseleaveintent(houtMenuAutre);
	};

	$.fn.mainMenu = function() {
		return this.each(initMainMenu);
	};

})(this, this.jQuery);
