$(document).ready(function(){
  	var gEffectDuration = 180; /*millisecondes*/
	/*navigation of accordion 'fixe'*/
	function manageAccordion(e){
		var eBrother = e.parent().children("div[@class^='navFixeBody']");		
		var eGlobal = e.parent().parent();
		if(eBrother.css("display") == "none"){
			$(eGlobal).children().children("div[@class^='navFixeBody']").each(function (){	
				if($(this) != eBrother && $(this).css("display") != "none" ){
					var iAvailHeight = $(this).height();
					$(this).slideUp(gEffectDuration);
					$(this).parent().children(0).children(1).children(0).children("span").html('+');
				}
			});
			eBrother.slideDown(gEffectDuration);		
			eBrother.parent().children(0).children(1).children(0).children("span").html('-');
		}
	}
	
	/*management of accordion 'fixe' */
	if($("div[class='navFixe']")){ 
		/*init : set visible '+' link*/
		$("div[class='navFixe'] div[class='navFixeTitre']").each(function (){	
			var eSpan= $(this).children(1).children(0).children("span");
			eSpan.css({display: "block"});
			$(this).css({cursor: "pointer"});
		});

		/*overflow*/
		$("div[class='navFixe'] div[class='navFixeContent']").each(function (){	
			$(this).css({overflow: "hidden"});
		});
		
		/*events*/
		/*disable direct links*/
		$("div[class='navFixe'] div[class='navFixeTitre'] h2 a").click(function(){			
			manageAccordion ($(this).parent().parent().parent().parent());
			return false;
		});
		/*click  title*/
		$("div[class='navFixe'] div[class='navFixeTitre']").click(function(){
			manageAccordion ($(this));
		});
	}
	if($("div[class='navFixeInterieur']")){ 
		/*navigation of accordion 'zone droite*/
		function manageFixeAccordion(e){
			var eBrother = e.parent().children("div[@class^='navFixeBody']");		
			
			if(eBrother.css("display") == "none"){			
				eBrother.slideDown(gEffectDuration);		
				eBrother.parent().children(0).children(1).children(0).children("a[class='action']").html('-');
			} else {		
				eBrother.slideUp(gEffectDuration);		
				eBrother.parent().children(0).children(1).children(0).children("a[class='action']").html('+');
			}
			
		}
		
		/*management of accordion 'fixe' */
		if($("div[class='navFixeInterieur']")){ 
			var isFirst = true;
			var eNavTitre = $("div[class='navFixeInterieur'] div[class='navFixeTitre']");
			/*init : set visible '+' link*/		
			eNavTitre.each(function (){	
				var eSpan= $(this).children(1).children(0).children("a[class='action']");
				eSpan.css({display: "block"});
				$(this).css({cursor: "pointer"});
			});
			/*cursor for title zone*/
			eNavTitre.each(function (){	
				$(this).css({cursor: "pointer"});
			});
			/*hide all content zone except the first one*/
			$("div[class='navFixeInterieur'] div[class='nav']").each(function (){	
				if(isFirst) {
					$(this).children(0).children(1).children(0).children("a[class='action']").html('-');
					isFirst = false;
				} else {
					$(this).children("div[@class^='navFixeBody']").hide();
				}
			});
			/*events*/
			/*disable direct links*/
			$("div[class='navFixeInterieur'] div[class='navFixeTitre'] a[class='action']").click(function(){	
				$(this).blur();				
				manageFixeAccordion ($(this).parent().parent().parent());		
				return false;
			});		
			/*click  title*/
			$("div[class='navFixeInterieur'] div[class='navFixeTitre']").click(function(){
				manageFixeAccordion ($(this));
			});
		}
	}
    /*navigation through flash info*/
	if($("div[class='flashInfo']")){
		var lHeight = 14;
		var label = '';
		var eFlashInfo = $("div[class='flashInfo']");
		//apply css effects
		eFlashInfo.each(function (){	
			var eDiv = $(this).children("div:first");
			var eDevelop =$(this).children("a[@class^='more']");
			eDiv.css({lineHeight: lHeight+"px"});
			eDiv.attr("name", "_"+ eDiv.height());
			$(this).maxHeight = eDiv.height();
			eDiv.css({overflow: "hidden"});
			eDiv.height((lHeight*3));
			label = eDevelop.attr("name");
			eDevelop.attr("name", $(this).children("a[@class^='more']").html());
			eDevelop.attr("title", label);
			eDevelop.html(label);
		});
		if($("div[class='flashInfo'] a[@class^='more']")){
			var eDevelop = $("div[class='flashInfo'] a[@class^='more']");
			eDevelop.css("display", "block");		
			eDevelop.click(function(){	
				var eDiv = $(this).parent().children("div:first");
				//check if this item is already initialized
				var attr = eDiv.attr("name");	
				var iHInit = parseFloat(attr.substring(1));
				if(eDiv.height() == iHInit) {
					//apply css effects
					eDiv.css({overflow: "hidden"});
					eDiv.animate({
						height:(lHeight*3)+"px"
					}, gEffectDuration);
					label = $(this).attr("name");
					$(this).attr("name", $(this).html());
					$(this).attr("alt", label);
					$(this).attr("class", 'more');
					$(this).html(label);
				} else {
					//apply css effects
					eDiv.css({overflow: "visible"});
					eDiv.animate({
						height:iHInit+"px"
					}, gEffectDuration);
					label = $(this).attr("name");
					$(this).attr("name", $(this).html());				
					$(this).attr("title", label);					
					$(this).attr("class", 'more less');
					$(this).html(label);
				}
				return false;	
			});	
		}
	}  
	/*slide auto 'en bref' */
	if($("div[id='brevesContent']")){  
		var eDiv = $("div[id='brevesContent']");
		$.brevesContent = {
				i	: 0,
				d 	: 8000 /*milliseconds*/
		};
		/*à améliorer : déplacer les enfants li à mesure*/
		/*slide lis elements of "en bref" */
		$.brevesContent.slideContent = function () {
			eDiv = $("div[id='brevesContent']");
			var moveHeight = 4 - eDiv.children(0).children("ul").children("li:first-child").height();
			
			eDiv.children(0).children("ul").children("li:first-child").clone().insertAfter(eDiv.children(0).children("ul").children("li:last-child"));
			eDiv.children(0).children("ul").animate({
					top:moveHeight+"px"
			}, (gEffectDuration*2), modulate);
			function modulate(){				
				eDiv.children(0).children("ul").css({top:"8px"});					
				eDiv.children(0).children("ul").children("li:first-child").remove();
			}	
			
		}
		eDiv.children(0).css({position: "relative"});
		eDiv.children(0).children("ul").css({position: "absolute"});
		var maxHeight = $("div[id='brevesContent'] div[class='navFixeContent']").height();

		var interval = setInterval("$.brevesContent.slideContent()", $.brevesContent.d);	
	}
	/*navigation of accordion 'menu'*/
	function manageMenuAccordion(e){
		var eBrother = e.parent().children("div[@class^='navSubContent']");		
		
		if(eBrother.css("display") == "none"){			
			eBrother.slideDown(gEffectDuration);		
			eBrother.parent().children(0).children(1).children(0).children("a[class='action']").html('-');
		} else {		
			eBrother.slideUp(gEffectDuration);		
			eBrother.parent().children(0).children(1).children(0).children("a[class='action']").html('+');
		}
		
	}
	
	/*management of accordion 'fixe' */
	if($("div[class='navSub']")){ 
		/*init : set visible '+' link*/
		$("div[class='navSub'] div[class='navSubTitre']").each(function (){	
			var eA= $(this).children(1).children(0).children("a[class='action']");
			eA.css({display: "block"});
		});
		/*events*/
		/*disable direct links*/
		$("div[class='navSub'] div[class='navSubTitre'] a[class='action']").click(function(){	
			$(this).blur();				
			manageMenuAccordion ($(this).parent().parent().parent());		
			return false;
		});		
	}
	
	
	/*search form*/
	if($("#searchForm input[class='searchText']")){ 
		$("#searchForm input[class='searchText']").focus(function(){	
			if($(this).attr('value') == $(this).attr('title')){
				$(this).attr('value', '');
			}
		});
		$("#searchForm input[class='searchText']").blur(function(){	
			if($(this).attr('value') == ''){
				$(this).attr('value', $(this).attr('title'));
			}
		});			
	}
	
	
	
	/*zone press*/	
	if($("div[@class^='buroPresse']")) {
		var eC = $("div[@class^='buroPresse'] div[class='dataField']");
		var eB = $("div[@class^='buroPresse'] div[class='buttonField']");
		eC.css({overflow: "hidden"});
		var iHeight;
		var visible;
		var iAvailableHeight = eC.height();
		var scrollManage = (function (){
			if(iHeight > iAvailableHeight){
				eC.css({width: "344px"});			
				/*tuned scroll visible*/
				$("div[@class^='buroPresse'] div[@class^='slider']").css({display: "block"});
				$("div[@class^='buroPresse'] div[@class^='handle']").css({top: "0px"});
				var slider = $("div[@class^='buroPresse'] div[class='slider']").slider({ 			
					handle: '.handle',
					min: 0, 
					max: iHeight - iAvailableHeight, 
					slide: function (ev, ui) {
						eC.children("div[class='data']").css('top', '-' + ui.value + 'px');
					}, 
					stop: function (ev, ui) {
						eC.children("div[class='data']").animate({ 'top' : '-' + ui.value + 'px' }, 200, 'linear');
					}
				});
			} else {
				eC.css({width: "364px"});
				eC.children("div[class='data']").css({top: "0px"});
				$("div[@class^='buroPresse'] div[@class^='slider']").css({display: "none"});
			}
			

		});
		
		
		//init class
		/*eC.css({width: "344px"});*/
		eB.children('div').each(function(){	
			$(this).css({cursor: "pointer"});
		}); 		
		//only first text visible
		eC.children(0).children().each(function(){	
			$(this).css({display: "none"})
		}); 
		eB.children().each(function(){
			$(this).click(function(){		
				$(this).parent().parent().focus();				
				
				var className = new String($(this).attr("class"));
				var ident = className.substr(0, className.length-6);
				$("div[@class^='buroPresse'] div[class='"+visible+"Data']").css({display: "none"});				
				$("div[@class^='buroPresse'] div[class='"+ident+"Data']").css({display: "block"});
				visible = ident;
				iHeight = $("div[@class^='buroPresse'] div[@class^='"+ident+"Data']").height();
				//ui scroll management
				scrollManage();
			});		
		});		
		$("div[@class^='buroPresse'] div[@class^='firstData']").css({display: "block"});
		iHeight = $("div[@class^='buroPresse'] div[@class^='firstData']").height();
		visible = "first";
		//ui scroll management
		scrollManage();
		
		
	}
	/*navigation of accordion 'zone droite*/
	function manageZoneAccordion(e){
		var eBrother = e.parent().children("div[@class^='zoneFixeBody']");		
		
		if(eBrother.css("display") == "none"){			
			eBrother.slideDown(gEffectDuration);		
			eBrother.parent().children(0).children(1).children(0).children("a[class='action']").html('-');
		} else {		
			eBrother.slideUp(gEffectDuration);		
			eBrother.parent().children(0).children(1).children(0).children("a[class='action']").html('+');
		}
		
	}
	
	/*management of accordion 'fixe' */
	if($("div[class='zoneFixe']")){ 
		var isFirst = true;
		var eZoneTitre = $("div[class='zoneFixe'] div[class='zoneFixeTitre']");
		/*init : set visible '+' link*/		
		eZoneTitre.each(function (){	
			var eA= $(this).children(1).children(0).children("a[class='action']");
			eA.css({display: "block"});
		});
		/*cursor for title zone*/
		eZoneTitre.each(function (){	
			$(this).css({cursor: "pointer"});
		});
		/*hide all content zone except the first one*/
		$("div[class='zoneFixe']").each(function (){	
			if(isFirst) {
				$(this).children(0).children(1).children(0).children("a[class='action']").html('-');
				isFirst = false;
			} else {
				$(this).children("div[@class^='zoneFixeBody']").hide();
			}
		});
		/*events*/
		/*disable direct links*/
		$("div[class='zoneFixe'] div[class='zoneFixeTitre'] a[class='action']").click(function(){	
			$(this).blur();				
			manageZoneAccordion ($(this).parent().parent().parent());		
			return false;
		});		
		/*click  title*/
		$("div[class='zoneFixe'] div[class='zoneFixeTitre']").click(function(){
			manageZoneAccordion ($(this));
		});
	}
	
	/*global : print */
	if($("img[@class$='print']")){ 
		$("img[@class$='print']").click(function(){
			window.print();
			return false;
		});
	}
});
