$(document).ready(function() {

	$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};

	$('.leftmenu a').click(function(){ $.scrollTo( this.hash, 1000); });
	
	$('div.startPanels').scrollTo( 0 );
	$.scrollTo( 0 );
	var $paneTarget = $('.startPanels');
	
	$('.startPanels .next').click(function(){
		var $target = $(this).parent().parent().parent().next();
		$paneTarget.stop().scrollTo( $target , 800 );
	});
	
	$('.startPanels .prev').click(function(){
		var $target = $(this).parent().parent().parent().prev();
		$paneTarget.stop().scrollTo( $target , 800 );
	});

});

jQuery(function ($) {
	// Register each section as a waypoint.
	$('.menupost').waypoint({ offset: '50%' });

	// The same for all waypoints
	$('body').delegate('.menupost', 'waypoint.reached', function (event, direction) {
		var $active = $(this);

		if (direction === "up") {
			$active = $active.prev();
		}
		if (!$active.length) $active.end();

		$('.section-active').removeClass('section-active');
		$active.addClass('section-active');

		$('.selected').removeClass('selected');
		$('a[href=#' + $active.attr('id') + ']').addClass('selected');
	});

	// Negates the flash of non-active nav.
	$('body .leftmenu a').click(function () {
		$(this).addClass('selected');
	}).eq(0).addClass('selected');

	// Wicked credit to
	// http://www.zachstronaut.com/posts/2009/01/18/jquery-smooth-scroll-bugs.html
	var scrollElement = 'html, body';
	$('html, body').each(function () {
		var initScrollTop = $(this).attr('scrollTop');
		$(this).attr('scrollTop', initScrollTop + 1);
		if ($(this).attr('scrollTop') == initScrollTop + 1) {
			scrollElement = this.nodeName.toLowerCase();
			$(this).attr('scrollTop', initScrollTop);
			return false;
		}
	});

});

$(document).ready(function () {
	$.waypoints.settings.scrollThrottle = 0;
	$('#container').waypoint(function (event, direction) {
		offset: '-100%'
	}).find('.leftmenu').waypoint(function (event, direction) {
		$(this).parent().toggleClass('sticky', direction === "down");
		event.stopPropagation();
	});
});
