
$(document).ready(function() {  
	var $window = $(window);
	var $firstBG = $('#intro');
	var $secondBG = $('#second');
	var $thirdBG = $('#third');
	var $fourthBG = $('#fourth');
	var $fifthBG = $('#fifth');
	var $sixthBG = $('#sixth');
	var trainers = $("#second .bg");
	var trainers2 = $("#third .bg2");
	var trainers3 = $("#fourth .bg4");
	var trainers4 = $("#fifth .bg5");
	var trainers5 = $("#sixth .bg6");
	
	var windowHeight = $window.height();  
	$('#intro, #second, #third, #fourth, #fifth, #sixth').bind('inview', function (event, visible) {
			if (visible == true) {
			$(this).addClass("inview");
			} else {
			$(this).removeClass("inview");
			}
		});
		
function RepositionNav(){
		var windowHeight = $window.height(1200);
		var navHeight = $('#nav').height() / 2;
		var windowCenter = (windowHeight / 2); 
		var newtop = windowCenter - navHeight;
		$('#nav').css({"top": newtop}); 
	}

	function newPos(x, windowHeight, pos, adjuster, inertia){
		return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
	}
	
	
	function Move(){ 
		var pos = $window.scrollTop(); 

	
		if($firstBG.hasClass("inview")){
		
			$firstBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1200, 0.1)}); 
		}
		
		
		if($secondBG.hasClass("inview")){
			$secondBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1500, 0.1)});
			trainers.css({'backgroundPosition': newPos(45, windowHeight, pos, 1900, 0.2)});
		}
		
	
		if($thirdBG.hasClass("inview")){
			$thirdBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1500, 0.1)});
			trainers2.css({'backgroundPosition': newPos(50, windowHeight, pos, 1900, 0.3)});
		}
		
		
		if($fourthBG.hasClass("inview")){
			$fourthBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1500, 0.1)});
			trainers3.css({'backgroundPosition': newPos(50, windowHeight, pos, 1900, 0.3)});
		}
		
		if($fifthBG.hasClass("inview")){
		$fifthBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1500, 0.1)});
		trainers4.css({'backgroundPosition': newPos(50, windowHeight, pos, 3010, 0.3)});
		}
		
		if($sixthBG.hasClass("inview")){
			$sixthBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1500, 0.1)});
			trainers5.css({'backgroundPosition': newPos(50, windowHeight, pos, 3010, 0.3)});
		}
		
		$('#pixels').html(pos); 
	}
		
	RepositionNav(); 
	
	$window.resize(function(){ 
		Move(); 
		RepositionNav(); 
	});		
	
	$window.bind('scroll', function(){ 
		Move(2); 
	});
	
});
