$(document).ready(function(){ resizeMenu("menuLogue");$("#contentMenuRelative").css("display","block"); });

/* Gestion de l'agrandissement du texte */

function reduireTexte(){	
	$("body").removeClass('bigger');
	$.post("/bodyClass", {"class":""});
	document.getElementById("smallerButton").className="smaller active";
	document.getElementById("biggerButton").className="bigger";
	resizeMenu("menuLogue");
	resizeTablePasseport();
	$("#contentMenuRelative").css("display","block");
	
	$("div[class*='resizeHeight']").css("height","");
	$("ul[class*='resizeHeight']").css("height","");
	resizeHeight();
	setBackgroundSpan();
	$("a.selectCustomize,ul.selectCustomize").each(function(index) {
						var width = $(this).width()*0.8;
						$(this).width(width);						
								});
}

function agrandirTexte(){	
	$("body").addClass('bigger');
	$.post("/bodyClass", {"class":"bigger"});
	document.getElementById("smallerButton").className="smaller";
	document.getElementById("biggerButton").className="bigger active";
	resizeMenu("menuLogue");
	resizeTablePasseport();
	$("div[class*='resizeHeight']").css("height","");
	$("ul[class*='resizeHeight']").css("height","");
	resizeHeight();
	setBackgroundSpan();
	$("a.selectCustomize,ul.selectCustomize").each(function(index) {
						var width = $(this).width()*1.25;
						$(this).width(width);						
								});
}

function resizeTablePasseport()
{
	if($("div.formTablePasseport") != null)
	{
		var height = $("div.formTablePasseport").height();
		$("ul.subhead li").each(function(){
			$(this).height(height-30);
		});
	}
}

/* Multi Blocs */

function multi_blocs_highlight(object){
	objectClass = object.className;
	objectClass = objectClass + " over";
	object.className = objectClass;
}
function multi_blocs_unHighlight(object){
	var reg=new RegExp("(over)", "g");
	objectClass = object.className;
	objectClass = objectClass.replace(reg,"");
	object.className = objectClass;
}

function highLightObject(objectJquery){
	if (objectJquery.hasClass("over")){
		objectJquery.removeClass("over");	
	}else{
		objectJquery.addClass("over");
	}	
}

function addOrRemoveClass(objectJquery,css,refreshPage){
	
	if (objectJquery.hasClass(css)){
		objectJquery.removeClass(css);	
	}else{
		objectJquery.addClass(css);
	}

	if (refreshPage){
		$("#contentMain").css("height","");
		$("#contentMenu").css("height","");
		resizeHeight();
		setBackgroundSpan();
	}

}

/* Tooltips */

var IE6=false;
$(document).ready(function(){
	var tooltip = document.createElement('div');	
	tooltip.id = "tooltip";
	$(tooltip).attr("class","helpBubble");
	
	var top = document.createElement('div');
	$(top).attr("class","top");
	var leftTop = document.createElement('div');
	$(leftTop).attr("class","left");
	var rightTop = document.createElement('div');
	$(rightTop).attr("class","right");
	var centerTop = document.createElement('div');
	$(centerTop).attr("class","center");
	top.appendChild(leftTop);
	top.appendChild(rightTop);
	top.appendChild(centerTop);	
	tooltip.appendChild(top);
	
	var middle = document.createElement('div');
	$(middle).attr("class","middle");
	tooltip.appendChild(middle);
	
	var bottom = document.createElement('div');
	$(bottom).attr("class","bottom");
	var leftBottom = document.createElement('div');
	$(leftBottom).attr("class","left");
	var rightBottom = document.createElement('div');
	$(rightBottom).attr("class","right");
	var centerBottom = document.createElement('div');
	$(centerBottom).attr("class","center");
	bottom.appendChild(leftBottom);
	bottom.appendChild(rightBottom);
	bottom.appendChild(centerBottom);
	
	tooltip.appendChild(bottom);
	
	var strChUserAgent = navigator.userAgent;   
	if(strChUserAgent.indexOf("MSIE 6") != -1) {
		var iframe = document.createElement('IFRAME');
		$(iframe).attr("src","/blank.html");
		tooltip.appendChild(iframe);
		IE6=true;
	}
	
	document.body.appendChild(tooltip);
	$(tooltip).hide();
					   
	$(".tooltip",this).each(function() { $(this).makeTooltip() });
});

