$(function(){ 
	//Paragraphes depliables
	//---------------------------------------------
	if($("div.question").length>0){
		$("div.question").each(function(){
			$(this).next("div.reponse").hide();
			$(this).css({"cursor":"pointer"});
			$(this).click(function(){
				$(this).next("div.reponse").toggle();
			});
		});
	}
	
	//Picture gallery
	//---------------------------------------------
	//....drag&drop sur la popin
	//$("div.popin").draggable();
	//utilisation de code javascript
	//....supprime les href
	$("div.items").children("a.picture_thumbnail").each(function(){
		$(this).attr("href","javascript:void(0);");
	});
	$("div.items").children("div.picture_page").children("a.picture_thumbnail").each(function(){
		$(this).attr("href","javascript:void(0);");
	});
	$("div.items").children("div.popin").children("div.picture_details").each(function(){
		$(this).children().children("a.closebutton").attr("href","javascript:void(0);");
		$(this).children("a.previousbutton").attr("href","javascript:void(0);");
		$(this).children("a.nextbutton").attr("href","javascript:void(0);");
	});
	$("div.items").children("div.pagination").each(function(){
		$(this).children("a").each(function(){
			$(this).attr("href","javascript:void(0);");
		});
	});
	//....cache les div details et les centre
	$("div.items").children("div.popin").hide().children("div.picture_details").hide();
	var left_int = $(window).width() / 2 - $("div.items").children("div.popin").children("div.picture_details").width() / 2;
	$("div.items").children("div.popin").children("div.picture_details").css("left", left_int); 
	//....cache les div page (pagination)
	$("div.items").each(function(){
		$(this).children("div.picture_page").hide();
		$(this).children("div.picture_page:first").show();
	});
	//....affiche la div details quand click sur le thumbnail
	$("div.items").each(function(){
		$(this).children("a.picture_thumbnail").each(function(i){
			$(this).click(function(){
				$(this).parent("div.items").children("div.popin").toggle().children("div.picture_details").each(function(j){
					if(j==i){
						$(this).toggle();
					}
				});
			});
		});
		$(this).children("div.picture_page").children("a.picture_thumbnail").each(function(i){
			$(this).click(function(){
				$(this).parent("div.picture_page").parent("div.items").children("div.popin").toggle().children("div.picture_details").each(function(j){
					if(j==i){
						$(this).toggle();
					}
				});
			});
		});
	});
	//....boutons de la div details
	$("div.items").children("div.popin").children("div.picture_details").children().children("a.closebutton").click(function(){
		$(this).parent().parent("div.picture_details").hide().parent("div.popin").toggle();
	});
	$("div.items").children("div.popin").children("div.picture_details").each(function(){
		if($(this).prev("div.picture_details").length==0){
			$(this).children("a.previousbutton").hide();
		}
		if($(this).next("div.picture_details").length==0){
			$(this).children("a.nextbutton").hide();
		}
	});
	$("div.items").children("div.popin").children("div.picture_details").children("a.previousbutton").click(function(){
		if($(this).parent("div.picture_details").prev("div.picture_details").length>0){
			$(this).parent("div.picture_details").toggle().prev("div.picture_details").show();
		}
	});
	$("div.items").children("div.popin").children("div.picture_details").children("a.nextbutton").click(function(){
		if($(this).parent("div.picture_details").next("div.picture_details").length>0){
			$(this).parent("div.picture_details").toggle().next("div.picture_details").show();
		}
	});
	//....pagination
	$("div.items").each(function(){
		var nbpages_string = $(this).children("div.picture_page").length + "";
		var nbpages_int = $(this).children("div.picture_page").length;
		$(this).children("div.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.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.pagination").parent("div.items").children("div.picture_page").hide();
			$(this).parent("div.pagination").parent("div.items").children("div.picture_page:first").show();
		});
		$(this).children("div.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.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.pagination").parent("div.items").children("div.picture_page").hide();
			$(this).parent("div.pagination").parent("div.items").children("div.picture_page").each(function(i){
				if(i==page_int-1){
					$(this).show();
				}
			});
		});
		$(this).children("div.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.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.pagination").parent("div.items").children("div.picture_page").hide();
			$(this).parent("div.pagination").parent("div.items").children("div.picture_page").each(function(i){
				if(i==page_int-1){
					$(this).show();
				}
			});
		});
		$(this).children("div.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.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.pagination").parent("div.items").children("div.picture_page").hide();
			$(this).parent("div.pagination").parent("div.items").children("div.picture_page:last").show();
		});
	});
	//Pagination liste de news
	//---------------------------------------------
	//....supprime les href
	$("div.commons_basicNewsList div.content div.items").children("div.loader_pagination").each(function(){
		$(this).children("a").each(function(){
			$(this).attr("href","javascript:void(0);");
		});
	});
	//....cache les div page (pagination)
	$("div.commons_basicNewsList div.content div.items").each(function(){
		$(this).children("div.listing_page").hide();
		$(this).children("div.listing_page:first").show();
	});
	//....pagination
	$("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(){
			var page_string = "1";
			var page_int = 1;
			$(this).next().next("span.currentpage").text(page_string);
			$(this).parent("div.loader_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.loader_pagination").parent("div.items").children("div.listing_page").hide();
			$(this).parent("div.loader_pagination").parent("div.items").children("div.listing_page:first").show();
		});
		$(this).children("div.loader_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.loader_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.loader_pagination").parent("div.items").children("div.listing_page").hide();
			$(this).parent("div.loader_pagination").parent("div.items").children("div.listing_page").each(function(i){
				if(i==page_int-1){
					$(this).show();
				}
			});
		});
		$(this).children("div.loader_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.loader_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.loader_pagination").parent("div.items").children("div.listing_page").hide();
			$(this).parent("div.loader_pagination").parent("div.items").children("div.listing_page").each(function(i){
				if(i==page_int-1){
					$(this).show();
				}
			});
		});
		$(this).children("div.loader_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.loader_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.loader_pagination").parent("div.items").children("div.listing_page").hide();
			$(this).parent("div.loader_pagination").parent("div.items").children("div.listing_page:last").show();
		});
	});
}); 