(function($){

// comportement des boutons du menu principal
$.fn.menuFlash = function(elm,excluded,magic){
	var stop;
	$(this).find(elm).each(function(){
		var stop = false;		
		if(excluded!=null)
			for(var i = 0;i<excluded.length;i++){
				if($(this).attr("id")==excluded[i]){
					stop = true;
					$(this).next().find("span").menuFlash(elm);
				}
			}
		if(!stop){
			$(this).click(function(){
				if(magic!=null)
					for(var i = 0;i<magic.length;i++){
						if($(this).attr("menu")==magic[i] && $("#content").attr("php")==$(this).attr("menu"))
							return;
					}
				$("#loader").show();
				var redirect = $(this).attr("menu");
				$.page(redirect,'');
			});
		}
	});
};


// fonction qui applique des bords a l'element choisi 
$.fn.borders = function(color){
	var map = "";
	if(color!=null)
		map = "-"+color;
	$(this).append('<span class="border-bottom"></span><span class="border-right"></span><span class="border-top-right'+map+'"></span><span class="border-bottom-right'+map+'"></span><span class="border-bottom-left'+map+'"></span>');
};

// comportement des boutons en dessous de la page
$.fn.button = function(file_hover){
	var file = $(this).attr("src");
	$(this).mouseleave(function(){
		$(this).attr("src",file).css("cursor","default");
	}).click(function(){
		var menu = $(this).attr("menu");
		if(menu!=null)
			$.page(menu,'');		
	});
};

// comportement des boutons onglets
$.fn.menuOnglets = function(){
	$(this).find("span").mouseenter(function(){
		$(this).find("img").css("background","none");
	});
	$(this).find("span").click(function(){	
		var redirect = $(this).attr("menu");
		$.popup(redirect,'');
	});
};

// comportement des boutons du menu ayant des sous-menus
$.fn.extraMenu = function(){
	$(this).next().hide();
	$(this).attr("visible","false");
	$(this).click(function(){
		var visible = $(this).attr("visible");
		if(visible=="true"){
			$(this).attr("visible","false");
			$(this).next().slideUp();
		}else{
			$(this).attr("visible","true");
			$(this).next().slideDown();
		}
	});
};

// affichage dans la page
$.page = function(redirect,args){
	$.counter(redirect);
	$("#page #content").hide();
	$.post("php/"+redirect,{page:redirect,args:args},function success(data){
				$("#page #content").html(data).attr("php",redirect);
				$("#page #content").fadeIn(1000);
				$("#loader").fadeOut("slow");
	});
};

// affichage en popup
$.popup = function(redirect,args){
	$("#loader").show();
	$("#popup").remove();
	$.post("php/"+redirect,{page:redirect,args:args},function success(data){
		$("body").append("<div id='popup' style='color:white;display:none;padding:5px;width:60%;height:60%;background:#595959;position:absolute;overflow:hidden;left:20%;top:25%;z-index:10;'>"+
	"<img src='img/boutons/close.png' id='popupClose' style='position:absolute;right:10px;top:10px;width:32px;height:32px'/>"+data+"</div>");

		$("#popupClose").click(function(){
			$("#popup").find("*").hide();$(this).parent().slideUp(1000,function(){$(this).remove();});});		
		$("#popup").find("*").hide();
		$("#popup").css("border","2px solid #CCCCCC").addClass("ui-corner-top").addClass("ui-corner-bottom").css("-moz-opacity","0.95").fadeIn(1000,function(){$("#popup").find("*").show();});
		$("#loader").hide();
	});
};

// compteur de visite
$.counter = function(redirect){
	$.post("php/counter.php",{page:redirect,args:''},function success(data){
		var response = eval('('+data+')');
		if(response.result=="ok"){
			$("#pied p:last").html("Page vue <b>"+response.counters.perDay+"</b> fois.");
		}
	});
};

// pagination des images
$.fn.imagePager = function(){
		var self = $(this);
		var imgs = self.find(".imgs");
		// initialisation
		var index = 1;
		position = 1;
		imgs.addClass("ui-corner-bottom").find("img").each(function(){
			$(this).attr("numero",index).addClass("ui-corner-bottom");
			index++;
		});
		imgs.find("img").not(":first").hide();
		var max = index;
		var left = $("<img src='img/boutons/left.png' style='width:16px;height:16px'></img>").css("float","left").css("cursor","pointer").click(function(){
			if(position > 1){
				imgs.find("img:visible").hide();
				position--;
				imgs.find("img[numero="+position+"]").show();
			}	
		});
		var right = $("<img src='img/boutons/right.png' style='width:16px;height:16px'></img>").css("float","right").css("cursor","pointer").click(function(){
			if(position < max-1){
				imgs.find("img:visible").hide();
				position++;
				imgs.find("img[numero="+position+"]").show();
			}
		});	
		var controls = $("<span id='controls'></span>");	
		controls.append(left);
		controls.append(right);
		controls.css("padding-top","5px").find("a").css("font-weight","bold").css("font-size","9px").css("color","black");
		imgs.after(controls);
		self.oneTime("2s", function() {
			$.switche(imgs,position,"right",max,2500);
		});
	};
$.switche = function(imgs,position,sens,max,timer){
		//if(imgs==null)
		//	imgs = $(".imagePager").find(".imgs");
		switch(sens){
		case "left":
			if(position > 1){
				imgs.find("img:visible").hide();
				position--;
				imgs.find("img[numero="+position+"]").show();
			}else
				sens = "right";
			break;
		case "right":
			if(position < max-1){
				imgs.find("img:visible").hide();
				position++;
				imgs.find("img[numero="+position+"]").show();
			}else
				sens = "left";
			break;
		default:break;
		}
		setTimeout(function(){$.switche(imgs,position,sens,max,timer);},timer);
	};
})(jQuery);
