/*
Vertical Parallax as General Function by Tim Miller
*/

// imgs:  1200px
// space: 1600px

$(document).ready(function() { //when the document is ready...
	var myBGS = [["home",3,0],["air",3,0],["water",3,0],["domestic",3,0],["warehouse",3,0],["local",3,0]];
//	var myBGS = [["home",3,0],["air",3,150],["water",3,150],["domestic",3,300],["warehouse",3,300],["local",3,300]];
/*	var myBGS = [["home",3,0],["air",3,400],["water",3,150],["domestic",3,300],["warehouse",3,500],["local",3,300]];*/

	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/);
	if (agentID) {
		// ios so do NOTHING
		$.each(myBGS, function(i, aLayer) { 
		
			
			$("#"+aLayer[0]+"0").css("background-attachment", "scroll");
			//$("#"+aLayer[0]+"0").css("background-position", "50% -"+ aLayer[2] + "px");
			$("#"+aLayer[0]+"1").css("background-attachment", "scroll");
			//$("#"+aLayer[0]+"1").css("background-position", "50% -"+ aLayer[2] + "px");
			$("#"+aLayer[0]+"2").css("background-attachment", "scroll");
			//$("#"+aLayer[0]+"2").css("background-position", "50% -"+ aLayer[2] + "px");
			
			
			//$("#"+aLayer[0]+"0").css("-webkit-background-size", "100%");
		});
		
	} else {
		parallaxInit(myBGS);
	}
});


var bgAr = [];
var bgLrs = [];
function parallaxInit($newBGS){

	//save selectors as variables to increase performance
	var $window = $(window);
	$bgAr = $newBGS;
	
	var windowHeight = $window.height(); //get the height of the window
	
	var tempI = 0;
	$.each($bgAr, function(i, aLayer) { 
		// build locked reference list for divs (faster lookup)
		bgLrs.push([]);
		bgLrs[tempI].push($("#"+aLayer[0]));
		for (b = 0; b < aLayer[1]; b++){
			bgLrs[tempI].push($("#"+aLayer[0]+b));
		}
		tempI++;
		//apply the class "inview" to a section that is in the viewport
		$("#"+aLayer[0]).bind('inview', function (event, visible) {
			if (visible == true) {
			$(this).addClass("inview");
			$(this).removeClass("outview");
			} else {
			$(this).addClass("outview");
			$(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 windowWidth = $window.width(); //get the width of the window
		var navHeight = $('#nav').height() / 2;
		var windowCenter = (windowHeight / 2); 
		var VwindowCenter = (windowWidth  / 2); 
		var newtop = windowCenter - navHeight;
		$('#nav').css({"top": newtop}); //set the new top position of the navigation list
		$('.aelogo').css({"left": VwindowCenter-478}); //set the new left of the logo
	}
	
	//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, myAdjuster){
		return x + "% " + (-((pos - adjuster)*inertia)-myAdjuster)  + "px";
		//return x + "% " + Math.round(-(pos - adjuster) * inertia)  + "px";
		//return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
	}
	
	//function to be called whenever the window is scrolled or resized
	function Move(){ 
	//return();
		var pos = $window.scrollTop(); //position of the scrollbar
		//document.title = pos;
		var tempHighest = 0;
		$.each($bgAr, function(i, aLayer) { 
			//if the section is in view...
			$("#nav li:eq("+i+")").removeClass("active");
			if(bgLrs[i][0].hasClass("inview")){
				if (tempHighest < i)
					tempHighest = i;
				var $offset = .6;
				var subLayerIndex = 1+aLayer[1];
				while (--subLayerIndex > 0){
					//call the newPos function and change the background position
					bgLrs[i][subLayerIndex].css({'backgroundPosition': newPos(50, windowHeight, pos, 950*i, $offset, aLayer[2])});
					$offset-=.25;
				}
			}
		});
		
		// override previous highest code, determine it by POS division instead.
		/*
		if (pos == 0)
			tempHighest = 0;
		*/
		tempHighest = Math.round(pos / 950);
			
			
		// now activate lowest visible
		$("#nav li:eq("+tempHighest+")").addClass("active");
		
		//$('#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...
		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
	});
	
	function setStatus(nuMsg){
		alert(nuMsg);
		//window.status=nuMsg;
	}
	//Move();
	RepositionNav();

}
