function pageScroll() {



	this.scrollTo = function(element) {
	
		var offset = $(element).offset();
		var top = ($('#page-header.fixed').length > 0) ? offset.top - $('#page-header.fixed').height() : offset.top;
	
	    $('html, body').animate({
	        scrollTop: top
	    }, 1200, 'easeOutQuint');
	
	};



	this.listen = function() {
		
		var scrollTo = this.scrollTo;
		
		$('a[data-destination=internal]').click(function() {
			scrollTo($(this).attr('href'));
			return false;
		});
	
	};
	
	
	
	this.listen();



}



function navMarker() {
	
	
	
	this.activePosition = null;
	
	
	
	this.hoverPosition = null;
	
	
	
	this.move = function(position) {
		if(!$.browser.msie) {
			$('#marker').css({
				left: position
			});
		} else {
			$('#marker').animate({
				left: position
			}, 300);
		}
	};
	
	
	
	this.calculatePosition = function(item) {
		return item.position().left + item.width() / 2 - $('#marker').width() / 2;
	};
	
	
	
	this.setActivePosition = function() {
		if($('#navigation .current a').length == 1) {
			this.activePosition = this.calculatePosition($('#navigation .current a'));
			this.move(this.activePosition);
		} else {
			this.activePosition = 0;
		}
	};
	
	
	
	this.setHoverPosition = function(item) {
		this.hoverPosition = this.calculatePosition(item);
		this.move(this.hoverPosition);
	};
	
	
	
	this.listen = function() {
	
		var setHoverPosition = this.setHoverPosition;
		var move = this.move;
		var activePosition = this.activePosition;
	
		$('#navigation a').mouseover(function() {
			setHoverPosition($(this));
		});
		
		$('#navigation').mouseleave(function() {
			move(activePosition);
		});
		
	};



	this.setActivePosition();
	
	this.listen();


	
}



function barFix() {



	/**
	* The offset of the fixed element
	*/
	this.barTop = null;
	
	
	
	/**
	* The fixed element/s
	*/
	this.fixed = null;
	
	
	
	/**
	* A clone of the fixed element inserted into the DOM so that setting the element to position fixed
	* doesn't break the layout.
	*/
	this.clone = null;



	this.init = function() {
	
		if($('.fixed').length > 0) {
		
			var self = this;
			
			this.fixed = $('.fixed');
		
			this.barTop = $('.fixed').offset().top;
			
			this.clone = this.fixed.clone().hide().css({
				opacity: 0
			}).insertAfter($('#header'));
			
			this.checkScrollTop();
			
			$(window).scroll(function() {
				self.checkScrollTop();
			});
		
		}
	
	};
	
	
	
	/**
	* Check whether the fixed element is off screen
	*/
	this.checkScrollTop = function() {
	
		if($(window).scrollTop() >= self.barTop) {
			this.fixed.addClass('is-fixed');
			this.clone.show();
		} else {
			this.fixed.removeClass('is-fixed');
			this.clone.hide();
		}
		
	};
	
	
	
	this.init();



}



/**
* Control the fixed page navigation
*/
function fixedPageNavigation() {



	this.enabled = false;



	/**
	* The navigation list - a jQ object
	*/
	this.navigation = $('#page-header.fixed li');
	
	
	
	/**
	* The sections that are being linked to
	*/
	this.section = {};



	this.init = function() {
	
		var self = this;
	
		this.enabled = this.navigation.length > 0;
		
		if(this.enabled) {
		
			self.navigation.each(function(i) {
				self.section[i] = $($(this).find('a').attr('href'));				
				self.section[i].offsetTop = $(self.section[i]).offset().top - $('#page-header.fixed').height();
			});
			
			this.listen();
			
		}
		
	};
	
	
	
	/**
	* Listen for the window scroll function
	* Set the correct nav item as active depending on which section is on screen
	*/
	this.listen = function() {
		
		var self = this;
		
		var checkActive = function() {
			
			var scrollTop = $(window).scrollTop();
			
			$.each(self.section, function(i) {
				
				var i = parseInt(i);
				
				if(scrollTop >= self.section[i].offsetTop - 100) {
					self.setNavActive(i);
				}
				
			});
			
		};
		
		checkActive();
		
		$(window).scroll(function() {
			checkActive();
		});
		
	};
	
	
	
	/**
	* Set the correct nav item active
	*/
	this.setNavActive = function(i) {
	
		this.navigation.find('.inset').removeClass('inset').addClass('outset');
		this.navigation.eq(i).find('.outset').removeClass('outset').addClass('inset');
	
	};
	
	

	this.init();



}



$(document).ready(function() {

	$('input:not(.disable-placeholder, .button), textarea:not(.disable-placeholder)').placeholder();
	barFix();
	fixedPageNavigation();
	
});

$(window).load(function() {

	navMarker();
	pageScroll();
	
});
