/*

*/

(function($){
	$.fn.extend({
		buildSportMenu: function(){
			var self = this;
			
			menuWidth = {};
			
			return this.each(function(){
				var obj = $(this);
				
				self.placeSubMenu(obj);
				
				//Set la largeur du sous-menu
				$('li.sub', obj).hover(function() {
					var ul = jQuery('> ul', $(this));
					ul.css('display', 'block');
					if(!ul.data('set'))
					{
						max = 0;
						nb = 0;
						jQuery('li', ul).each(function(){
							var width = $(this).outerWidth();
							if(width>max)
								max = width;
							nb++;
						});
						
						//Vérifier si le scroll bar apparait
						if((nb*jQuery('li', ul).height()) > ul.height())
							max +=25;

						ul.css('width', max);
						ul.data('set', true);
					}
				}, function(){
					jQuery('> ul', $(this)).css('display', 'none');
				});				
				
				//Affiche le sousmenu subFixe au bon endroit
				jQuery('ul > li.subFixe', obj).each(function(){
					var left = jQuery($(this)).position().left;
					var top = jQuery('span', $(this)).position().top;
					var height = jQuery('span', $(this)).height();
					
					jQuery('> ul', $(this)).css('left', -left);
					jQuery('> ul', $(this)).css('top', top + height);
				});
				
				//Affiche le bon UL sur le click
				jQuery('ul > li.subFixe', obj).bind('click', function(){
					self.subFixeClick($(this), obj);
				});
				//Affiche l'info-bulle
				jQuery('.questionMark', obj).hover(function(){

					var offset = $(this).offset();
					var left = offset.left;
					var top = offset.top;

					var width = $(this).outerWidth();
					var height = $(this).outerHeight();
					jQuery('.infoBulle').css({'top':top+height+15, 'left':left, 'display':'block'});
				}, function(){
					jQuery('.infoBulle').css('display', 'none');
				});
			});
		},
		
		subFixeClick: function(li, menu){
			var self = this;
			jQuery('ul > li', menu).removeClass('active');
			li.addClass('active');
			self.placeSubMenu(menu);
		},
		
		placeSubMenu: function(obj)
		{
			//Affiche le sousmenu sub au bon endroit
			jQuery('li.sub', obj).each(function(){
				var top = jQuery('span', $(this)).position().top;
				var height = jQuery('span', $(this)).height();				
				
				jQuery('> ul', $(this)).css('left', 0).css('top', top + height);
				//alert("Top: " + top + " Height: " + height);
			});			
			
			//Affiche le sousmenu sub 2e niveau au bon endroit et sa largeur
			jQuery('li.sub2', obj).hover(function(){
				var ul = jQuery('> ul', $(this));
				var top = jQuery('span', $(this)).position().top;
				var parent = jQuery($(this)).parent();
				var left = 0;/*parent.position().left;*/
				var width = parent.width();				
				
				ul.css('display', 'block');
				if(!ul.data('set'))
				{
					max = 0;
					nb = 0;
					jQuery('li', ul).each(function(){
						var width = $(this).outerWidth();
						if(width>max)
							max = width;
						nb++;
					});
					
					//Vérifier si le scroll bar apparait
					if((nb*jQuery('li', ul).height()) > ul.height())
						max +=25;

					ul.css('width', max);
					ul.data('set', true);
				}				
				
				ul.css('top', -2).css('left', left + width);
			}, function(){
				jQuery('> ul', $(this)).css('display', 'none');			
			});
		}
	});
})(jQuery);
