	
	$(document).ready(function()
	{
		setInterval( "slideSwitch()", 5000 );
		
		INITgalleries();
		INITnewsletter();
		INITcalendars();
		INITtooltip();
		INITradiobtn();
		INITcontact();
	});
		
	
	function INITcontact(){
	  
	  //Newsletter validation
    var validatorContact = $("form#contactform").validate(
    { 
      rules: { email: {  required: true, email: true }, msg: { required: true}, nom: { required: true} }, 
      messages: { email: "S'il vous plaît entrer une adresse email valide.",
                  nom: "S'il vous plaît entrer un nom.",
                  msg: "S'il vous plaît entrer un message." } 
    }); 
    
    $("form#contactform").submit(function()
    {
      if( $("form#contactform").valid() )
      { 
        $("#ajax").val(1);
        $.ajax(
        {
          url: $(this).attr("action"),
          data: $(this).serialize(),
          type: "POST",
          success: function(data) 
          {
            if(data != '')
            {
              $("#newsletter input[name$='email']").val('email');
              validatorContact.resetForm();
              $("#form_res_r").html("<p>Merci de nous contacter.</p>");
            }
          }
        });
      }
      return false;
    });
	  
	}
	
	function INITgalleries()
	{
		//ColorBox gallery: link activate gallery
		$('a.openGallery').bind('click', function() {
		    $('a[rel=gallery]').eq(0).trigger('click');
		});
		
		//ColorBox
		$("a[rel='gallery']").colorbox(
			{
				opacity:0,
				maxWidth:780,
				innerWidth:"780px",
				scalePhotos:true,
				current:"{current}",
				onOpen: function()
				{
			        $('#cboxOverlay,#colorbox').css('visibility', 'hidden');

			        $('#cboxOverlay').css({
			            'visibility': 'visible',
			            'opacity': 0.98,
			            'cursor': 'pointer'
			        }).animate({
			            opacity: 'toggle'
			        }, 200 , function() {
			            $('#colorbox').css({
			            	'visibility': 'visible'
			        	}).fadeIn(300);
			        });
				},
				onComplete:function()
				{
					$("#cboxLoadedContent").append("<div id='imageTitle'></div>");
					$("#cboxLoadedContent").append("<div id='linkTitle'><a href='/participer' title='>Participer: MA RAISON d’aimer la Vie'>Participer: MA RAISON d’aimer la Vie</a></div>");

				},
				onClosed:function()
				{
					$("#imageTitle").remove();
					$("#linkTitle").remove();
				}
			});

		$("a[rel='img-gallery']").colorbox({
			opacity:0,
			maxWidth:780,
			innerWidth:"780px",
			scalePhotos:true,
			current:"{current}",
			onOpen: function()
			{
		        $('#cboxOverlay,#colorbox').css('visibility', 'hidden');
				
		        $('#cboxOverlay').css({
		            'visibility': 'visible',
		            'opacity': 0.98,
		            'cursor': 'pointer'
		        }).animate({
		            opacity: 'toggle'
		        }, 200 , function() {
		            $('#colorbox').css({
		            	'visibility': 'visible'
		        	}).fadeIn(300);
		        });
			},
			onComplete:function()
			{
				$("#cboxLoadedContent").append("<div id='thumbTitle'></div>");
				$('#wcd_r h1').clone().prependTo("#thumbTitle");
				$('#wcd_l img').clone().prependTo("#thumbTitle");
				
			},
			onClosed:function()
			{
				$("#thumbTitle").remove();
			}
		});
	}
	function INITnewsletter()
	{
		//Newsletter
		$('#newsletter').hide();
		$('#newsletter').css({opacity:0, width: '0'});
		$('.newsletter').click(function()
		{
			var ypos = $('.newsletter').offset().top + 30;
			$('#newsletter').css({'top':ypos+'px'});

			$('#newsletter').show();
			$('#newsletter').animate({width: '484px', opacity: 1}, 200);
			return false;
		});
		$('#closeNewslettter').click(function(){
			$('#newsletter').animate({width: '0', opacity: 0}, 200, function() {$(this).hide();});
			return false;
		});
		
		//Newsletter validation
		var validatorNewsletter = $("form#nlform").validate(
		{ 
			rules: { email: {  required: true, email: true } }, 
		 	messages: { email: "S'il vous plaît entrer une adresse email valide." } 
		});	
		
		$("form#nlform").submit(function()
		{
			if( $("form#nlform").valid() )
			{	
				$.ajax(
				{
					url: $(this).attr("action"),
					data: $(this).serialize(),
					type: "POST",
					success: function(data) 
					{
						$("#newsletter input[name$='email']").val(data);

						if(data != '')
						{
							$('#newsletter').delay(3000).animate({width: '0', opacity: 0}, 200, function() {
							$('#newsletter').hide();
							$("#newsletter input[name$='email']").val('email');
								validatorNewsletter.resetForm();
							});
						}
					}
				});
			}
			return false;
		});
	}
	function INITcalendars()
	{
		$("#ajaxcal a.calnext, #ajaxcal a.calprev").live("click", function(e){
			$.ajax({
			  url: $(this).attr("href"),
			  success: function(data) {
			    $('#ajaxcal').html(data);
				INITtooltip();
			  }
			});

		 	e.stopPropagation(); return false;	
		});	
	}
	function INITtooltip()
	{	
		xOffset = 5;
		yOffset = 10;		

		$(".calendar table a").hover(function(e){
									  
				this.t = this.title;
				
				if(this.t != "")
				{
					this.title = "";									  
					$("body").append("<p id='tooltip'>"+ this.t +"</p>");
					$("#tooltip")
					.css("top",(e.pageY - xOffset) + "px")
					.css("left",(e.pageX + yOffset) + "px")
					.css("display", "none")
					.fadeIn("fast");
				}		
	    },
		function()
		{
				if(this.t != "")
				{
					this.title = this.t;		
					$("#tooltip").remove();
				}
	    });	
	
		$(".calendar table a").mousemove(function(e){
			$("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		});			
	};
	function INITradiobtn()
	{
		$(".RadioClass").click(function()
		{
			if($(this).is(":checked")){
				$(".RadioSelected:not(:checked)").removeClass("RadioSelected");
				$(this).next("label").addClass("RadioSelected");
			}
		});
		
		//IE fix
		if($.browser.msie)
		{
			$("#newsletter label").click(function(event) {
			 	var id = $(this).attr('for');
				$('#'+id).attr('checked', 'checked');
				$('#'+id).click();
			});
		}
	}

	function slideSwitch() 
	{
	    var $active = $('#wlg_slideshow a.active');
	
	    if ( $active.length == 0 ) $active = $('#wlg_slideshow a:last');
	    var $next =  $active.next().length ? $active.next() : $('#wlg_slideshow a:first');

	    $active.addClass('last-active');

	    $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
	       $active.removeClass('active last-active');
	    });
	}
	function focusField(field, txt, color, bool)
	{
		field.style.color = color;
		if(bool){if (field.value == txt){field.value = "";}}	
		else{if (field.value == ""){field.value = txt;}}	
	}
	
