/******************************************************************************
* decdisDefault.js
*******************************************************************************
Javascript principal
*******************************************************************************
*                                                                             *
* Copyright 2011									                          *
*                                                                             *
******************************************************************************/
(function ($) {
	$(document).ready(function(){
		
		/* Lien cacher / décacher */
		$(".decdis_blockNone").each(function(){
			var idMap = $(this).children("img").attr("usemap");
			if(!idMap) return false;
			
			$("map"+idMap).children("area").removeAttr("onclick").click(function(e){
				e.preventDefault();
				var idInset = $(this).attr("href");
				if(!idInset) return false;
				idInset = idInset.split("#")[1];
				$(".decdis_displayBlock,.decdis_displayNone").hide();
				$("a[name="+idInset+"]").next().children(".decdis_displayNone").fadeIn("slow");
			});
		});
	
		/* Ouverture de nyroModal sur les liens de type decdis_modalLink */
		$(".decdis_modalLink").click(function(e){
			e.preventDefault();
			var target = $(this).attr("href");
			$.nyroModalManual({
				url: target,
				minHeight: 0
			});
		});
		
		/* Fenêtre title image */
		var imgTitleWindow = $("<div class='decdis_modalTitleWindow' />").css({
		"width":"200"
		}).hide();
		
		var newTitle;
		if($(".decdis_modalTitle").length > 0){
			$("body").append(imgTitleWindow);
		}
		$(".decdis_modalTitle").mousemove(function(e){
			imgTitleWindow.css({
				"top":e.pageY + 20,
				"left":e.pageX + 20
			});
		}).mouseenter(function(){
			newTitle = $(this).attr("title");
			$(this).attr("title","");
			imgTitleWindow.append(newTitle).show();
		}).mouseleave(function(){
			$(this).attr("title",newTitle);
			newTitle = "";
			imgTitleWindow.empty().hide();
		});
		
		/* Navigation principale */
		
		$(".decdis_mnRub1").hoverIntent({
			over : openMainNav,
			out : closeMainNav,
			timeout : 50
		});
		function openMainNav(){
			$(this).children("a:not(#decdis_mnrFirst_link)").addClass("decdis_mnrOn_link");
			$(this).children(".decdis_globalSubRub").fadeIn('slow');
		}
		function closeMainNav(){
			$(this).children("a:not(#decdis_mnrStickyOn_link)").removeClass("decdis_mnrOn_link");
			$(this).children(".decdis_globalSubRub").hide();
		}
		
		setTimeout(function(){
			$(".decdis_mnRub1_link:not(.decdis_mnrFirst_link)").each(function(i){
				var ah = $(this).height();
				var ch = $(this).children().height();
				var mh = Math.ceil((ah-ch) / 2);
				$(this).children().css({
					'padding-top': mh,
					'height': (mh-ah)
				});
			});
		},10);
		
		var nbRub = $(".decdis_mnRub1:not(#decdis_mnRub1_0)").length;
		var blockWidth = 606; // 606 = Largeur + border doire et gauche + padding droite et gauche
		$(".decdis_mnRub1:not(#decdis_mnRub1_0)").each(function(num){
			var element = $(this);
			element.children(".decdis_globalSubRub").css("left",function(){
				return (0 - ((blockWidth - element.width()) * (num / (nbRub-1))));
			});
		});
		
		/* Ouvrir dans un nouvelle page */
		$("._blank").attr("target","_blank");
		
		/* Newsletter */
		$("#decdis_emailInput").click(function(){
			if($(this).val() == "Saisissez votre e-mail") this.select();
		}).blur(function(){
			if($(this).val() == "") $(this).val("Saisissez votre e-mail");
		});
		
		$("#decdis_newsletterForm").submit(function(e){
			var url = $(this).attr("action");
			var cUser = $(this).children("#decdis_nlcUserInput").val();
			var cEmail = $(this).children("#decdis_emailInput").val();
			var reg = new RegExp('^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,6}$','i');
			
			if(reg.exec(cEmail) != null){
				$.post(url, {pageMode: "ajax", ajaxFunc: "addNlMembers", currentUser: cUser, currentEmail: cEmail}, function (data) {
					
					switch(data){
						case "1" :
							alertBox("L'inscription a bien été effectée.","iso_icons/decdisOkNl.gif",true,data);
						break;
						case "2" :
							alertBox("L'inscription a bien été effectée.","iso_icons/decdisOkNl.gif",true,data);
							$("#decdis_newsletterForm").slideUp("slow");
						break;
						default:
							alertBox("Une erreur lors de l'inscription, merci d'essayer de nouveau.","iso_icons/decdisOkNl.gif",true,data);
						break;						
					}
					
				});
			}else{
				alertBox("Format de l'email non conforme","iso_icons/decdisOkNl.gif",true);
			}
			return false;
		});
		
			/* alertBox */
			function alertBox(title,img,isClosedImg,data){
				var alertBoxTitle = $("<h6 id='decdis_alertBoxTitle'/>").text(title);
				var alertBoxImg = "";
				if(img){
					alertBoxImg = $("<img src='"+img+"' alt='ok' title='' id='decdis_alertBoxImg' />");
					if(isClosedImg){
						alertBoxImg = $(alertBoxImg).click(function(e){
							e.stopPropagation();
							removeAlertBox();
							if(parseInt(data) < 1) $("#decdis_emailInput").select();
						});
					}
				}
				var globalAlertBox = $("<div id='decdis_alertBox' /> ").append(alertBoxTitle).append(alertBoxImg);
				var alertBlackBox = $("<div id='decdis_loading' />").css("height",$(document).height());
				
				if($("#decdis_loading").length < 1){
					$("body").append(globalAlertBox).prepend(alertBlackBox);
					$(globalAlertBox).css({
						"top": function(index){
							return (($(window).height() - $(this).height()) / 2);
						},
						"left": function(index){
							return (($(window).width() - $(this).width()) / 2);
						}
					});
				}

				$(window).resize(function() {
					var win = $(this);
					$(globalAlertBox).css({
						"top": function(index){
							return (($(win).height() - $(this).height()) / 2);
						},
						"left": function(index){
							return (($(win).width() - $(this).width()) / 2);
						}
					});
				});
			}
			
			function removeAlertBox(){
				$("#decdis_loading,#decdis_alertBox").remove();
			}

		
	});
})(jQuery);
