$(document).ready(function() { //when the document is ready...


	//save selectors as variables to increase performance
	var $window = $(window);
	var $bg_home = $('#home');
	var $bg_gallery = $('#gallery');
	var $bg_contact = $('#contact');
	var $bg_foot = $('#foot');
	var seagull0 = $("#home .seagull0");
	var seagull1 = $("#gallery .seagull1");
	var seagull2 = $("#gallery .seagull2");
	var point1 = $("#contact .point1");
	var point2 = $("#contact .point2");
	
	var windowHeight = $window.height(); //get the height of the window
	
	
	//apply the class "inview" to a section that is in the viewport
	$('#home, #gallery, #contact, #foot').bind('inview', function (event, visible) {
			if (visible == true) {
			$(this).addClass("inview");
			} else {
			$(this).removeClass("inview");
			}
		});
	
			
	//function that places the navigation in the center of the window
	function RepositionNav(){
		var windowHeight = $window.height(); //get the height of the window
		var navHeight = $('#nav').height() / 2;
		var windowCenter = (windowHeight / 2); 
		var newtop = windowCenter - navHeight;
		$('#nav').css({"top": newtop}); //set the new top position of the navigation list
	}
	
	function RepositionNav(){
		var windowHeight = $window.height(); //get the height of the window
		var navHeight = $('#nav2').height() / 2;
		var windowCenter = (windowHeight / 2); 
		var newtop = windowCenter - navHeight;
		$('#nav2').css({"top": newtop}); //set the new top position of the navigation list
	}
	
	function RepositionNav(){
		var windowHeight = $window.height(); //get the height of the window
		var navHeight = $('#nav3').height() / 2;
		var windowCenter = (windowHeight / 2); 
		var newtop = windowCenter - navHeight;
		$('#nav3').css({"top": newtop}); //set the new top position of the navigation list
	}
	
	function RepositionNav(){
		var windowHeight = $window.height(); //get the height of the window
		var navHeight = $('#nav4').height() / 2;
		var windowCenter = (windowHeight / 2); 
		var newtop = windowCenter - navHeight;
		$('#nav4').css({"top": newtop}); //set the new top position of the navigation list
	}
	
	//function that is called for every pixel the user scrolls. Determines the position of the background
	/*arguments: 
		x = horizontal position of background
		windowHeight = height of the viewport
		pos = position of the scrollbar
		adjuster = adjust the position of the background
		inertia = how fast the background moves in relation to scrolling
	*/
	function newPos(x, windowHeight, pos, adjuster, inertia){
		return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
	}
	
	//function to be called whenever the window is scrolled or resized
	function Move(){ 
		var pos = $window.scrollTop(); //position of the scrollbar

		//if the first section is in view...
		if($bg_home.hasClass("inview")){
			//call the newPos function and change the background position
			$bg_home.css({'backgroundPosition': newPos(50, windowHeight, pos, 400, 0.1)});
			//call the newPos function and change the secnond background position
			seagull0.css({'backgroundPosition': newPos(90, windowHeight, pos, 500, -0.2)}); 
		}
		
		//if the second section is in view...
		if($bg_gallery.hasClass("inview")){
			//call the newPos function and change the background position
			$bg_gallery.css({'backgroundPosition': newPos(50, windowHeight, pos, 2000, 0.1)});
			//call the newPos function and change the secnond background position
			seagull1.css({'backgroundPosition': newPos(98, windowHeight, pos, 1950, -0.3)});
			seagull2.css({'backgroundPosition': newPos(87, windowHeight, pos, 3050, 0.3)});
		}
		
		//if the third section is in view...
		if($bg_contact.hasClass("inview")){
			//call the newPos function and change the background position
			$bg_contact.css({'backgroundPosition': newPos(50, windowHeight, pos, 1550, 0.1)});
			//call the newPos function and change the secnond background position
			point1.css({'backgroundPosition': newPos(49, windowHeight, pos, 4180, 0.5)});
			point2.css({'backgroundPosition': newPos(49, windowHeight, pos, 4650, 0.3)});
		}
		
		//if the fourth section is in view...
		if($bg_foot.hasClass("inview")){
			//call the newPos function and change the background position for CSS3 multiple backgrounds
			$bg_foot.css({'backgroundPosition': newPos(50, windowHeight, pos, 100, 0.1) + ", " + newPos(50, windowHeight, pos, 0, 0.7) + ", " + newPos(50, windowHeight, pos, 0, 0.5) + ", " + newPos(55, windowHeight, pos, 1700, -0.5) + ", " + newPos(50, windowHeight, pos, 0, 0.1)});
		}
		
		$('#pixels').html(pos); //display the number of pixels scrolled at the bottom of the page
	}
		
	RepositionNav(); //Reposition the Navigation to center it in the window when the script loads
	
	$window.resize(function(){ //if the user resizes the window...
		Move(); //move the background images in relation to the movement of the scrollbar
		RepositionNav(); //reposition the navigation list so it remains vertically central
	});		
	
	$window.bind('scroll', function(){ //when the user is scrolling...
		Move(); //move the background images in relation to the movement of the scrollbar
	});
	
});
