// JavaScript Document

jQuery(document).ready(function()
{
	/* --- Menu --- */
	var imgBas = new Image();
	imgBas.src = "images/fleche-bas.gif";
	
	var imgHaut = new Image();
	
	if(jQuery.browser.msie && jQuery.browser.version == "6.0")
		imgHaut.src = "images/fleche-haut.gif";
	else
		imgHaut.src = "images/fleche-haut.png";
		
	function switchEtat(elem)
	{
		var divPrinc = jQuery(".dernier-match");
		elem = jQuery(elem);
		
		if(divPrinc.children(".content-encart-right:visible").length < 2)
		{	
			if(!elem.parent().hasClass("titre-right"))
			{
				if(to != undefined) clearTimeout(to);
				var obj = divPrinc.children("p[class=titre-right]");
				
				obj.find("img").replaceWith("<img src=\""+imgHaut.src+"\" />");
				obj.next(".content-encart-right").slideToggle("slow");
				obj.removeClass().addClass("titre-right-off");
				
				elem.parent().find("img").replaceWith("<img src=\""+imgBas.src+"\" />");
				elem.parent().next(".content-encart-right").slideToggle("slow");
				elem.parent().removeClass().addClass("titre-right");
			}
		}
		else
		{
			var to = setTimeout(function()
			{
				switchEtat(elem);
			}, 50);
		}
	}
	
	var divPrinc = jQuery(".dernier-match");
	
	divPrinc.children("p[class=titre-right-off]").each(function()
	{
		jQuery(this).next(".content-encart-right").hide();
	});
	
	divPrinc.children("p").each(function()
	{
		jQuery(this).children("a").click(function()
		{
			switchEtat(this);
		});
	});
	/* --- end Menu --- */
	
	/* --- SousMenu --- */
	function over2(_this)
	{
		clearTimeout(timer2);
		
		if(action == null)
		{
			menu.children("li").children("ul").hide();
			action = jQuery("");
		}
		
		timer = setTimeout(function()
		{
			if(jQuery("*").index(_this) != jQuery("*").index(action))
			{
				if(jQuery("*").index(action) != -1)
				{
					_this.children("ul").show();
					_this.children("a").addClass("rubrique_active");
				}
				else
				{
					_this.children("ul").fadeIn();
					_this.children("a").addClass("rubrique_active");
				}
			
				action.children("ul").hide();
				action.children("a").removeClass("rubrique_active");
			}
			
			action = _this;
			menu.children("li").unbind("mouseover");
			menu.children("li").each(function()
			{						
				jQuery(this).mouseover(function()
				{
					over2(jQuery(this));
				});
			});
		}, 20);
	}
	
	function click2(_this)
	{
		clearTimeout(timer2);
		
		if(action == null)
		{
			menu.children("li").children("ul").hide();
			action = jQuery("");
		}
		
		if(jQuery("*").index(_this) != jQuery("*").index(action))
		{
			if(jQuery("*").index(action) != -1)
			{
				_this.children("ul").show();
				_this.children("a").addClass("rubrique_active");
			}
			else
			{
				_this.children("ul").fadeIn();
				_this.children("a").addClass("rubrique_active");
			}
		
			action.children("ul").hide();
			action.children("a").removeClass("rubrique_active");
		}
		
		action = _this;
		menu.children("li").unbind("mouseover");
		menu.children("li").each(function()
		{						
			jQuery(this).mouseover(function()
			{
				over2(jQuery(this));
			});
		});
	}
	
	var menu = jQuery("#menuTop");
	var action = null;
	var timer = null;
	var timer2 = null;
	
	menu.children("li").mouseover(function()
	{
		clearTimeout(timer2);
		
		if(jQuery(this).children("ul").length > 0)
		{
			if(jQuery(this).children("ul:visible").length == 0)
			{
				var obj_this = jQuery(this);
				
				timer = setTimeout(function()
				{
					obj_this.children("ul").fadeIn();
					obj_this.children("a").addClass("rubrique_active");
					
					action = obj_this;
					
					menu.children("li").unbind("mouseover");
					menu.children("li").each(function()
					{						
						jQuery(this).mouseover(function()
						{
							over2(jQuery(this));
						});
					});
				}, 20);
			}
		}
	});
		
	menu.children("li").click(function()
	{
		click2(jQuery(this));
	});
	
	menu.children("li").mouseleave(function()
	{
		clearTimeout(timer);
		
		jQuery(this).unbind("mouseover");
		jQuery(this).each(function()
		{						
			jQuery(this).mouseover(function()
			{
				over2(jQuery(this));
			});
		});
	});
	
	menu.mouseleave(function()
	{
		timer2 = setTimeout(function()
		{
			menu.children("li").children("ul").fadeOut();
			menu.children("li").children("a").removeClass("rubrique_active");
			
			action = null;
		}, 20);
	});
	
	/* --- end SousMenu --- */
});
