$(document).ready(function(){
		$('textarea.tinymce').tinymce({
			// Location of TinyMCE script
			script_url : urlBlog + '/js/tiny_mce/tiny_mce.js',

			// General options
			//language : "fr",
			mode : "exact",
			elements : "content-text-ft",
			theme : "advanced",
			plugins : "paste",
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,cut,copy,paste,pasteword,strikethrough,separator,formatselect,separator,justifyleft,justifycenter,justifyright,separator,bullist,numlist,outdent,indent,separator,undo,redo,link,unlink",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			
			// Drop lists for link/image/media/template dialogs
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_blockformats : "p,h2,h3"
		});
		
		//Inscription masquer devenir membre
		$("#emailMember").change(function() { 
			if($(this).val() == '') {
				$("#member").fadeIn();
				$('label[for="member"]').fadeIn();
			} else {
				$("#member").fadeOut();
				$('label[for="member"]').fadeOut();
			}
		});			
		
		
		//Afficher la gallerie photo Cooliris
        var flashvars = {
            feed: "api://www.flickr.com/?user=56307678@N05"
        };
        var params = {
             allowFullScreen: "true",
             allowscriptaccess: "always",
			 style: "white"
        };
        swfobject.embedSWF("http://apps.cooliris.com/embed/cooliris.swf",
            "wall", "960", "600", "9.0.0", "",
            flashvars, params);
		
		// Afficher le hover sur le menu
		$("#menu img.over").hover(function() { 
			var hoverImg = HoverImgOf($(this).attr("src"));
			$(this).attr("src", hoverImg);
		}, function() {
			var normalImg = NormalImgOf($(this).attr("src"));
			$(this).attr("src", normalImg);
		});
		function HoverImgOf(filename)
		{
			var re = new RegExp("(.+)\\.(gif)", "g");
			return filename.replace(re, "$1_over.$2");
		}
		function NormalImgOf(filename)
		{
		   var re = new RegExp("(.+)_over\\.(gif)", "g");
		   return filename.replace(re, "$1.$2");
		}
		
		// Afficher les credits
		//0 means disabled; 1 means enabled;
		var popupStatus = 0;
		//loading popup with jQuery magic!
		function loadPopup(){
			//loads popup only if it is disabled
			if(popupStatus==0){
				$("#backgroundPopup").css({
				"opacity": "0.7"
				});
				$("#backgroundPopup").fadeIn("slow");
				$("#popupCredit").fadeIn("slow");
				popupStatus = 1;
			}
		}
		//disabling popup with jQuery magic!
		function disablePopup(){
			//disables popup only if it is enabled
			if(popupStatus==1){
				$("#backgroundPopup").fadeOut("slow");
				$("#popupCredit").fadeOut("slow");
				popupStatus = 0;
			}
		}
		//centering popup
		function centerPopup(){
			//request data for centering
			var windowWidth = document.documentElement.clientWidth;
			var windowHeight = document.documentElement.clientHeight;
			var popupHeight = $("#popupCredit").height();
			var popupWidth = $("#popupCredit").width();
			//centering
			$("#popupCredit").css({
				"position": "absolute",
				"bottom": windowHeight/2-popupHeight/2,
				"left": windowWidth/2-popupWidth/2
			});
			
			//only need force for IE6
			$("#backgroundPopup").css({
				"height": windowHeight
			});

		}
		
		//LOADING POPUP
		//Click the button event!
		$("#loadCredit").click(function(){
			//centering with css
			centerPopup();
			//load popup
			loadPopup();
		});

		//CLOSING POPUP
		//Click the x event!
		$("#popupCreditClose").click(function(){
			disablePopup();
		});
		//Click out event!
		$("#backgroundPopup").click(function(){
			disablePopup();
			});
			//Press Escape event!
			$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
			disablePopup();
			}
		});
		
		
		// Afficher le preview des formulaires
		//0 means disabled; 1 means enabled;
		var popupPreview = 0;
		//loading popup with jQuery magic!
		function loadPreview(){
			//loads popup only if it is disabled
			if(popupPreview==0){
				$("#formTexte").fadeOut("fast");
				$("#displayPreview").fadeIn("slow");
				popupPreview = 1;
			}
		}
		//disabling preview with jQuery magic!
		function disablePreview(){
			//disables popup only if it is enabled
			if(popupPreview==1){
				$("#displayPreview").fadeOut("fast");
				$("#formTexte").fadeIn("slow");
				popupPreview = 0;
			}
		}

		function completeField(){
			//request data for complete the field			
			
			$("#displayTitle").append($("#content-title-ft").val());
			$("#displayAuthor").append($("#whoami_name").val());
			
			$("#displayContent").append($('#content-text-ft').html());
			
			$("#displayEmail").append($("#whoami_email").val());
			$("#displayRegion").append($("#select_region").val());
		}
		
		function clearField(){
			//clear ther field			
			$("#displayTitle").empty();
			$("#displayAuthor").empty();
			$("#displayContent").empty();
			$("#displayCategories").empty();
			$("#displayEmail").empty();
			$("#displayRegion").empty();
		}
		
		//LOADING Preview
		//Click the button event!
		$("#openPreview").click(function(){
			//complete the field
			completeField();
			//load preview
			loadPreview();
		});

		//CLOSING preview
		//Click the x event!
		$("#closePreview").click(function(){
			clearField();
			disablePreview();
		});
		
		$("#sendForm").click(function(){
			$('#formTexte').submit();
		});
		
		
		//Formulaire Membership
		$('#soutienGEDI .link').hide();
		
		if($('#age').val() != 'Plus de 35 ans') {
			$('#2035').addClass('ok');
			$('#2035').animate({opacity:1}, 400);
		} else {
			$('#2035').animate({opacity:0.5}, 400);
		}
		
		$('#formTexte .soutienBox').click(function() {
						
			var x = $(this).attr('id');
			
			if($('#age').val() == 'Plus de 35 ans' && x == '2035') {
				return false;
			}
			
			$('#soutienGEDI .soutienBox').removeClass('select');
			$(this).addClass('select');
			$('#member').val(x);
			showWallOfFame();
		});
		
		$('.post .soutienBox').click(function() {
			var x = $(this).attr('id');
			$(location).attr('href', urlGedi + "/idees/devenez-membre/formulaire-dinscription/?membre=" + x);
		});

		$('#age').change(function() {
			if($(this).val() == 'Plus de 35 ans') {
				//$('#2035').fadeOut();
				$('#2035').animate({opacity:0.5}, 400);
				$('#2035').removeClass('ok');
				if($('#member').val() == '2035') {
					$(this).val('');
					$('#2035').removeClass('select');
				}
			} else {
				if($('#2035').css('opacity') == 0.5) {
					$('#2035').animate({opacity:1}, 400);
					$('#2035').addClass('ok');
					if($('#member').val() == 'observateur') {
						$(this).val('');
						$('#observateur').removeClass('select');
					}
				}
			}
			
			showWallOfFame();
			
		});
		
		
		
		function showWallOfFame() {
			if($('#member').val() == 'philanthrope') {
				$("label[for='walloffame']").fadeIn();
				$('#walloffame').fadeIn();
			} else {
				$("label[for='walloffame']").fadeOut();
				$('#walloffame').fadeOut();
			}
		}
		
		//Ajout d'un email à la liste
		$("#InscrireAjout").click(function() {
			$.ajax({
				type: "POST",
				url: urlBlog + '/form/mcapi_listSubscribe.php',
				data: "email=" + $('#InscrireEmail').val(),
				dataType: 'json',
				success:  function(data) {
					$('#InscrireEmail').fadeOut();
					$('#InscrireAjout').fadeOut();
					$('#InscrireMess').fadeIn().html(data.mess);
				}
			});
		});
		
	});
