$(document).ready(function(){
	
	
	/* Menu Top */
	$( "div#topmenu-content ul#topmenu li" ).hover(function(){
		var DOM = $(this).find('ul.submenu');
		DOM.show();
		activeSubMenu(DOM);
	}, function() {
		var DOM = $(this).find('ul.submenu');
		DOM.hide();
	});
	
	var countLiTopMenu = ( $( "div#topmenu-content ul#topmenu > li" ).length - 1 );
	
	/*alert("C'est un test, pas de panique Rik !!! : "+countLiTopMenu);*/
	
	$( "div#topmenu-content ul#topmenu > li" ).each(function(i){ 
		if (countLiTopMenu == i) {
			$(this).addClass("last-child");
		}
	});
	
	$('.webpage-line').hover(
	function(){
		$(this).addClass('hovered');
	},
	function(){
		$(this).removeClass('hovered');
	}

	);
	
	function activeSubMenu(DOMul) {
		
		if (!DOMul.hasClass("resized")) {
			
			var pos 			= DOMul.position();
			var widthParentUL 	= DOMul.parent().parent().width();
			var posParentLI 	= DOMul.parent().position();
			var nbColumns 		= DOMul.find( "li.column_submenu" ).size();
			var widthMin		= (nbColumns * 182.5);

			DOMul.width(widthMin);
			
			var widthUL			= DOMul.width();
			var widthUlAll		= widthUL + 19 + 2; // Width of UL AND Padding-left AND two borders
			var sizeColumn 		= 0;
			var up 				= '\n';
			
			DOMul.find( "li.column_submenu" ).each(function(){ 
				if ( sizeColumn < $(this).find('ul').height() ) {
					sizeColumn = $(this).find('ul').height();
				}
				DOMul.height(sizeColumn);
			});
			DOMul.find( "li.column_submenu" ).each(function(){
				$(this).find('ul').height(sizeColumn);
			});
			
			if ( (pos.left + widthMin) > (widthParentUL - 24) ) {
				var positionLeft = (widthParentUL - 24) - widthUlAll;
				DOMul.css("left", positionLeft);
			}
			
			/*if (widthUL < widthMin) {
				var positionLeft = (widthUL - widthMin);
				DOMul.css("left", positionLeft);
			}*/
			
			if (window.console && window.console.firebug){
				var txt = 'Position of UL Child : ' + pos.left + up;
				txt += 'Width of UL Child : ' + widthUL + up;
				txt += 'Width of Parent UL : ' + widthParentUL + up;
				txt += 'Position of Parent LI : ' + posParentLI.left + up;
				txt += 'Width total of UL child : ' + widthUlAll + up;
				txt += 'Numbers columns : ' + nbColumns + up;
				txt += 'Height max columns : ' + sizeColumn;
				console.debug( txt );
			}
			
			DOMul.addClass("resized");
			
		}
		
	}
	
	// Email obfuscation
	$('a.email,span.email a').each(function()
	{
		var link = $(this).attr('href');
		var text = $(this).html();
		
		link = link.replace(/\(at\)/g,'@');
		link = link.replace(/\(dot\)/g,'.');
		
		text = text.replace(/\(at\)/g,'@');
		text = text.replace(/\(dot\)/g,'.');
		
		$(this).attr('href', link);
		$(this).html( text);
	});
	

});

	function openLightbox(url,width,height)
	{
		$.fancybox(
		{
        	'autoDimensions'	: false,
			'width'         		: width,
			'height'        		: height,
			'transitionIn'		: 'none',
			'transitionOut'	: 'none',
			'type'				: 'iframe',
			'scrolling'			: 'no',
			'href'					: url

		}
	);
	}
