/*
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 = $("#dot_home");
	var $secondBG = $("#dot_musique");
	var $thirdBG = $("#dot_culture");
	var $fourthBG = $("#dot_art");
	var $fifthBG = $("#dot_mode");
	var $sixthBG = $("#dot_events");
	var $seventhBG = $("#dot_press");
	var $eightBG = $("#dot_shop");
	var $nineBG = $("#dot_playlist");
	var trainers = $(".parallax");
	
	var windowHeight = $window.height(); //get the height of the window
	
	
	//apply the class "inview" to a section that is in the viewport
	$('#dot_home,#dot_musique,#dot_art, #dot_culture, #dot_mode, #dot_playlist,#dot_events,#dot_press, #dot_shop').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 = $("#dotnav").height()/2;
		var windowCenter = (windowHeight/2); 
		var newtop = windowCenter - navHeight;
		$("#dotnav").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($firstBG.hasClass("inview")){
			//call the newPos function and change the background position
			$firstBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 0, 0.3)});
			$(".01").addClass("dot_active"); 
		}
		
		else {
			$(".01").removeClass("dot_active");
		}
		
		//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, 1250, 0.3)});
			$(".02").addClass("dot_active");
			$(".01").removeClass("dot_active");
		}
		
		else {
			$(".02").removeClass("dot_active");
		}
		
		//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, 2850, 0.3)});
			$(".03").addClass("dot_active");
			$(".02").removeClass("dot_active");
		}
		
		else {
			$(".03").removeClass("dot_active");
		}
		
		//4th
		if($fourthBG.hasClass("inview")){
			$(".04").addClass("dot_active");
			$(".03").removeClass("dot_active");
		}
		
		else {
			$(".04").removeClass("dot_active");
		}
		
		//5th
		if($fifthBG.hasClass("inview")){
			$(".05").addClass("dot_active");
			$(".04").removeClass("dot_active");
		}
		
		else {
			$(".05").removeClass("dot_active");
		}
		
		//6th
		if($sixthBG.hasClass("inview")){
			$(".06").addClass("dot_active");
			$(".05").removeClass("dot_active");
		}
		
		else {
			$(".06").removeClass("dot_active");
		}
		
		//7th
		if($seventhBG.hasClass("inview")){
			$(".07").addClass("dot_active");
			$(".06").removeClass("dot_active");
		}
		
		else {
			$(".07").removeClass("dot_active");
		}
		
		//8th
		if($eightBG.hasClass("inview")){
			$(".08").addClass("dot_active2");
			$(".07").removeClass("dot_active");
		}
		
		else {
			$(".08").removeClass("dot_active2");
		}
		
		//9th
		if($nineBG.hasClass("inview")){
			$(".09").addClass("dot_active2");
			$(".08").removeClass("dot_active2");
		}
		
		else {
			$(".09").removeClass("dot_active2");
		}
				
				
		$('#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
	});
	
});
