$(document).ready(function()
{
	
	//QueryLoader.selectorPreload = "body";
	//QueryLoader.init();
	
	$('#nav').localScroll();
	
	var $window = $(window);
	var windowHeight = $window.height();
	var $homeBG = $('#home');
	var $companyBG = $('#company');
	var $diferenciaisBG = $('#services');
	
	var $portfolioBG = $('#portfolio');
	var $equipeBG = $('#product');
	var $newsBG = $('#news');
	var $contactBG = $('#contact');
	var $imgHome = $("#home .bg");
	var $imgContato = $("#contact .bg");
	
	$('#home, #company, #services, #portfolio, #product, #news, #contact').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, 900, 0.6)});
			//$imgHome.css({'backgroundPosition': newPos(40, windowHeight - 260, pos, 900, 0.3)});
		}
		
		//company
		if($companyBG.hasClass("inview"))
		{
			$companyBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 2000, 0.6)});
		}
		
		//portfolio
		if($portfolioBG.hasClass("inview"))
		{
			$portfolioBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 5000, 0.3)});
		}
		
		//product
		if($equipeBG.hasClass("inview"))
		{
			$equipeBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 7280, 0.3)});
		}
		
		//news
		if($newsBG.hasClass("inview"))
		{
			$newsBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 8170, 0.3)});
		}
		
		//contact
		if($contactBG.hasClass("inview"))
		{
			$contactBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 9180, 0.3)});
			//$imgContato.css({'backgroundPosition': newPos(100, windowHeight, pos, 8920, 0.3)});
		}
	}

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

	$window.bind('scroll', function()
	{
		Move();
	});
	//
	$('.product').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});
	
	//contact valida
	/*
	$('form.contact').submit(function()
	{
		$('form.contact .error').remove();
		var hasError = false;
		
		$('.requiredField').each(function()
		{
			if($.trim($(this).val()) == '')
			{
				var labelText = $(this).prev('label').text();
				
				$(this).parent().append('<p class="error">O campo '+labelText+' é obrigatório.</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">! You\'ve entered an invalid '+labelText+'.</p>');
					$(this).addClass('inputError');
					hasError = true;
				}
			}
		});
		if(!hasError)
		{
			var formInput = $(this).serialize();
			$.post($(this).attr('action'),formInput, function(data)
			{
				$('form.contact').slideUp("fast", function()
				{
					$(this).before('<p class="error"><strong>Obrigado!</strong> Sua mensagem foi enviada.</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 : '');
		}
	}); 
*/	
});


