jQuery.noConflict();

(function($) {

	function updateMenuColumns( submenu ){
		var expCols = $('div.expandable',submenu);
		var maxHeight = ($(window).height() < 500?500:$(window).height()) - 86 - 68 - 48 - 20; // 86(header) 60(de sub header pana la submenu), 48(submenu-expandable), 20(expandable-submenu_bottom)
		var width,height,columns = [],col,colIndex,lastColContainer,els,el,neededCols,currentColHeight,dcols;
		for(var iterator = 0; iterator < expCols.length; iterator++){
			var currentCol = $(expCols[iterator]);
			width = currentCol.width();
			height = currentCol.height();
			if(width > 0 && height > 0 && height > maxHeight){
				col = currentCol.parent();
				colIndex = col.parent().children().index(col);
				lastColContainer = col.parent();
				els = currentCol.children();
				neededCols = [[]];
				currentColHeight = 0;
				for(var i = 0; i<els.length; i++){
					el = $(els[i]);
					if(!el.hasClass('dynamic-column')){ 
						if(currentColHeight + el.height() > maxHeight){
							neededCols.push(new Array());
							currentColHeight = 0;
						}
						neededCols[neededCols.length-1].push(el);
						currentColHeight += el.height();
					}
				}

				dcols = currentCol.children('.dynamic-column');
				while(dcols.length < neededCols.length){
					currentCol.append('<div class="dynamic-column"></div>');
					dcols = currentCol.children('.dynamic-column');
				}
				for(i = 0; i<dcols.length; i++){
					$(neededCols[i]).appendTo($(dcols[i]));
				}
				

				columns.push({
					elem:col,
					width:width,
					modWidth:width * (neededCols.length)
				});
			} else {
				columns.push({elem:currentCol.parent(),width:width});
			}
		}
		
		width = 0;
		submenuWidth = 0;
		for(i = 0; i < columns.length;i++) if(i == 0){ submenuWidth = (columns[i].modWidth > 0?columns[i].modWidth:columns[i].width)} else { width += (columns[i].modWidth > 0?columns[i].modWidth:columns[i].width); }
		submenuWidth += width + 1;
		var colWrapper = lastColContainer;
		for(iterator = 0; iterator < columns.length; iterator++){
			col = columns[iterator].elem;
			col.css('width',columns[iterator].modWidth);
			col.css('left',width+'px');
			if(colWrapper){
				if(iterator < columns.length - 1 ) $(colWrapper).css('right', columns[iterator + 1].modWidth);
				colWrapper = colWrapper.parent();
			}
		}
		$(colWrapper).css('width',submenuWidth+'px');
	}

	$(document).ready(function() {
		$('.submenu').hide();
		$('#language_select').hide();
		
		$('#menu ul li').click(function() {
			var show = $(this).children('.submenu').css('display') == 'none';
			$('.submenu').hide();
			$('#menu a.main').removeClass('open');
			if(show){
				$(this).children('a').addClass('open');				
				$(this).children('.submenu').toggle();
				updateMenuColumns(this);
			}
		});

		/*$('#menu ul li').hover(function() {},
			function() {
				$('.submenu').hide();
				$('#menu a.main').removeClass('open');
			});
		$('.submenu').hover(function(){},function() {
			$('.submenu').hide();
			$('#menu a.main').removeClass('open');
		});*/

		$('#languages').hover(
			function() { $('#language_select').show(); },
			function() { $('#language_select').hide(); }
		);
		$("#empty").parent().hide();

		$(window).resize(function(){
			var h = $(window).height();
			var w = $(window).width();
		});
		
		// **********************
		// mouse out/over
		// **********************
		
		function mouseOver(){
			$(this).not('ul').addClass('hover');
		}

		function mouseOut(){ 
			$(this).not('ul').removeClass('hover');
		}

		var config = {     
		 sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)     
		 interval: 110, // number = milliseconds for onMouseOver polling interval     
		 over: mouseOver, // function = onMouseOver callback (REQUIRED)     
		 timeout: 110, // number = milliseconds delay before onMouseOut     
		 out: mouseOut // function = onMouseOut callback (REQUIRED) 
		}; 
		$("#nav").children().hoverIntent( config );
	
		// **********************
		// products
		// **********************
		
		// clone list in products	
		var products_level3 = $('#nav li.first ul.level2 li.last ul.level3');
		var products_level3_cloned = products_level3.clone().addClass('extended');
		
		// remove the first three li's from first list
		products_level3.children().not(':eq(0),:eq(1),:eq(2)').remove();
		
		// remove the last three li's from second list
		products_level3_cloned.children().not(':eq(3),:eq(4),:eq(5)').remove();
		products_level3_cloned.insertAfter(products_level3);
		
		// **********************
		// services
		// **********************
		
		// modify and clone list in services	
		var services_level2 = $('li.services ul.level2').removeClass('container').wrap('<div class="container" />');
		var services_level2_cloned = services_level2.clone();
		
		// remove the first two li's from first list
		services_level2.children().not(':eq(0),:eq(1)').remove();
		
		// remove the last two li's from second list
		services_level2_cloned.children().not(':gt(1)').remove();		
		services_level2_cloned.addClass('last');	
		services_level2_cloned.insertAfter(services_level2);

		// **********************
		// company
		// **********************
		var company = $('li.company');
		var company_ul = $('li.company ul.level2');
		var company_li = company_ul.find('li');
		$('li.company').append('<ul class="level2 container"></ul>');
		var company_new = $('li.company ul.level2');
		
		company_li.each(function(index) {
		
			var el = $(this).addClass($(this).parent().attr('class'));
			company_new.append(el);
		});
		
		company_ul.remove();
		company_new.find('ul').remove();
 		company_new.find('li').removeClass('first');
 		company_new.find('li:first').addClass('first');
		
		$('#nav').find('a[id]').each(function() {
			return $.trim($(this).text()) === ''
		}).remove();
		
		$('#nav').children().each(function() {
			$(this).find('a:first').attr('href','javascript:void(0)');
		});
		
	});
})(jQuery);

