$(document).ready(function()
{
	
	QueryLoader.selectorPreload = "body";
	QueryLoader.init();
	
	$('#nav').localScroll();
	
	var $window = $(window);
	var windowHeight = $window.height();
	var $homeBG = $('#home');
	var $restaurantBG = $('#restaurant');
	var $diferenciaisBG = $('#diferenciais');
	var $clientesBG = $('#clientes');
	var $skyBG = $('#sky');
	var $equipeBG = $('#equipe');
	var $blogBG = $('#blog');
	var $contatBG = $('#contat');
	var $imgHome = $("#home .bg");
	var $imgcontat = $("#contat .bg");
	
	$('#home, #restaurant, #diferenciais, #clientes, #sky, #equipe, #blog, #contat').bind('inview', function (event, visible)
	{
		if (visible == true)
		{
			$(this).addClass("inview");
		}
		else
		{
			$(this).removeClass("inview");
		}
	});
	
	function newPos(x, windowHeight, pos, adjuster, inertia)
	{
		return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
	}

	function Move()
	{ 
		var pos = $window.scrollTop();

		//home
		if($homeBG.hasClass("inview"))
		{
			$homeBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 500, 0.1)});
			//$imgHome.css({'backgroundPosition': newPos(40, windowHeight - 260, pos, 900, 0.3)});
		}
		
		//restaurant
		if($restaurantBG.hasClass("inview"))
		{
			$restaurantBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1600, 0.3)});
		}
		
		//por que
		if($diferenciaisBG.hasClass("inview"))
		{
			$diferenciaisBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 2000, 0.3)});
		}
		
		//clientes
		if($clientesBG.hasClass("inview"))
		{
			$clientesBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 3500, 0.3)});
		}
		
		//sky
		if($skyBG.hasClass("inview"))
		{
			$skyBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 4500, 0.3)});
		}
		
		//equipe
		if($equipeBG.hasClass("inview"))
		{
			$equipeBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 7280, 0.3)});
		}
		
		//blog
		if($blogBG.hasClass("inview"))
		{
			$blogBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 8170, 0.3)});
		}
		
		//contat
		if($contatBG.hasClass("inview"))
		{
			$contatBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 5500, 0.3)});
			//$imgcontat.css({'backgroundPosition': newPos(100, windowHeight, pos, 8920, 0.3)});
		}
	}

	$window.resize(function()
	{
		Move();
	});		

	$window.bind('scroll', function()
	{
		Move();
	});
	//
	$('.equipe').roundabout({
		minOpacity: 0,
		minScale: 0.6,
		easing: 'easeOutExpo',
		duration: 500,
		reflect: false,
		maxZ: 50,
		minZ: 0
	});
	//
	$("a.fancy").fancybox({
		'overlayShow'	: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'fade',
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.4
	});
	
	//carousel
	$(".carousel").carousel({slideEasing: 'easeOutExpo', animSpeed : 600});
	
	//contat valida
	$('form.contat').submit(function()
	{
		$('form.contat .error').remove();
		var hasError = false;
		
		$('.requiredField').each(function()
		{
			if($.trim($(this).val()) == '')
			{
				var labelText = $(this).prev('label').text();
				
				$(this).parent().append('<p class="error">Ce champ '+labelText+' est obligatoire.</p>');
				$(this).addClass('inputError');
				hasError = true;
			}
			else if($(this).hasClass('email'))
			{
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				
				if(!emailReg.test($.trim($(this).val())))
				{
					var labelText = $(this).prev('label').text();
					
					$(this).parent().append('<p class="error">! adresse email invalide'+labelText+'.</p>');
					$(this).addClass('inputError');
					hasError = true;
				}
			}
		});
		if(!hasError)
		{
			var formInput = $(this).serialize();
			$.post($(this).attr('action'),formInput, function(data)
			{
				$('form.contat').slideUp("fast", function()
				{
					$(this).before('<p class="error"><strong>Merci!</strong> Votre message est enovoyé.</p>');
				});
			});
		}
		
		return false;	
	});

});

$(window).load(function()
{
	var total = $('.slider img').length;
	var rand = Math.floor(Math.random()*total);
	$('.slider').nivoSlider({
		effect:'boxRainGrow',
		animSpeed:700,
		pauseTime:4500,
		boxCols: 4,
		boxRow: 8,
		startSlide: rand,
		directionNav:true,
		directionNavHide:false,
		controlNav:false,
		controlNavThumbs:false,
		controlNavThumbsFromRel:false,
		controlNavThumbsSearch: '.jpg',
		controlNavThumbsReplace: '_thumb.jpg',
		keyboardNav:true,
		pauseOnHover:true,
		manualAdvance:false,
		captionOpacity:0.8
	});
	
	$('noscript').each(function() {
        var $this = $(this);
        if (typeof $this.data('delayed') === 'string') {
            $this.before($this.text()).remove();
        }
    });
	
	//placeholder
	
	$('.test').addPlaceholder();
	
	$('input[type="text"], textarea').focus(function()
	{
		if (this.value == this.defaultValue)
		{ 
			this.value = '';
		}
		if(this.value != this.defaultValue)
		{
			this.select();
		}
	});
	$('input[type="text"], textarea').blur(function()
	{
		if ($.trim(this.value) == '')
		{
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	}); 
	
});
