/*
Author: Ian Lunn
Author URL: http://www.ianlunn.co.uk/
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() {
	var i=0;
	/*$("#nav li a").click(function(){
		$("#nav li a").removeClass("active");
		$(this).addClass("active");
	});*/
	
	
	var $window = $(window);
	var $firstBG = $('#intro');
	var $secondBG = $('#second');
	var $thirdBG = $('#third');
	var trainers = $("#third .bg");
	var trainers_intro = $("#intro .bg");
	var windowHeight = $window.height();
	$('#intro, #second, #third').bind('inview', function (event, visible) {
		if (visible == true) {
			$(this).addClass("inview");
		} else {
			$(this).removeClass("inview");
		}
	});
	function RepositionNav(){
		var windowHeight = $window.height();
		var navHeight = $('#nav').height() / 2;
		var windowCenter = (windowHeight / 2); 
		var newtop = windowCenter - navHeight;
		$('#nav').css({"top": newtop});
	}
	function newPos(x, windowHeight, pos, adjuster, inertia){
		return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
	}
	function Move(){ 
		var pos = $window.scrollTop();
		if($firstBG.hasClass("inview")){
			if(i==0){
				$firstBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1100, 0.3)}); /*900*/
				i=1;
			}else{
				$firstBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 860, 0.3)});
			}
		}
		if($secondBG.hasClass("inview")){
			$secondBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1250, 0.3)});
		}
		if($thirdBG.hasClass("inview")){
			$thirdBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 2850, 0.3)});
			trainers.css({'backgroundPosition': newPos(70, windowHeight, pos, 3200, 0.6)});
		}
	}
	RepositionNav();
	$window.resize(function(){
		Move();
		RepositionNav();
	});		
	$window.bind('scroll', function(){
		Move();
	});
});
