$(function() {
	if($.os.name == 'iphone') {
      $(function(){
		$('#room').css({ backgroundPosition:"center 580px"});
		$('#facilities').css({ backgroundPosition:"center 300px"});
		$('#serviced').css({ backgroundPosition:"center 800px"});
		$('#environment').css({ backgroundPosition:"center 480px"});
      });
	} else if($.os.name == 'ipod') {
      $(function(){
		$('#room').css({ backgroundPosition:"center 580px"});
		$('#facilities').css({ backgroundPosition:"center 300px"});
		$('#serviced').css({ backgroundPosition:"center 800px"});
		$('#environment').css({ backgroundPosition:"center 480px"});
      });
	} else if($.os.name == 'ipad') {
      $(function(){
		$('#room').css({ backgroundPosition:"center 580px"});
		$('#facilities').css({ backgroundPosition:"center 300px"});
		$('#serviced').css({ backgroundPosition:"center 800px"});
		$('#environment').css({ backgroundPosition:"center 480px"});
      });
	} else {
		$(document).ready(function() { //when the document is ready...
	//save selectors as variables to increase performance
	var $window = $(window);
	var $headerBG = $('#header');
	var $roomBG = $('#room');
	var $facilitiesBG = $('#facilities');
	var $servicedBG = $('#serviced');
    var $environmentBG = $('#environment');
	var $t_footerBG = $('#t_footer');
	
	var windowHeight = $window.height(); //get the height of the window
	
	
	//apply the class "inview" to a section that is in the viewport
	$('#header, #room, #facilities, #serviced, #environment, #t_footer').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
	}
	
	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($headerBG.hasClass("inview")){
			$headerBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 960, 0.3)}); 
		}
		
		if($roomBG.hasClass("inview")){
			$roomBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1160, 0.3)});
		}
		
		if($facilitiesBG.hasClass("inview")){
			$facilitiesBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1860, 0.3)});
		}
		
		if($servicedBG.hasClass("inview")){
			$servicedBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 2660, 0.3)});
		}
		

		if($environmentBG.hasClass("inview")){
			$environmentBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 3260, 0.3)});
		}

		if($t_footerBG.hasClass("inview")){
			$t_footerBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 4260, 0.3)});
		}
		
		$('#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
	});
	
});
	}
});
