$(document).ready(function(){
	/******
	* MENU
	******/
	initMenu()
	// HOVER + CLICK
	$("#menu h2").hover(
		function() {
			$(this).addClass("over");
		}, 
		function() {
			$(this).removeClass("over");
		}
	);
	$("#menu .selected a").addClass("over");
	$("#menu ul .selected a").removeClass("over");
	$("#menu h2").click(
		function () {
			selectMenuItem($(this));
		}
	); 
	/******
	* TOOLTIP
	******/
	$().mousemove(function(e){
		mouseX = e.pageX;
		mouseY = e.pageY;
	}); 
	$(".tooltip").css({display:"block"});
	$(".gallery li").hover(
		function() {
			showToolTip($(this).children("a").attr("rel"));
		}, 
		function() {
			hideAllToolTips();
		}
	);
	/******
	* ARTICLES MODAL
	******/
	$(".articles a").removeAttr("href");
	$(".articles a").click(
		function() {
			var art_id = $(this).attr("rel");
			$.modal($("#modal_article"), {
				overlay:95,
				close:false,
				closeClass:"btn_close",
				onShow:function(){
					var modal = this;				
					$("#article").load(
						"pages/article.php?ajax=1&id="+art_id,
						null,
						function(){$("#modal_article .modalClose").click(function(){modal.close();});}
					);
					//$("html head link[href='css/passage-porte.css']").attr({href: ""});
					$("html head").append("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/print_modal.css\" media=\"print\" />");
				}
			});
		}
	);
	/******
	* WORKS MODAL
	******/
	$(".works a").removeAttr("href");
	$(".works a").click(
		function() {
			var art_id = $(this).attr("rel");
			$.modal($("#modal_work"), {
				overlay:95,
				close:false,
				closeClass:"btn_close",
				onShow:function(){
					var modal = this;				
					$("#work").load(
						"pages/work.php?ajax=1&id="+art_id,
						null,
						function(){
							$("#modal_work .modalClose").click(function(){modal.close();});
							makeVisus(); 
						}
					);
					//$("html head link[href='css/passage-porte.css']").attr({href: ""});
					$("html head").append("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/print_modal.css\" media=\"print\" />");
				}
			});
		}
	);
	/******
	* MEDIAS MODAL
	******/
	$(".medias a").removeAttr("href");
	$(".medias a").click(
		function() {
			var art_id = $(this).attr("rel");
			$.modal($("#modal_media"), {
				overlay:95,
				close:false,
				closeClass:"btn_close",
				onShow:function(){
					var modal = this;				
					$("#media").load(
						"pages/media.php?ajax=1&id="+art_id,
						null,
						function(){
							$("#modal_media .modalClose").click(function(){modal.close();});
							makeVisus(); 
						}
					);
					$("html head").append("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/print_modal.css\" media=\"print\" />");
				}
			});
		}
	);
	/******
	* VISUS
	******/
	makeVisus();
});
/******
* VISUS
******/
function makeVisus () {
	var i = 1;
	$(".menu_visus li").each ( function () {
		var j = i;
		$(this).css("cursor", "pointer");
		$(this).click( function () {
			swapVisus(j);
			$(".menu_visus li").each ( function () {
				$(this).removeClass("selected");
			} );
			$(this).addClass("selected");
		} );
		i++;
	} );
	$(".menu_visus li:first").addClass("selected");
}
function swapVisus(n) {
	var i = 1;
	$(".visu ul li").each ( function () {
		if (i==n) $(this).show();
		else  $(this).hide();
		i++;
	} );
}
/******
* MENU
******/
function initMenu() {
	$("#menu .expanded").removeClass("expanded"); 
	$("#menu h2").css({ cursor:"pointer" });
	// selected
	var s = $("#menu li.selected h2");
	collaspeAllMenus();
	if(s) selectMenuItem(s);
}
function collaspeAllMenus (s) {
	$("#menu li:has(ul)").removeClass("selected");
	$("#menu ul").hide(s);
	/*
	$("#menu").children().removeClass("selected");
	$("#menu ul").hide(s);
	*/
}
function selectMenuItem(i) {
// ici je dois differencier 2 cas ... un sous menu et un bouton normal
	var p = i.parent();
	var n = i.next();
	if(p.hasClass("selected") == false) collaspeAllMenus("fast");
	p.addClass("selected");
	n.show("fast");
}
/******
* TOOLTIP
******/
function showToolTip(i) {
	current_tooltip = $("#tooltip_"+i);
	$(document.body).bind('mousemove', updateToolTipPos);
	current_tooltip.css({visibility:"visible"});
	updateToolTipPos();
}
function hideAllToolTips() {
	current_tooltip = null;
	$(".tooltip").css({visibility:"hidden"});
}
function updateToolTipPos(){
	if(current_tooltip == null || $("#modalOverlay").css("display") == "block") {
		$(document.body).unbind('mousemove', updateToolTipPos);
	} else if (current_tooltip != null && mouseX != null) {
		var tho = 15; // tooltip left offset
		var tvo = 15; // tooltip left offset
		var ww = $(window).width();
		var wh = $(window).height();
		var tw = current_tooltip.width();
		var th = current_tooltip.height();
		var xpos = mouseX+tho;
		var ypos = mouseY+tvo;
		if(xpos+tw > ww) xpos = xpos - tw - 2*tho;
		if(ypos+th > wh) ypos = ypos - ((ypos+th) - wh) - 2*tvo;
		current_tooltip.css({left:xpos, top:ypos});
		//$(".lang").html(xpos + " , " + ypos);
	
	}
}