jQuery.noConflict();
(function($) { 
	$(function() {
		//handle the MENU
		$(document).ready(function(){
			//init some object
			var menu = new MenuUtil();
			
			/*
			 * Make the menu visible when the mouseover's event is fired
			 */
			if(!$.browser.msie){
				//OK for Firefox + Safari
				$('#menu li').mouseover(function(){ $(this).addClass('hover'); });
				$('#menu li').mouseout(function(){ $(this).removeClass('hover'); });
			}
			else{
				//OK for the stupid IE6
				for(var index = 0; index < $('#menu li').length; index++){
					var node = $('#menu li').get(index);
					node.onmouseover = function(){$(this).addClass('hover')}
					node.onmouseout = function(){$(this).removeClass('hover')}
				}
			}
			
			/**
			 * DEBUG LINE : reset the cookies
			 */ 
//			$.cookie('widthElementsLevel1',null);
//			$.cookie('widthElementsLevel2',null);
			
			/**
			 * decide if the cookie has already been set.
			 * If it is the case get the value from the cookie and transform the string into an array
			 */
    		if($.cookie('widthElementsLevel1') != null){
    			var widthElementsLevel1 = $.cookie('widthElementsLevel1').split(',');
    			var widthElementsLevel2 = $.cookie('widthElementsLevel2').split(',');
    		}
    		else{
    			var widthLevel1Cookie = widthLevel2Cookie = '';
    		}

			/*
			 * As it takes time to find the width of a HTML element, for Firefox particulary, 
			 * the width is memorized in a non-persistant cookie. On the next load, the width of the elements are 
			 * gotten from the cookie which spare a lot of Dom modification (up to 50 time faster) 
			 */
			//adapt te width of the menu
			var level0 = $('#menu > li');
			for(var i = counter = 0 ; i < level0.length ; i++){
				//reinit the value
				var widthLevel1 = 0;
				
				/*
				 * process the level1 of the menu
				 */
				var level1 = $('#menu > li:eq('+i+') > ul > li');
				//true when there is a cookie
				if(typeof(widthElementsLevel1) != 'undefined'){
					widthLevel1 = widthElementsLevel1[i] - 0;
				}
				else{
					//search for the widthest element
					widthLevel1 = menu.getWidthest(level1);
					widthLevel1Cookie += widthLevel1+',';
				}
				$(level1).css('width',widthLevel1);
				
				/*
				 * process the level2 of the menu
				 */
				for(var j = 0; j < level1.length; j++){
					var widthLevel2  = 0
					var level2 = $('#menu > li:eq('+i+') > ul > li:eq('+j+') > ul > li');
					//echo(level2)
					if(typeof(widthElementsLevel2) != 'undefined'){
						widthLevel2 = widthElementsLevel2[counter] - 0;
					}
					else{
						//search for the widthest element
						widthLevel2 = menu.getWidthest(level2);
						widthLevel2Cookie += widthLevel2+',';
					}
					$(level2).css('width',widthLevel2);
					counter++
				}
			}
			
			/*
			 * memorize the cookies, this code is true only with the first load
			 */
    		if($.cookie('widthElementsLevel1') == null){
				$.cookie('widthElementsLevel1', widthLevel1Cookie);
				$.cookie('widthElementsLevel2', widthLevel2Cookie);
    		}
		});
		
		function MenuUtil(){
			this.getWidthest = function(elements){
				var widthElement = 0;
				for(var j=0;j<elements.length;j++){
					var width = $(elements[j]).width();
					if(width > widthElement){
						widthElement = width;
					}
				}
				return widthElement;
			}
		}
	});
})(jQuery);

