/*
JavaScript for the demo: Recreating the Nikebetterworld.com Parallax Demo
Demo: Recreating the Nikebetterworld.com Parallax Demo
Author: Ian Lunn
Author URL: http://www.ianlunn.co.uk/
Demo URL: http://www.ianlunn.co.uk/demos/recreate-nikebetterworld-parallax/
Tutorial URL: http://www.ianlunn.co.uk/blog/code-tutorials/recreate-nikebetterworld-parallax/

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 = $('#intro');
	var $secondBG = $('#second');
	var $thirdBG = $('#third');
	var $fourthBG = $('#fourth');
	var $fifthBG = $('#fifth');
	var $sixthBG = $('#sixth');
	var $seventhBG = $('#seventh');
	var $eighthBG = $('#eighth');
	var $ninethBG = $('#nineth');
	
	var pig = $("#Pig");
	var lavandino = $("#Lavandino");
	var pallina = $("#Pallina");
	var pencil = $("#Pencil");
	
	var $eighth_big = $('#eighth .bg');
	
	var img1 = $("#img1");
	var img2 = $("#img2");
	var img3 = $("#img3");
	
	var windowHeight = $window.height(); //get the height of the window
	
	
	//apply the class "inview" to a section that is in the viewport
	$('#intro, #second, #third, #fourth, #fifth, #sixth, #seventh, #eighth, #nineth').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
		$('#blog').css({"top": newtop});
	}
	
	//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 newPosTwo(x, windowHeight, pos, adjuster, inertia){
		return "0px " + (-((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")){
			$("#nav li").removeClass("Selezionato")
			$("#nav li:nth-child(1)").addClass("Selezionato")
			//call the newPos function and change the background position
			$firstBG.css({'backgroundPosition': newPos(50, windowHeight, pos, -300, 0.3)}); 
		}
		
		//if the second section is in view...
		if($secondBG.hasClass("inview")){
			if($firstBG.hasClass("inview")){
				$("#nav li").removeClass("Selezionato")
			$("#nav li:nth-child(1)").addClass("Selezionato")
			}else{
				$("#nav li").removeClass("Selezionato")
				$("#nav li:nth-child(2)").addClass("Selezionato")	
			}
			
			//call the newPos function and change the background position
			$secondBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1600, 0.4)});
			//call the newPos function and change the secnond background position
			//trainers.css({'backgroundPosition': newPos(50, windowHeight, pos, 1900, 0.6)});
		}
		
		//if the third section is in view...
		if($thirdBG.hasClass("inview")){
			$("#nav li").removeClass("Selezionato")
			$("#nav li:nth-child(3)").addClass("Selezionato")
			//call the newPos function and change the background position
			$thirdBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1500, 0.3)});
		}
		
		//if the fourth section is in view...
		if($fourthBG.hasClass("inview")){
			$("#nav li").removeClass("Selezionato")
			$("#nav li:nth-child(4)").addClass("Selezionato")
			//call the newPos function and change the background position for CSS3 multiple backgrounds
			//$fourthBG.css({'backgroundPosition': newPos(0, windowHeight, pos, 200, 0.9) + ", " + newPos(50, windowHeight, pos, 0, 0.7) + ", " + newPos(50, windowHeight, pos, 0, 0.5) + ", " + newPos(50, windowHeight, pos, 1700, 0.3)});
			$fourthBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 2450, 0.3)});
		}
		
		if($fifthBG.hasClass("inview")){
			$("#nav li").removeClass("Selezionato")
			$("#nav li:nth-child(5)").addClass("Selezionato")
			//call the newPos function and change the background position for CSS3 multiple backgrounds
			//$fourthBG.css({'backgroundPosition': newPos(0, windowHeight, pos, 200, 0.9) + ", " + newPos(50, windowHeight, pos, 0, 0.7) + ", " + newPos(50, windowHeight, pos, 0, 0.5) + ", " + newPos(50, windowHeight, pos, 1700, 0.3)});
			img1.css({'backgroundPosition': newPos(50, windowHeight, pos, 3000, 0.1)});
			img2.css({'backgroundPosition': newPos(50, windowHeight, pos, 4200, 0.2)});
			img3.css({'backgroundPosition': newPos(50, windowHeight, pos, 4100, 0.6)});
			
			$fifthBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 3550, 0.6)});
		}
		
		if($sixthBG.hasClass("inview")){
			$("#nav li").removeClass("Selezionato")
			$("#nav li:nth-child(6)").addClass("Selezionato")
			//call the newPos function and change the background position for CSS3 multiple backgrounds
			//$fourthBG.css({'backgroundPosition': newPos(0, windowHeight, pos, 200, 0.9) + ", " + newPos(50, windowHeight, pos, 0, 0.7) + ", " + newPos(50, windowHeight, pos, 0, 0.5) + ", " + newPos(50, windowHeight, pos, 1700, 0.3)});
			$sixthBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 4300, 0.4)});
		}
		
		if($seventhBG.hasClass("inview")){
			$("#nav li").removeClass("Selezionato")
			$("#nav li:nth-child(7)").addClass("Selezionato")
			//call the newPos function and change the background position for CSS3 multiple backgrounds
			//$fourthBG.css({'backgroundPosition': newPos(0, windowHeight, pos, 200, 0.9) + ", " + newPos(50, windowHeight, pos, 0, 0.7) + ", " + newPos(50, windowHeight, pos, 0, 0.5) + ", " + newPos(50, windowHeight, pos, 1700, 0.3)});
			$seventhBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 7100, 0.23)});
			$('#Up').css({'backgroundPosition': newPos(50, windowHeight, pos, 7100, 0.23)});
			
			
			pig.css({'backgroundPosition': newPos(50, windowHeight, pos, 6200, 0.0)});
			lavandino.css({'backgroundPosition': newPos(50, windowHeight, pos, 6800, 0.2)});
			pallina.css({'backgroundPosition': newPos(50, windowHeight, pos, 6200, 0.6)});
			pencil.css({'backgroundPosition': newPos(50, windowHeight, pos, 6300, 1.0)});
		}
		
		if($eighthBG.hasClass("inview")){
			$("#nav li").removeClass("Selezionato")
			$("#nav li:nth-child(8)").addClass("Selezionato")
			//call the newPos function and change the background position for CSS3 multiple backgrounds
			//$fourthBG.css({'backgroundPosition': newPos(0, windowHeight, pos, 200, 0.9) + ", " + newPos(50, windowHeight, pos, 0, 0.7) + ", " + newPos(50, windowHeight, pos, 0, 0.5) + ", " + newPos(50, windowHeight, pos, 1700, 0.3)});
			//\$eighthBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 5000, 0.1)});
			
			$eighth_big.css({'backgroundPosition': newPosTwo(50, windowHeight, pos, 4000, 0.8)});
		}
		
		if($ninethBG.hasClass("inview")){
			$("#nav li").removeClass("Selezionato")
			$("#nav li:nth-child(9)").addClass("Selezionato")
			//call the newPos function and change the background position for CSS3 multiple backgrounds
			//$fourthBG.css({'backgroundPosition': newPos(0, windowHeight, pos, 200, 0.9) + ", " + newPos(50, windowHeight, pos, 0, 0.7) + ", " + newPos(50, windowHeight, pos, 0, 0.5) + ", " + newPos(50, windowHeight, pos, 1700, 0.3)});
			//$ninethBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 4300, 0.4)});
		}
		
		//$('#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
	});
	
});
