$(document).ready(function(){

	
	function megaHoverOver(){
		
		var largLi=$(this).width();
		var larSub=$(this).find(".sub").width();
		
		if(largLi>0)
		{
			var Left=(-115)+(largLi/2);
			$(this).find(".sub").css("marginLeft",Left);
		}
		
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
		
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 200, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
	
	$("ul#topnav li").hover(function()
	{
		var largLi=$(this).width();
	});
	
	$('#mycarousel').jcarousel({
    	wrap: 'circular',
		auto: '5',
		animation:'50000'
    });
	
	$(".video").click(function()
	{
		$.fancybox({
			'padding'		: 10,
			'autoScale'		: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'title'			: this.title,
			'width'			: 480,
			'height'		: 390,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			   	 'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});
		return false;

	});
	
	$("#nouveautes").fancybox({'padding':'10','width':'610','height':'520','href':"pages/nouveautes.php",'transitionIn': 'elastic', 'transitionOut' : 'elastic'});	
	$("#coeur").fancybox({'padding':'10','width':'610','height':'520','href':"pages/coupdecoeur.php",'transitionIn': 'elastic', 'transitionOut' : 'elastic'});

		
	
	$("#contactArticle").click(function()
	{
		$.ajax({
			type: "POST",
			url: "pages/form_contact.php",
			data: "Action=contact&NumArticle="+$('#NumArticle').val(),
			success: function(msg){				
				$.fancybox(msg,{'padding':'10','width':'610','height':'520','transitionIn': 'elastic', 'transitionOut' : 'elastic'});
				
					$('#form_contact input').keypress(function(e) { 
		
						var vide=0;
						var rel=  /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

						$("#form_contact .obli td input, #form_contact .obli td textarea").each(function(i){
							if($("#"+this.id).val()=="" || !$("#mail").val().match(rel)) vide=vide+1;    
						});

						if(e.keyCode=='13') 
						{
							if(vide==0)
							{
								$("#form_contact").submit();
								return true;				
							}
							else
							{
								if($("#nom").val()=='') $("#labelnom").addClass('obligatoire'); else $("#labelnom").removeClass('obligatoire');
								if($("#mail").val()=='' || !$("#mail").val().match(rel)) $("#labelmail").addClass('obligatoire'); else $("#labelmail").removeClass('obligatoire');
								return false;
							} 
						}
					});
	
				$("#envoiInfos").click(function(){
					var vide=0;
					var rel=  /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

					$(".obli td input, .obli td textarea").each(function(i){
						if($("#"+this.id).val()=="" || !$("#mail").val().match(rel)) vide=vide+1;    
					});

					if(vide==0)
					{
						$("#form_contact").submit();
						return true;				
					}
					else
					{
						if($("#nom").val()=='') $("#labelnom").addClass('obligatoire'); else $("#labelnom").removeClass('obligatoire');
						if($("#mail").val()=='' || !$("#mail").val().match(rel)) $("#labelmail").addClass('obligatoire'); else $("#labelmail").removeClass('obligatoire');
						return false;
					}   
				});
			}				
		});
	
	});
	
	$('#form_contactmenu input').keypress(function(e) { 
		
		var vide=0;
		var rel=  /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

		$("#form_contactmenu .obli td input, #form_contactmenu .obli td textarea").each(function(i){
			if($("#"+this.id).val()=="" || !$("#mail").val().match(rel)) vide=vide+1;    
		});

		if(e.keyCode=='13') 
		{
			if(vide==0)
			{
				$("#form_contactmenu").submit();
				return true;				
			}
			else
			{
				if($("#nom").val()=='') $("#labelnom").addClass('obligatoire'); else $("#labelnom").removeClass('obligatoire');
				if($("#mail").val()=='' || !$("#mail").val().match(rel)) $("#labelmail").addClass('obligatoire'); else $("#labelmail").removeClass('obligatoire');
				if($("#message").val()=='') $("#labelmessage").addClass('obligatoire'); else $("#labelmessage").removeClass('obligatoire');
				return false;
			} 
		}
	});
		
	$("#envoiInfosFormulaire").click(function(){

		var vide=0;
		var rel=  /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

		$(".obli td input, .obli td textarea").each(function(i){
			if($("#"+this.id).val()=="" || !$("#mail").val().match(rel)) vide=vide+1;    
		});

		if(vide==0)
		{
			$("#form_contactmenu").submit();
			return true;			
		}
		else
		{
			if($("#nom").val()=='') $("#labelnom").addClass('obligatoire'); else $("#labelnom").removeClass('obligatoire');
			if($("#mail").val()=='' || !$("#mail").val().match(rel)) $("#labelmail").addClass('obligatoire'); else $("#labelmail").removeClass('obligatoire');
			if($("#message").val()=='') $("#labelmessage").addClass('obligatoire'); else $("#labelmessage").removeClass('obligatoire');
			return false;
		}   
	});
	
		
	setInterval( "slideSwitch('slide_nouveautes')", 3500 );
	setInterval( "slideSwitch('slide_coeur')", 3500 );
	//window.setTimeout(hovertipInit, 1);
	
	
	
	$(".changeLangue").click(function()
	{
		var Valeur=this.id;
		$.ajax({
			type: "POST",
			url: "traitements/change_langue.php",
			data: "lang="+Valeur,
			success: function(msg)
			{
				document.location.href=document.location.href;
			}		
		});
	});

	
});
	
	
function slideSwitch(slide) {
    var $active = $('#'+slide+' IMG.active');

	
    if ( $active.length == 0 ) $active = $('#'+slide+' IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#'+slide+' IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    var $sibs  = $active.siblings();
    var rndNum = Math.floor(Math.random() * $sibs.length );
    var $next  = $( $sibs[ rndNum ] );


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

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 2000, function() {
            $active.removeClass('active last-active');
        });
}