jQuery.fn.makeTooltip = function() {
	var tooltipTimeout;
	
	$(this).attr("tooltip", $(this).attr("title"));
	$(this).attr("title", "");
	
	$(this).hoverIntent({
		sensitivity: 10, // number = sensitivity threshold (must be 1 or higher)
		interval: 30, // number = milliseconds for onMouseOver polling interval
		over: function() {

			$("#tooltip").find(".middle").eq(0).html($(this).attr("tooltip"));

			// determination de la position top du tooltip (au dessus, en dessous)
			var tooltipHeight = $("#tooltip").height();
			var tooltipWidth = $("#tooltip").width();			
			var windowHeight = $(document).height();
			var windowWidth = $(document).width();
			
			
			var eTop = $(this).offset().top;
			var eLeft = $(this).offset().left;
			var eWidth = $(this).width();
			
			if (IE6){
				$("#tooltip iframe").height(tooltipHeight-11);
				$("#tooltip iframe").width(tooltipWidth);
			
				$("#tooltip").css("top",""+(eTop-tooltipHeight)+"px");
			}
			$("#tooltip").css("top",""+(0+eTop-tooltipHeight)+"px");
			
			$("#tooltip").css("left",""+(5+eWidth+eLeft-tooltipWidth)+"px");			
			
			$("#tooltip").fadeIn(300);

			
			tooltipTimeout = setTimeout(function() {
				$("#tooltip").hide();
				//$("#tooltip").fadeOut(300);
			},8000);
		} ,
		timeout: 0, // number = milliseconds delay before onMouseOut
		out: function() {
			clearTimeout(tooltipTimeout);
			$("#tooltip").hide();
			//$("#tooltip").fadeOut(300);
		}
	});
}

/* Impression */

function imprimer(){
	window.print();
}


/* Controles */

function disableCtrlKeyCombination(e)
{
   //list all CTRL + key combinations you want to disable
   var forbiddenKey = 'v';
   var key;
   var isCtrl;
   if(window.event)
   {
       key = window.event.keyCode;     //IE
       if(window.event.ctrlKey)
           isCtrl = true;
       else
           isCtrl = false;
       }
   else
   {
      key = e.which;     //firefox
      if(e.ctrlKey){
         isCtrl = true;
      }else{
         isCtrl = false;
     }
   }
   //if ctrl is pressed check if other key is in forbidenKeys array
   if(isCtrl)
   {              
      if(forbiddenKey.toLowerCase() == String.fromCharCode(key).toLowerCase())
      {
         return false;
 	  }
      return true;
   }
}


/* Decompte */

function tableau_highlight(object)
{
 objectClass = object.className;
 objectClass = objectClass + " over";
 object.className = objectClass;
}
function tableau_unHighlight(object)
{
 var reg=new RegExp("(over)", "g");
 objectClass = object.className;
 objectClass = objectClass.replace(reg,"");
 object.className = objectClass;
}
function tableau_foldUnfold(object)
{
	var selected = object.className;
	var listeElementsTheme = $("div[@name=block]");
	for (var i=0; i<listeElementsTheme.length; i++) {
		listeElementsTheme[i].className='blockInside';
		}
	if ((selected == "blockInside focus") ||(selected == "blockInside focus over")){
		object.className = 'blockInside';
	} else {
		object.className = 'blockInside focus';
	}
}

/* Faq */

function faq_highlight(object)
{
 objectClass = object.className;
 objectClass = objectClass + " over";
 object.className = objectClass;
}
function faq_unHighlight(object)
{
 var reg=new RegExp("(over)", "g");
 objectClass = object.className;
 objectClass = objectClass.replace(reg,"");
 object.className = objectClass;
}
function faq_foldUnfold(object)
{
	var selected = object.className;
	var listeElements = $("div[@name=block]");
	for (var i=0; i<listeElements.length; i++) {
		listeElements[i].className='blockInside';
		}
	if ((selected == "blockInside focus") ||(selected == "blockInside focus over")){
		object.className = 'blockInside';
	} else {
		object.className = 'blockInside focus';
	}
}

/* Themes */

function theme_foldUnfold(object){
	var selected = object.className;
	var listeElementsDomaine = $("div[@name=block]");
	for (var i=0; i<listeElementsDomaine.length; i++) {
		
		listeElementsDomaine[i].className='blockInside';
		}
	if ((selected == "blockInside focus") ||(selected == "blockInside focus over")){
		object.className = 'blockInside';
	} else {
		object.className = 'blockInside focus';
	}
}

function theme_highlight(object)
{
	objectClass = object.className;
	objectClass = objectClass + " over";
	object.className = objectClass;
}
function theme_unHighlight(object)
{
	var reg=new RegExp("(over)", "g");
	objectClass = object.className;
	objectClass = objectClass.replace(reg,"");
	object.className = objectClass;
}

