$(document).ready(function() {
	
	// Cache the Window object
	$window = $(window);
	
	// Cache the Y offset and the speed of each sprite
	$('[data-type]').each(function() {
		$(this).data('offsetY', parseInt($(this).attr('data-offsetY')));
		$(this).data('Xposition', $(this).attr('data-Xposition'));
		$(this).data('speed', $(this).attr('data-speed'));
	});
	
	// For each element that has a data-type attribute of background
	$('[data-type="background"]').each(function() {
		
		// Store some variables based on where we are
		var $self = $(this),
			offsetCoords = $self.offset(),
			topOffset = offsetCoords.top;
		
		// When the window is scrolled...
	    $(window).scroll(function() {
			
			// If this section is in view
			if ( ($window.scrollTop() + $window.height()) > (topOffset) &&
				 ( (topOffset + $self.height()) > $window.scrollTop() ) ) {
				
				// Scroll the background at var speed
				// the yPos is a negative value because we're scrolling it UP!								
				var yPos = -($window.scrollTop() / $self.data('speed'));
				
				// If this element has a Y offset then add it on
				if ($self.data('offsetY')) {
					yPos += $self.data('offsetY');
				}
				
				// Put together our final background position
				var coords = '50% ' + yPos + 'px';

				// Move the background
				$self.css({ backgroundPosition: coords });
				
				// Check for other sprites in this section	
				$('[data-type="sprite"]', $self).each(function() {
					
					// Cache the sprite
					var $sprite = $(this);
					
					// Use the same calculation to work out how far to scroll the sprite
					var yPos = -($window.scrollTop() / $sprite.data('speed'));					
					var coords = $sprite.data('Xposition') + ' ' + (yPos + $sprite.data('offsetY')) + 'px';
					
					$sprite.css({ backgroundPosition: coords });													
					
				}); // sprites	
			
			}; // in view
	
		}); // window scroll
			
	});	// each data-type
	
	$(window).scroll(function() {
		
		var $footer = $('#footer'),
		    $copyright = $('#footer .copyright');
		
		// If the header is off the screen
		if ( $window.scrollTop() >= 200 ) {
			
			if ( $footer.hasClass('offscreen') )
				$footer.removeClass('offscreen').animate({ bottom: 0 }, 200);
		
		} else {
			
			if ( !$footer.hasClass('offscreen') )
				$footer.addClass('offscreen').animate({ bottom: '-50px' }, 200);
			
		}; // header hidden
		
		// If we are on the last slide
		if ( $window.scrollTop() >= 6000 ) {
			
			if ( $copyright.hasClass('offscreen') )
				$copyright.removeClass('offscreen').animate({ marginTop: 0 }, 200);
		
		} else {
			
			if ( !$copyright.hasClass('offscreen') )
				$copyright.addClass('offscreen').animate({ marginTop: '46px' }, 200);
			
		}; // on last slide

	}); // window scroll
	
	// Set the height of the last slide to the same height as the window
	$('#slide-6').height($window.height());
	$(window).resize(function() {
		
		$('#slide-6').height($window.height());
		
	}); // window resize
	
	// Setup the navigation menu
	$('.secondary-nav ul').onePageNav({
		currentClass: 'current',
		changeHash: false,
		scrollSpeed: 750,
		scrollOffset: -100
	});
	
	$('.secondary-nav a').tooltip({
		position: 'center left',
		effect: 'slide',
		direction: 'left',
		slideFade: true,
		delay: 0
	});

}); // document ready
