$(document).ready(function(){
	
	$('.galerie a').lightBox();
	
	//------------------------------ Menu ---------------------------------
	var timeo, timeo2, timeo3, curCatMenu, curCatOver, curCatOver2;
	curCatOver2 = -1;
	
	var setWidth = function(pos){
		$("#ssmenu").css('left','0');
		var contenerWidth = $("#ssmenu").width();
		$("#ssmenu").css('left', pos.left+'px');
		var tmpWidth = $("#ssmenu").width();
		if(tmpWidth < contenerWidth){ //on dépasse à droite, on va décaler le menu vers la gauche
			var tmp = contenerWidth - tmpWidth;
			$("#ssmenu").css('left', (pos.left - tmp - 20) +'px');
		}
	}
	
	$("#menu ul").hide();
	var mouseEnter = function(){
		var _id = $(this).attr('id');
		_id = _id.substr(1, _id.length);
		if(_id == curCatOver2){
			clearTimeout(timeo3);
		}
		clearTimeout(timeo);
		$("#menu li:not(.current) .a-cat").removeClass("over");
		$(this).addClass("over");
		curCatMenu = _id;
		var pos = $(this).offset();
		$("#ssmenu").html('');
		$("#ss"+_id).clone().prependTo("#ssmenu");
		$("#ssmenu").show();
		$("#ssmenu ul").show();
		$("#ssmenu").css('left', pos.left+'px');
		$("#ssmenu").css('top', (pos.top+47)+'px');
	};
	
	var mouseLeave = function(){
		timeo = setTimeout(function(){
			$("#ssmenu").hide();
			$(this).removeClass("over");
		}, 0);
		
		var _id = $(this).attr('id');
		curCatOver = _id.substr(1, _id.length);
		timeo2 = setTimeout(function(){
			_id = _id.substr(1, _id.length);
			$("#a"+_id).removeClass("over");
		}, 0);
	}
	
	var timeout;
	$("#menu .a-cat").mouseenter(function(){
		clearTimeout(timeo);
		clearTimeout(timeo2);
		clearTimeout(timeout);
		var _this = this;
		timeout = setTimeout(function(){
			mouseEnter.call(_this);
		}, 100);
	});
	
	$("#ssmenu").mouseenter(function(){
		clearTimeout(timeo);
		var _id = $("#ssmenu ul").attr('id');
		_id = _id.substr(2, _id.length);
		if (curCatOver == _id) {
			clearTimeout(timeo2);
		}
	});
	
	$("#menu .a-cat").mouseleave(function(){
		clearTimeout(timeout);
		mouseLeave.call(this);
	});
	
	
	
	$("#ssmenu").mouseleave(function(){
		if (!$("#li" + curCatMenu).hasClass("current")) {
			$(this).removeClass("over");
		}
		var _id = $(this).children('ul').attr('id');
		_id = _id.substr(2, _id.length);
		curCatOver2 = _id;
		timeo3 = setTimeout(function(){
			$("#ssmenu").hide();
			$("#a" + _id).removeClass("over");
		}, 0);
	});
});
