/*
Author: Ian Lunn
Author URL: http://www.ianlunn.co.uk/
Mdified and edited by: Alessandro Dallafina

License: http://creativecommons.org/licenses/by-sa/3.0/ (Attribution Share Alike). Please attribute work to Ian Lunn simply by leaving these comments in the source code or if you'd prefer, place a link on your website to http://www.ianlunn.co.uk/.

Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
*/


$(document).ready(function() { //when the document is ready...


	//save selectors as variables to increase performance
	var $window = $(window);
	var $firstBG = $('#first');
	var firstBG_up = $("#first .bg");
	var $secondBG = $('#second');
	var secondBG_up = $("#second .bg");
	var $thirdBG = $('#third');
	var thirdBG_up = $("#third .bg");
	var $fourthBG = $('#fourth');
	var fourthBG_up = $("#fourth .bg");
	var $fifthBG = $('#fifth');
	var $sixthBG = $('#sixth');
	var sixthBG_up = $("#sixth .bg");


	
	var windowHeight = $window.height(); //get the height of the window
	
	
	//apply the class "inview" to a section that is in the viewport
	$('#first, #second, #third, #fourth, #fifth, #sixth').bind('inview', function (event, visible) {
			if (visible == true) {
			$(this).addClass("inview");
			} else {
			$(this).removeClass("inview");
			}
		});
	
	//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($firstBG.hasClass("inview")){
			//call the newPos function and change the background position
			$firstBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1000, 0.3)}); 
			//call the newPos function and change the secnond background position
			firstBG_up.css({'backgroundPosition': newPos(90, windowHeight, pos, 1270, 0.6)});
		}
		
		//if the second section is in view...
		if($secondBG.hasClass("inview")){
			//call the newPos function and change the background position
			$secondBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 2400, 0.4)});
			//call the newPos function and change the secnond background position
			secondBG_up.css({'backgroundPosition': newPos(20, windowHeight, pos, 2920, 0.6)});
		}
		
		//if the third section is in view...
		if($thirdBG.hasClass("inview")){
			//call the newPos function and change the background position
			$thirdBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 4000, 0.3)});
			//call the newPos function and change the secnond background position
			thirdBG_up.css({'backgroundPosition': newPos(99, windowHeight, pos, 4020, 1.7)});
		}
		
		//if the fourth section is in view...
		if($fourthBG.hasClass("inview")){
			//call the newPos function and change the background position
			$fourthBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 5350, 0.4)});
			//call the newPos function and change the secnond background position
			fourthBG_up.css({'backgroundPosition': newPos(50, windowHeight, pos, 5650, 1.2)});
		}
		
		//if the fifth section is in view...
		if($fifthBG.hasClass("inview")){
			//call the newPos function and change the background position
			$fifthBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 5400, 0.3)}); 
		}
		
		//if the sixth section is in view...
		if($sixthBG.hasClass("inview")){
			//call the newPos function and change the background position
			$sixthBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 7200, 0.3)});//6600
			//call the newPos function and change the background position
			sixthBG_up.css({'backgroundPosition': newPos(10, windowHeight, pos, 7600, 0.6)});
		}
		
	}
		
			
	$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
	});


});