/* Popup */

function openArticlePopup(articleURL){window.open('/popup'+articleURL,'MalakoffMederic','height=550, width=770, resizable=yes, scrollbars=yes');}
function openArticlePopup(articleURL,width,height){window.open('/popup'+articleURL,'MalakoffMederic','height='+height+', width='+width+', resizable=yes, scrollbars=yes');}

/* Menu */

function resizeMenu(idMenu)
{
	//Récupération des positions qui nous interesse
	
	var menu =document.getElementById(idMenu);
	
	var position_menu; 
	var position_derniere_rubrique; 
	var position_dernier_fils;
	
	if(menu != null)
	{
		position_menu = getPosition(menu,false);
		position_derniere_rubrique =  getPosition($("#"+idMenu+">li:last").get(0),true);
		position_dernier_fils =  getPosition($("#"+idMenu+">li[class*=open]:last>ul>li:last").get(0),true);
		if(position_derniere_rubrique != null && position_dernier_fils != null)
		{
			var hauteur_menu = position_derniere_rubrique[1]-position_menu[1];
			var hauteur_totale = position_dernier_fils[1]-position_menu[1];
			if(hauteur_menu > hauteur_totale )
			{
				$("#"+idMenu).height(hauteur_menu);
			}
			else
			{
				$("#"+idMenu).height(hauteur_totale);
			}
		}
		else if (position_derniere_rubrique != null && position_dernier_fils == null)
		{
			var hauteur_menu = position_derniere_rubrique[1]-position_menu[1];
			$("#"+idMenu).height(hauteur_menu);
		}
		else if (position_derniere_rubrique == null && position_dernier_fils != null)
		{
			var hauteur_totale = position_dernier_fils[1]-position_menu[1];
			$("#"+idMenu).height(hauteur_totale);
		}
		else
		{
			$("#"+idMenu).height("380px");
		}
	}
}

function getPosition(element,initTop)
{
	if(element != null)
	{
		/*On récupère l'élément*/
	    var e = element;
		
	    var left = 0;
	    var top = 0;
	    if(initTop) { top = e.offsetHeight;}
	 
	    /*Tant que l'on a un élément parent*/
	    while (e.offsetParent != undefined && e.offsetParent != null)
	    {
	        /*On ajoute la position de l'élément parent*/
	        left += e.offsetLeft + (e.clientLeft != null ? e.clientLeft : 0);
	        top += e.offsetTop + (e.clientTop != null ? e.clientTop : 0);
	        e = e.offsetParent;
	    }
	   
	    return new Array(left,top);
	}
	else { return null};
}

function uncheckOthers(checkbox){

	var form=checkbox.form;
	for(var i=0;i<form[checkbox.name].length;i++){
		var currentCheckbox=form[checkbox.name][i];
		if ( currentCheckbox!= checkbox){
			currentCheckbox.checked=false;
		}
	}

}

function enableInput(id){
	var element=$("#"+id);
	element.attr("disabled","");
	element.removeClass("disabled");
}

function disableInput(id){
	var element=$("#"+id);
	element.attr("disabled","disabled");
	element.addClass("disabled");
}

function readOnlyInput(id){
	var element=$("#"+id);
	element.attr("readonly","true");
	element.addClass("disabled");
}

function nonReadOnlyInput(id){
	var element=$("#"+id);
	element.attr("readonly","");
	element.removeClass("disabled");
}

function deselect(id){
	$("#"+id).attr("checked","");
}


/* GESTION DES ANGLES */

var pas=30;
var contentPage;

function setArrondiBackground(identifiant){
	var offsetContentPage=contentPage.offset();
	
	var bottomContentPage=offsetContentPage.top+contentPage.height();
	var element=$(identifiant);
	
	if (element.css("display")!="none"){
		var offsetElement=element.offset();

		var reg=new RegExp("(position[0-9]{2,2})", "g");
		var object=element.get(0);
		objectClass = object.className;
		objectClass = objectClass.replace(reg,"");
		object.className = objectClass;
		
		var difference=bottomContentPage-offsetElement.top;

		var numero=Math.floor(difference/pas);
		var classCss="position";
		if (numero>=10){
			classCss+="10";
		}else{
			classCss+="0"+numero;
		}

		element.addClass(classCss);
	}
}

function setBackgroundSpan(){
	contentPage=$("#contentMain");
	$("span.topCorners",contentPage).each(function(){
		setArrondiBackground(this);
	});
	$("span.bottomCorners",contentPage).each(function(){
		setArrondiBackground(this);
	});
	
}
