var bodySize;

$(document).ready(function(){
	
	var largeur = $(window).width();
	var hauteur = $(window).height();
	
	manageBandeBlanche(largeur);
	manageFooter(largeur,hauteur);
	
	$(window).resize(function(){
		
		var largeur = $(window).width();
		var hauteur = $(window).height();
		
		manageBandeBlanche(largeur);
		manageFooter(largeur,hauteur);
		manageQuickLink(0);
			
 	});
	
	// ManageMenu
	
	$('.block-rich_megamenu .rich_megamenu li').hover(function(){
		
		devoile($(this));
	  
	},function() {
		
		cache($(this));		
	  
	});
	
	// Quick Link
	
	var noeud = $("#content-right #block-block-4")
	
	var min_position = getGetPositionQuickLink();
	
	var cssObj = {
		'position' : 'absolute',
		'margin-top' : '0px',
		'left' : '3px',
		'top' : min_position
	}
	
	$(noeud).css(cssObj);
	
	manageQuickLink(0);
	
	$(window).scroll( function() { manageQuickLink(0); } );
	
	var bodySize = $("body").height();
	
	setTimeout("onResizeBody()",100);
	
	//Accordéon
	
	$("#content #contenu .accordion").each(function(i){
													
		initAccordion($(this));
		
	});
	
	//Print
	
	$("#content-right .print").click( function() { impression(); return false; } );
	
	
	//PopIn sur tableau governance d'entreprise
	
	$("#content #contenu .tab_pop_in tbody td a").each(function(i){
													
		initPopin($(this));
		
	});
	
	$("#content #contenu .tab_pop_in tbody td a").hover(function(){
		
		devoilePopIn($(this));
	  
	},function() {
		
		cachePopIn($(this));		
	  
	});
	
	// Ajustement automatique de la taille des titres de page (h1) pour qu'ils restent sur une seule ligne
	//contentWidth = $("#content").css("width").substring(0,$("#contenu").css("width").length - 1);
	contentWidth = $("#content").width() - 60;
	if($("h1.title").outerWidth(true) >=  contentWidth) {
	  setFontSizeforMaxWidth($("h1.title"), contentWidth);
	}
	
	
});

/*
 * Definit la plus grande taille de police pour "element" qui permette de rentrer dans maxWidth
 */
// En general, candidat ideal pour une recherche dichotomique. Cependant, dans ce cas de figure, 
// une recherche sequentielle est beaucoup plus simple, et dans la plupart des cas plus rapide
function setFontSizeforMaxWidth(element, maxWidth){
  while(element.outerWidth(true) >=  maxWidth) {
    element.css("font-size", (parseInt(element.css("font-size")) - 1) + "px");
  }
}


function initPopin(noeud) {
	
	var cssObj = {
		'display' : 'block',
		'opacity' : 0
	}
	
	$(noeud).next().css(cssObj);
	
}


function devoilePopIn(noeud) {
	
	var noeud = $(noeud).next();
		
	$(noeud).stop();
	
	$(noeud).animate({ 
												
		'opacity' : 1
		
	}, 250);
	
	
}

function cachePopIn(noeud) {
	
	var noeud = $(noeud).next();
		
	$(noeud).stop();
	
	$(noeud).animate({ 
												
		'opacity' : 0
		
	}, 250);
	
	
}

function initAccordion(noeud) {
	
	var autoslide = true;
	
	if($(noeud).hasClass('noAutoSlide')) {
			
		autoslide = false;
			
	}
	
	var removeDeployed = false;
	var elemtoDeployed;
	
	var url = window.location.href;
	var elem = url.split('#');
	
	if(elem.length == 2){
		
		removeDeployed = true;
		elemtoDeployed = elem[1];
	
	}
	
	$('a[href~=#]').click(function(){
	
		document.location.href = "http://"+window.location.host+$(this).attr("href");
		window.location.reload();
	
	});

		
	$(noeud).find(".head").each(function(i){
		
		if(removeDeployed) {
			
			$(this).removeClass('deployed');
			if(i+1==elemtoDeployed) {
				$(this).addClass('deployed');
			}
		
		}
		
		if(!$(this).hasClass('noClick')) {
		
			$(this).css('cursor', 'pointer');
			
		}
		
		if(!$(this).hasClass('deployed')) {
			
			$(this).next().hide();
			
		} else {
			
			if($(this).hasClass('puce')) {
				
				accordionChangePuce($(this));
				
			}	
			
		}
		
		$(this).click( function(){
			
			if(!$(this).hasClass('noClick')) {
			
				if($(this).hasClass('deployed')) {
					
					if(autoslide) {
					
						deleteDeployedAll($(noeud));
						accordionHideAll($(noeud));
						
					} else {
						
						deleteDeployedOne($(this));
					
					}
					
				} else {
					
					if(autoslide) {
					
						deleteDeployedAll($(noeud));
					
					}
					
					$(this).addClass('deployed');
					
					if(autoslide) {
					
						accordionHideAll($(noeud));
					
					}
					
					$(this).next().slideDown();
					
					if($(this).hasClass('puce')) {
						
						accordionChangePuce($(this));
					
					}
					
				}
			
			}
			
		});
		
	});

}

