$(function(){
	
	//Login header : image
	//----------------------------
	if($(".header_utils_login").length>0){
		$(".header_utils_login").each(function(){			
			var div = $(this);
			var errorMessage = div.children().children("#footlogin_form").length>0 && div.children().children("#footlogin_form").children("p.error_message").length>0;
			div.children().children("#footlogin_form").hide();
			if(errorMessage){
				div.children().children("#footlogin_form").show();
				$( "#header-form-connection" ).dialog();
			}
			if(div.children("a").length>0){
				$(div.children("a")).attr('href', 'javascript:void(0);');

				$(div.children("a")).click(function(){	
					div.children().children("#footlogin_form").show();

					if($("#header-form-connection").length>0){
						$( "#header-form-connection" ).dialog();
					}
				});
			}			
		});
	}
	if($("#header-form-connection-error").length>0){
		$( "#header-form-connection-error" ).dialog();
	}
	
	
	
	
	//News : visionneur a onglets
	//---------------------------------------------
	if($(".list_news_tabs").length>0){
		$(".list_news_tabs").each(function(){
			var composant = $(this);
			//pages
			composant.children().children(".list_news_tabs_page_1").show();
			composant.children().children(".list_news_tabs_page_2").hide();
			composant.children().children(".list_news_tabs_page_3").hide();
			//pagination
			var nav = $(this).children().children().children().children().children(".list_news_tabs_nav");
			var prec = nav.children(".list_news_tabs_nav_btnprec");
			var b1 = nav.children(".list_news_tabs_nav_btn1");
			var b2 = nav.children(".list_news_tabs_nav_btn2");
			var b3 = nav.children(".list_news_tabs_nav_btn3");
			var suiv = nav.children(".list_news_tabs_nav_btnnext");
			prec.hide();
			b1.addClass("selected");
			nav.children(".list_news_tabs_nav_btn").each(function(){
				$(this).hover(
					function(){$(this).addClass("hover");},
					function(){$(this).removeClass("hover");}				
				);
				$(this).click(function(){
					b1.removeClass("selected");
					b2.removeClass("selected");
					b3.removeClass("selected");
					$(this).addClass("selected");
				});
			});
			prec.each(function(){
				$(this).click(function(){
					if(composant.children().children(".list_news_tabs_page_2").css("display")=='block'){
						composant.children().children(".list_news_tabs_page_1").show();
						composant.children().children(".list_news_tabs_page_2").hide();
						composant.children().children(".list_news_tabs_page_3").hide();
						prec.hide();
						b2.removeClass("selected");
						b1.addClass("selected");
					}else if(composant.children().children(".list_news_tabs_page_3").css("display")=='block'){
						composant.children().children(".list_news_tabs_page_1").hide();
						composant.children().children(".list_news_tabs_page_2").show();
						composant.children().children(".list_news_tabs_page_3").hide();
						suiv.show();
						b3.removeClass("selected");
						b2.addClass("selected");
					}	
				});
			});
			b1.each(function(){
				$(this).click(function(){
					composant.children().children(".list_news_tabs_page_1").show();
					composant.children().children(".list_news_tabs_page_2").hide();
					composant.children().children(".list_news_tabs_page_3").hide();
					b2.removeClass("selected");
					b3.removeClass("selected");
					b1.addClass("selected");
					prec.hide();
					suiv.show();
				});
			});
			b2.each(function(){
				$(this).click(function(){
					composant.children().children(".list_news_tabs_page_1").hide();
					composant.children().children(".list_news_tabs_page_2").show();
					composant.children().children(".list_news_tabs_page_3").hide();
					b1.removeClass("selected");
					b3.removeClass("selected");
					b2.addClass("selected");
					prec.show();
					suiv.show();
				});
			});
			b3.each(function(){
				$(this).click(function(){
					composant.children().children(".list_news_tabs_page_1").hide();
					composant.children().children(".list_news_tabs_page_2").hide();
					composant.children().children(".list_news_tabs_page_3").show();
					b1.removeClass("selected");
					b2.removeClass("selected");
					b3.addClass("selected");
					prec.show();
					suiv.hide();
				});
			});
			suiv.each(function(){
				$(this).click(function(){
					if(composant.children().children(".list_news_tabs_page_1").css("display")=='block'){
						composant.children().children(".list_news_tabs_page_1").hide();
						composant.children().children(".list_news_tabs_page_2").show();
						composant.children().children(".list_news_tabs_page_3").hide();
						prec.show();
						b1.removeClass("selected");
						b2.addClass("selected");
					}else if(composant.children().children(".list_news_tabs_page_2").css("display")=='block'){
						composant.children().children(".list_news_tabs_page_1").hide();
						composant.children().children(".list_news_tabs_page_2").hide();
						composant.children().children(".list_news_tabs_page_3").show();
						suiv.hide();
						b2.removeClass("selected");
						b3.addClass("selected");
					}
				});
			});
		});
	}

	/*
	 *	Enrich behaviour of Pagination in news page 
	 */
	$("div.commons_basicNewsList div.content div.items").each(function(){
		
		//var nbpages_string = $(this).children("div.listing_page").length + "";
		//var nbpages_int = $(this).children("div.listing_page").length;
		$(this).children("div.loader_pagination").children("a.gotofirstpage").click(function(){
			$("div.commons_basicNewsList div.content div.emphase").show(); 
		});
		$(this).children("div.loader_pagination").children("a.gotopreviouspage").click(function(){
			var page_int = parseInt($(this).next("span.currentpage").text()) - 1;		
			if(page_int == 0){
				$("div.commons_basicNewsList div.content div.emphase").show(); 
			}
		});
		$(this).children("div.loader_pagination").children("a.gotonextpage").click(function(){
			$("div.commons_basicNewsList div.content div.emphase").hide(); 	
		});
		$(this).children("div.loader_pagination").children("a.gotolastpage").click(function(){
			$("div.commons_basicNewsList div.content div.emphase").hide(); 
		});
	});
	
	
	var lastPopin = null
	var lastPopinVisited=null;
	/*
	 * Pop-in behaviour in Adherent page 
	 * -> Open popup 
	 */
	$("div.ffc_listeAdherent .content .allAlphabet ul.listPerLetter li a.infobulle").each(function(){
		$(this).attr("href","javascript:void(0);");
		$(this).click(function(){
			$(this).addClass("currentlyVisited");			
			//var popin = $(this).parent().children(".infobulle_content");
			var popin = $(this).next(".infobulle_content");
			var screenWidth =0;
			var screenHeight=0;
			var scrollTop = 0;
			var scrollLeft = 0;
			 
			 
		   if($.browser.msie){//ie 		   
		     screenWidth = document.body.offsetWidth;
			 screenHeight = document.body.offsetHeight;
			 scrollTop = document.documentElement.scrollTop; 
			 scrollLeft = document.documentElement.scrollLeft;
			 
			 /*light hack to move popup up*/
			 scrollTop-=200;
		   }else{//not ie
			   screenWidth = window.innerWidth;
			   screenHeight = window.innerHeight;
			   scrollTop = window.pageYOffset; 
			   scrollLeft = window.pageXOffset;	
		   }
		   
		 
		   
		  //alert('screen.height = '+screenHeight+' - '+'screen.width = '+screenWidth);
		  //alert('scrollTop = '+scrollTop+' - '+'scrollLeft = '+scrollLeft);
	   
		   var left = ((screenWidth-popin.width())/2+scrollLeft);
		   var top = ((screenHeight-popin.height())/2+scrollTop);
		   popin.css('left',left);
		   popin.css('top',top);
				
			
			//close previous popin
			if(lastPopin!=null){
				lastPopin.hide();
				lastPopin = null;
			}
			if(lastPopinVisited!=null){
				lastPopinVisited.removeClass("currentlyVisited");
				lastPopinVisited = null;
			}
			
			//open popin
			if(popin != null){
				popin.show();
				lastPopin = popin;
				lastPopinVisited = $(this);
			}
		});
	});
	
	/*
	 * Pop-in behaviour in Adherent page 
	 * -> Close popup 
	 */
	$("div.ffc_listeAdherent .content .allAlphabet ul.listPerLetter li .infobulle_content a.closeImage").each(function(){
		$(this).attr("href","javascript:void(0);");
		$(this).click(function(){			
			if(lastPopin!=null){
				lastPopin.hide();
				lastPopin = null;
			}
			if(lastPopinVisited!=null){
				lastPopinVisited.removeClass("currentlyVisited");
				lastPopinVisited = null;
			}
		});
	});
	
	
	
	/*
	 * Pagination de l'agenda
	 * 
	 */
	
	$("div.ffc_Agenda div.content div.items, div.ffc_EtudeEtMarketing div.content div.items, div.ffc_JuridicAndFormation div.content div.items").each(function(){
		var nbpages_string = $(this).children("div.listing_page").length + "";
		var nbpages_int = $(this).children("div.listing_page").length;
		
		//Inactive the link to change page only by js
		$(this).children("div.ffc_pagination").children("a.gotofirstpage").attr("href","#");
		$(this).children("div.ffc_pagination").children("a.gotopreviouspage").attr("href","#");
		$(this).children("div.ffc_pagination").children("a.gotonextpage").attr("href","#");
		$(this).children("div.ffc_pagination").children("a.gotolastpage").attr("href","#");
		
		
		
		$(this).children("div.ffc_pagination").children("a.gotofirstpage").click(function(){
			var page_string = "1";
			var page_int = 1;
			$(this).next().next("span.currentpage").text(page_string);
			$(this).parent("div.ffc_pagination").children("a").show();
			if(page_int >= nbpages_int){
				$(this).next().next().next("a.gotonextpage").hide();
				$(this).next().next().next().next("a.gotolastpage").hide();
			}
			if(page_int <= 1){
				$(this).hide();
				$(this).next("a.gotopreviouspage").hide();
			}
			$(this).parent("div.ffc_pagination").parent("div.items").children("div.listing_page").hide();
			$(this).parent("div.ffc_pagination").parent("div.items").children("div.listing_page:first").show();
		});
		$(this).children("div.ffc_pagination").children("a.gotopreviouspage").click(function(){
			var page_string = (parseInt($(this).next("span.currentpage").text()) - 1) + "";
			var page_int = parseInt($(this).next("span.currentpage").text()) - 1;
			$(this).next("span.currentpage").text(page_string);
			$(this).parent("div.ffc_pagination").children("a").show();
			if(page_int >= nbpages_int){
				$(this).next().next("a.gotonextpage").hide();
				$(this).next().next().next("a.gotolastpage").hide();
			}
			if(page_int <= 1){
				$(this).prev("a.gotofirstpage").hide();
				$(this).hide();
			}
			$(this).parent("div.ffc_pagination").parent("div.items").children("div.listing_page").hide();
			$(this).parent("div.ffc_pagination").parent("div.items").children("div.listing_page").each(function(i){
				if(i==page_int-1){
					$(this).show();
				}
			});
		});
		$(this).children("div.ffc_pagination").children("a.gotonextpage").click(function(){
			var page_string = (parseInt($(this).prev("span.currentpage").text()) + 1) + "";
			var page_int = parseInt($(this).prev("span.currentpage").text()) + 1;
			$(this).prev("span.currentpage").text(page_string);
			$(this).parent("div.ffc_pagination").children("a").show();
			if(page_int >= nbpages_int){
				$(this).hide();
				$(this).next("a.gotolastpage").hide();
			}
			if(page_int <= 1){
				$(this).prev().prev().prev("a.gotofirstpage").hide();
				$(this).prev().prev("a.gotopreviouspage").hide();
			}
			$(this).parent("div.ffc_pagination").parent("div.items").children("div.listing_page").hide();
			$(this).parent("div.ffc_pagination").parent("div.items").children("div.listing_page").each(function(i){
				if(i==page_int-1){
					$(this).show();
				}
			});
		});
		$(this).children("div.ffc_pagination").children("a.gotolastpage").click(function(){
			var page_string = nbpages_string;
			var page_int = nbpages_int;
			$(this).prev().prev("span.currentpage").text(page_string);
			$(this).parent("div.ffc_pagination").children("a").show();
			if(page_int >= nbpages_int){
				$(this).prev("a.gotonextpage").hide();
				$(this).hide();
			}
			if(page_int <= 1){
				$(this).prev().prev().prev().prev("a.gotofirstpage").hide();
				$(this).prev().prev().prev("a.gotopreviouspage").hide();
			}
			$(this).parent("div.ffc_pagination").parent("div.items").children("div.listing_page").hide();
			$(this).parent("div.ffc_pagination").parent("div.items").children("div.listing_page:last").show();
		});
	});
	
	
	
});




