
/**
	Fonction permettant d'ajouter a la volee des evenements comme window.onload,
	sans rsique d'ecrasement des fonctions.
	obj : element sur lequel on met l'evenement. EX : window
	event : nom de l'evenement. Ex : 'load'
	fct : fonction appellee par l'evenement
	Exemple sur le lecteur flash :
	addEvent(window, 'load',my_function );
*/
function addEvent(obj, event, fct) {
    if (obj.attachEvent){ /*IE*/
        obj.attachEvent('on' + event, fct); /*Ne pas oublier le "on"*/
    }else{
        obj.addEventListener(event, fct, true);
    }
}



$(function(){ 
	//Paragraphes depliables
	//---------------------------------------------
	if($("div.question").length>0){
		$("div.question").each(function(){
			if($(this).attr("class")=='question closed'){
				$(this).next("div.reponse").hide();
			}
			$(this).css({"cursor":"pointer"});
			$(this).click(function(){
				$(this).toggleClass("opened").toggleClass("closed");
				$(this).next("div.reponse").toggle();
			});
		});
	}
	
	//Vider les champs .queryfield sur un click (formulaires de recherche)
	//---------------------------------------------
	if($(".queryfield").length>0){
		$(".queryfield").each(function(){
			$(this).click(function(){
				$(this).val("");
			});
		});
	}
	
	//Roll Over sur les boutons de formulaires
	//---------------------------------------------
	if($(".submitbutton").length>0){
		$(".submitbutton").each(function(){
			$(this).hover(
				function(){
					$(this).css({"background-color":""});
					$(this).css({"color":""});
					$(this).removeClass().addClass("formbutton submitbutton hover");
				},
				function(){
					$(this).css({"background-color":""});
					$(this).css({"color":""});
					$(this).removeClass().addClass("formbutton submitbutton");
				}				
			);
		});
	}
	if($(".resetbutton").length>0){
		$(".resetbutton").each(function(){
			$(this).hover(
				function(){
					$(this).css({"background-color":""});
					$(this).css({"color":""});
					$(this).removeClass().addClass("formbutton resetbutton hover");
				},
				function(){
					$(this).css({"background-color":""});
					$(this).css({"color":""});
					$(this).removeClass().addClass("formbutton resetbutton");
				}				
			);
		});
	}
	if($(".closebutton").length>0){
		$(".closebutton").each(function(){
			$(this).hover(
				function(){
					$(this).css({"background-color":""});
					$(this).css({"color":""});
					$(this).removeClass().addClass("formbutton closebutton hover");
				},
				function(){
					$(this).css({"background-color":""});
					$(this).css({"color":""});
					$(this).removeClass().addClass("formbutton closebutton");
				}				
			);
		});
	}
	if($(".navformbutton").length>0){
		$(".navformbutton").not(".selectedbutton").each(function(){
			$(this).hover(
				function(){
					$(this).css({"background-color":""});
					$(this).css({"color":""});
					$(this).removeClass().addClass("formbutton navformbutton hover");
				},
				function(){
					$(this).css({"background-color":""});
					$(this).css({"color":""});
					$(this).removeClass().addClass("formbutton navformbutton");
				}				
			);
		});
	}
	
	//Picture gallery
	//---------------------------------------------
	if($(".commons_basicPictureGallery").length>0){
		$(".commons_basicPictureGallery").each(function(){
			var composant = $(this);
			composant.children(".items").children(".pagination").show();
			composant.children(".items").children(".picturegallery").hide();
			composant.children(".items").children("#picturegallery_1").show();
			composant.children(".items").children(".pagination").children(".gotofirstpage").hide();
			composant.children(".items").children(".pagination").children(".gotopreviouspage").hide();
			var selected = composant.children(".items").children("#picturegallery_1");
			var nb = composant.children(".items").children(".picturegallery").length;
			composant.children(".items").children(".pagination").children(".gotonextpage").click(function(){
				selected.next(".picturegallery").show();
				selected.hide();
				selected = selected.next(".picturegallery");
				if(selected.next(".picturegallery").length>0){
					composant.children(".items").children(".pagination").children(".gotonextpage").show();
					composant.children(".items").children(".pagination").children(".gotolastpage").show();
				}else{
					composant.children(".items").children(".pagination").children(".gotonextpage").hide();
					composant.children(".items").children(".pagination").children(".gotolastpage").hide();
				}
				if(selected.prev(".picturegallery").length>0){
					composant.children(".items").children(".pagination").children(".gotofirstpage").show();
					composant.children(".items").children(".pagination").children(".gotopreviouspage").show();
				}else{
					composant.children(".items").children(".pagination").children(".gotofirstpage").hide();
					composant.children(".items").children(".pagination").children(".gotopreviouspage").hide();
				}
			});
			composant.children(".items").children(".pagination").children(".gotopreviouspage").click(function(){
				selected.prev(".picturegallery").show();
				selected.hide();
				selected = selected.prev(".picturegallery");
				if(selected.next(".picturegallery").length>0){
					composant.children(".items").children(".pagination").children(".gotonextpage").show();
					composant.children(".items").children(".pagination").children(".gotolastpage").show();
				}else{
					composant.children(".items").children(".pagination").children(".gotonextpage").hide();
					composant.children(".items").children(".pagination").children(".gotolastpage").hide();
				}
				if(selected.prev(".picturegallery").length>0){
					composant.children(".items").children(".pagination").children(".gotofirstpage").show();
					composant.children(".items").children(".pagination").children(".gotopreviouspage").show();
				}else{
					composant.children(".items").children(".pagination").children(".gotofirstpage").hide();
					composant.children(".items").children(".pagination").children(".gotopreviouspage").hide();
				}
			});
			composant.children(".items").children(".pagination").children(".gotolastpage").click(function(){
				composant.children(".items").children(".picturegallery:last").show();
				selected.hide();
				selected = composant.children(".items").children(".picturegallery:last");
				composant.children(".items").children(".pagination").children(".gotonextpage").hide();
				composant.children(".items").children(".pagination").children(".gotolastpage").hide();
				if(selected.prev(".picturegallery").length>0){
					composant.children(".items").children(".pagination").children(".gotofirstpage").show();
					composant.children(".items").children(".pagination").children(".gotopreviouspage").show();
				}else{
					composant.children(".items").children(".pagination").children(".gotofirstpage").hide();
					composant.children(".items").children(".pagination").children(".gotopreviouspage").hide();
				}
			});
			composant.children(".items").children(".pagination").children(".gotofirstpage").click(function(){
				composant.children(".items").children(".picturegallery:first").show();
				selected.hide();
				selected = composant.children(".items").children(".picturegallery:first");
				if(selected.next(".picturegallery").length>0){
					composant.children(".items").children(".pagination").children(".gotonextpage").show();
					composant.children(".items").children(".pagination").children(".gotolastpage").show();
				}else{
					composant.children(".items").children(".pagination").children(".gotonextpage").hide();
					composant.children(".items").children(".pagination").children(".gotolastpage").hide();
				}
				composant.children(".items").children(".pagination").children(".gotofirstpage").hide();
				composant.children(".items").children(".pagination").children(".gotopreviouspage").hide();
			});
		});
	}
	
	//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();
		});
	});
}); 