function accordionChangePuce(noeud) {
	
	var lurl = $(noeud).css("background-image");
	lurl = lurl.replace("\"","").replace("\"","");
	var url = lurl.slice(4,-1);
	var tab_url = url.split(".");
	
	var extension = tab_url.pop();
	
	var new_puce = tab_url.join(".")+"_roll."+extension;
	
	$(noeud).css("background-image","url(\""+new_puce+"\")");
	
}

function accordionInitPuce(noeud) {
	
	var lurl = $(noeud).css("background-image");
	lurl = lurl.replace("\"","").replace("\"","");
	var url = lurl.slice(4,-1);
	var tab_url = url.split(".");
	
	var extension = tab_url.pop();
	
	var nom = tab_url.pop();
	
	if(nom.search("_roll") != -1) {
	
		nom = nom.slice(0,-5);
		
		var new_puce = tab_url.join(".")+"."+nom+"."+extension;
		
		$(noeud).css("background-image","url(\""+new_puce+"\")");
		
	}
	
}

function deleteDeployedAll(noeud) {
	
	$(noeud).find(".head").each(function(i){
		
		if($(this).hasClass('puce')) {
			
			accordionInitPuce($(this));
			
		}
		
	 	$(this).removeClass('deployed');
	 
	 });
										 
}

function accordionHideAll(noeud) {
	
	$(noeud).find(".head").each(function(i){
		
		if(!$(this).hasClass('deployed')) {
		
			$(this).next().slideUp();
			
		}
		
	});
	
}

function deleteDeployedOne(noeud) {
	
	if($(noeud).hasClass('puce')) {
		
		accordionInitPuce($(noeud));
		
	}
	
	$(noeud).removeClass('deployed');
	$(noeud).next().slideUp();
}

function onResizeBody() {
	
	if( $("body") != bodySize) {
		
		moveQuickLink();
		
		bodySize = $("body");
		
	}
		
}

function manageQuickLink(delay) {
	
	setTimeout("moveQuickLink()",delay);
	
}

function moveQuickLink() {
	
	var noeud = $("#content-right #block-block-4");
	
	var min_position = getGetPositionQuickLink();
	
	var position = $(document).scrollTop() +  $(window).height() / 2 - $(noeud).height() / 2 -130;
	
	if(position<min_position){position=min_position;};
	
	$(noeud).stop();
	
	$(noeud).animate({ 
		top: position
	}, 1200, "easeOutCubic" );
	
}

function getGetPositionQuickLink() {
	
  var min_position = 15;
  
  $("#content-right .block").each(function(i){
								
	  if(i<2) {
		  
		min_position = min_position + $(this).height();  
		
	  }
		
	});
	
	return min_position;
}

function impression() {
	
	window.print();
	
}

function devoile(element) {
	
	var noeud = $(element).find("div.ss-menu");
		
	$(noeud).stop();
	
	var cssObj = {
		'display' : 'block'
	}
	
	$(noeud).css(cssObj);
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		
		$(element).find("span").addClass('over');
		$(element).find("a").addClass('over');
		
	}
	
}

function cache(element) {
	
	var noeud = $(element).find("div.ss-menu");
	
	$(noeud).stop();
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		
		$(element).find("span").removeClass('over');
		$(element).find("a").removeClass('over');
		
	}
		
	$(noeud).css({'display' : 'none'});

	
}

function manageBandeBlanche(lar) {
	
	if(lar>1000) {
		
		if( ( (lar - 1000) % 2) == 0 ) {
			
			var largeurGauche = (lar - 1000) / 2;
			var largeurDroite = (lar - 990) / 2;
			
		} else {
			
			var largeurGauche = ((lar-1) - 1000) / 2;
			var largeurDroite = (lar - 990) / 2;
			
		}
		
		var cssObjGauche = {
			'width' : largeurGauche,
			'display' : 'block'
		}
		
		var cssObjDroite = {
			'width' : largeurDroite,
			'display' : 'block'
		}
		
		$("#bande_blanche_gauche").css(cssObjGauche);
		$("#bande_blanche_droite").css(cssObjDroite);
		
	} else {
			
		var cssObjGauche = {
			'width' : 0,
			'display' : 'none'
		}
		
		var cssObjDroite = {
			'width' : 0,
			'display' : 'none'
		}
		
		$("#bande_blanche_gauche").css(cssObjGauche);
		$("#bande_blanche_droite").css(cssObjDroite);
		
	}

}

function manageFooter(lar,haut) {
	
	var hauteurBande = 30;
	var largeurBande = 990;
	
	if(largeurBande < $(document).width()) {
		
		largeurBande = "100%";
	
	}
	
	if($(window).height() < ($("#global").height() + hauteurBande)) {
		
		var cssObjNoir = {
			/*'position' : 'absolute',
			'top' : $("#global").height(),
			'height' : 	hauteurBande,*/
			'width' : largeurBande
		}	
		
	} else {
	
		var cssObjNoir = {
			/*'position' : 'absolute',
			'top' : $(window).height() - hauteurBande,
			'height' : 	hauteurBande,*/
			'width' : largeurBande
		}
		
	}
	
	$("#footer").css(cssObjNoir);
	
}

