/*!
 * Require jQuery JavaScript Library v1.3.2+
 * http://jquery.com/
 *
 * Copyright (c) 2009 Daynos.net pour MBA Multimédia (www.mba-multimedia.com)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Thanks to : http://www.jquery-france.com/
 *
 * Date: 2009-09-10
 * Revision: 2
 */

(function($) {

	$.fn.jMenu = function(settings)
	{
		settings = $.extend({}, $.fn.jMenu.defaults, settings);
		var idUl = 0;
		var $firstUl = $("ul:first", $(this));
		var debug = false;
		
		if (debug) {
			// DEBUG
			$debug = $('<div />')
				.addClass ('debug')
				.attr ("id", "debug")
				.text ('test')
				;
			$('#header').append ($debug);
		}
		
		// this : #header .menuprim
		return $firstUl.each(function(){
				// Gestion rollover des LI de 1er niveau
				$("li:not(li li)", $(this)).mouseover(function(){
					var $element = $(this);
					var $ul = $element.children("ul");
					if (!$ul.hasClass ("hover")) {
						var nbLi = $("li", this).length;
						var ulHeight = String (nbLi*21) + "px";
						
						// Retirer le hover de tous les autres UL
						$element.prev().children("ul").removeClass ("hover");
						$element.siblings().children("ul").removeClass ("hover");
					
						$ul.addClass ("hover");
						$ul.css({"opacity":0, "height":"0px"}); 
						$ul.stop().animate ({opacity:1, height:ulHeight}, nbLi*settings.speed, "linear");
						$element.prev().children("ul").stop().fadeOut("fast");
						$element.siblings().children("ul").stop().fadeOut("fast");
					}
				});
							
				// Gestion des UL de 2ème niveau
				$("ul", $(this)).each(function(){
					var $ul = $(this);
					// Ajout de classe pour debug de "hover"
					$ul.addClass ('ul-'+idUl)
						.mouseout (function (e) { 
							var posHeader = $('#header .menuprim').offset();
							var posUl = $ul.offset();
							l = parseInt(posUl.left);
							t = parseInt(posUl.top);
							w = $ul.width ();
							h = $ul.height ();
							
							var x = e.pageX - l;
							var y = e.pageY - t;
							
							out = (x<2 || x>w || y<0 || y>h)
							if (out){
								$ul.removeClass ("hover").stop().fadeOut ("fast");
							}
							
							if (debug) {
								$('#debug').html (""
									+"posHeader.left:"+parseInt(posHeader.left)+"; posHeader.top:"+parseInt(posHeader.top)+'<br/>'
									+"posUl.left:"+parseInt(posUl.left)+"; posUl.top:"+parseInt(posUl.top)+'<br/>'
									+"e.pageX:"+e.pageX+"; e.pageY:"+e.pageY+'<br/>'
									+"l:"+l+"; t:"+t+'<br/>'
									+"x:"+x+"; y:"+y+'<br/>'
									+"w:"+w+"; h:"+h+'<br/>'
									+"test:"+(out?'out':'in')
									);
							}
						});
						
					$ul.hide ();
					idUl++;
				});

				// Gestion des LI de 2ème niveau
				$("li li", $(this)).each(function(){
					$(this).css ({opacity:settings.alphaMin});
					$(this).hover(function(){
							if (settings.alphaMaxFade) {
								$(this).stop().fadeTo (settings.alphaMaxFade, settings.alphaMax);
							} else {
								$(this).stop().css ({opacity:settings.alphaMax});
							}
						}, function(){ 
							if (settings.alphaMinFade) {
								$(this).stop().fadeTo (settings.alphaMinFade, settings.alphaMin);								
							} else {
								$(this).stop().css ({opacity:settings.alphaMin});
							}
						});
				 });	
			}
		);
		/*$("body").click(function(){
			$("#header .menuprim li ul").fadeOut("fast");
		});*/
	}
	
	
	
	// Valeurs par défaut
	$.fn.jMenu.defaults = {
		speed:50,
		alphaMax:1.0,
		alphaMaxFade:null,
		alphaMin:0.9,
		alphaMinFade:null
	};
	
	
})(jQuery);
