/*
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
*/

jQuery(document).ready(function($) { //when the document is ready...

	function changeImg(imgNumber) {
		var slash = window.location;
		var myImages = [
			slash+"/wp-content/themes/claremont/img/frontpage/slide_1/bild_1.jpg", 
			slash+"/wp-content/themes/claremont/img/frontpage/slide_1/bild_2.jpg", 
			slash+"/wp-content/themes/claremont/img/frontpage/slide_1/bild_3.jpg"
		];
		
		var imgShown = myImages[Math.floor(Math.random() * myImages.length)];
		if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod'){
			jQuery('#intro').css('background','url('+ imgShown +')');
			jQuery('#intro').css('background-repeat','no-repeat');
			jQuery('#intro').css('background-attachment','scroll');
			jQuery('#intro').css('background-position','0 0');
		}else{
			jQuery('#intro').css('background','url('+ imgShown +')');
			jQuery('#intro').css('background-repeat','no-repeat');
			jQuery('#intro').css('background-attachment','fixed');
			jQuery('#intro').css('background-position','50% 0');
		}

	}
	changeImg();
	
	var $window = $(window);
	var $firstBG = $('#intro');
	var $secondBG = $('#second');

	var $thirdBG = $('#third');
	var $thirdText = $('#third .text');

	
	var $fourthBG = $('#fourth');
	var $fourthText = $('#fourth .text');

	var $fifthBG = $('#fifth');
	var $sixthBG = $('#sixth');
	var $seventhBG = $('#seventh');
	
	var $second_2 = $("#second .bg");
	var $third_2 = $("#third .bg");
	var $fourth_2 = $("#fourth .bg");
	var $sixth_2 = $("#sixth .bg");

	
	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').bind('inview', function (event, visible) {
		if (visible == true) {
			$(this).addClass("inview");
			} else {
			$(this).removeClass("inview");
		}
	});
	
	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 newPos(x, windowHeight, pos, adjuster, inertia){
		return x + (-((1000 + pos) - adjuster) * inertia)  + "px";
	}
	
	function textPos(x, windowHeight, pos, adjuster, inertia){
		return 10 + (-((1000 + pos) - adjuster) * inertia)  + "px";
	}
	
	/*
	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 windowHeight = $window.height();
		var pos = $window.scrollTop(); //position of the scrollbar
		var w_height = 1000;
		var b_height = -300;

		if($firstBG.hasClass("inview")){
			$firstBG.css({'backgroundPosition': newPos(50 +"% ", w_height, pos, w_height, 0.1)}); 
			$('#nav li').removeClass('active');
			$('#nav li.intro').addClass('active');
		}		
		
		if($secondBG.hasClass("inview")){
			$secondBG.css({'backgroundPosition': newPos(50+"% ", w_height, pos, w_height*2, 0.4)});
			$second_2.css({'backgroundPosition': newPos(b_height, w_height, pos, w_height*4, 0.3)});
			$('#nav li').removeClass('active');
			$('#nav li.second').addClass('active');

		}			

		if($thirdBG.hasClass("inview")){
			$thirdBG.css({'backgroundPosition': newPos(50+"% ", w_height, pos, w_height*3, 0.1)});
			$third_2.css({'backgroundPosition': newPos(50+"% ", w_height, pos, w_height*3.3, 0.1)});
			$thirdText.css({'top': textPos(1, w_height, pos, w_height*2.99, 1.5)});
			$('#nav li').removeClass('active');
			$('#nav li.third').addClass('active');

		}
		
		if($fourthBG.hasClass("inview")){
			$fourthBG.css({'backgroundPosition': newPos(50+"% ", w_height, pos, w_height*4, 0.4)});
			$fourth_2.css({'backgroundPosition': newPos(50+"px ", w_height, pos, w_height*4, 0.22)});
			$fourthText.css({'top': textPos(1, w_height, pos, w_height*4, 1.5)});

			$('#nav li').removeClass('active');
			$('#nav li.fourth').addClass('active');

		}

		if($fifthBG.hasClass("inview")){
			$fifthBG.css({'backgroundPosition': newPos(50+"% ", w_height, pos, w_height*5, 0.4)});
			$('#nav li').removeClass('active');
			$('#nav li.fifth').addClass('active');

		}

		if($sixthBG.hasClass("inview")){
			$sixthBG.css({'backgroundPosition': newPos(50+"% ", w_height, pos, w_height*6, 0.1)});
			$sixth_2.css({'backgroundPosition': newPos(50+"% ", w_height, pos, w_height*6, 0.28)});
			$('#nav li').removeClass('active');
			$('#nav li.sixth').addClass('active');
		}
		
		if($seventhBG.hasClass("inview")){
			$seventhBG.css({'backgroundPosition': newPos(50+"% ", w_height, pos, w_height*5, 0.1)});
			$('#nav li').removeClass('active');
			$('#nav li.seventh').addClass('active');

		}
	}
		
	RepositionNav(); //Reposition the Navigation to center it in the window when the script loads

	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod'){
		$('.bg').css('height',1000);    
	}else{
		$window.resize(function(){ //if the user resizes the window...
			maxWidth()
			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
		});
	
 	   	$('#intro, #second, #third, #fourth, #fifth, #sixth, #seventh, .bg').css('height',1000);    
    	$('#seventh').css('height',830);    
		maxWidth();
	}
    function maxWidth(){
	    var winWidth = $('.section').width();
	    if(winWidth > 2000){
	    
		   	$('#intro, #second, #third, #fourth, #fifth, #sixth, #seventh').css('background-size','cover');    
		   	$('#intro, #second, #third, #fourth, #fifth, #sixth, #seventh').css('-webkit-background-size','cover');    
		   	$('#intro, #second, #third, #fourth, #fifth, #sixth, #seventh').css('-moz-background-size','cover');    
		   	$('#intro, #second, #third, #fourth, #fifth, #sixth, #seventh').css('-o-background-size','cover');
	    }
    }

});
