$('a[href=#legal]').click(function() {
	$('#legal').slideToggle('slow');
});

$('.section a[href=#home]').addClass('highlight').after('<img src="images/nav-highlight.gif" class="nav-highlight" />');

/*--------------------------------------------------------------------------------*/
// Register each section as a waypoint.
/*--------------------------------------------------------------------------------*/
$('.section:not("#legal,#footer")').waypoint({ offset: 40 });
$('.section#home').waypoint({ offset: 103 });

/*--------------------------------------------------------------------------------*/
// The same for all waypoints
/*--------------------------------------------------------------------------------*/
$('body').delegate('.section', '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');
	
	$('.highlight').removeClass('highlight');
	$('.nav-highlight').remove();
	$('.section a[href=#'+$active.attr('id')+']').addClass('highlight').after('<img src="images/nav-highlight.gif" class="nav-highlight" />');
});

/*--------------------------------------------------------------------------------*/
// Negates the flash of non-active nav.
/*--------------------------------------------------------------------------------*/
$('.nav a').click(function() {
	$(this).addClass('highlight');
}).eq(0).addClass('highlight');

/*--------------------------------------------------------------------------------*/
// Smooth scroll bug fix
/*--------------------------------------------------------------------------------*/
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;
	}
});

/*--------------------------------------------------------------------------------*/
// Smooth scrolling for internal links
/*--------------------------------------------------------------------------------*/
$("a[href^='#']").click(function(event) {
	event.preventDefault();
	var $this = $(this),
	target = this.hash,
	$target = $(target);
	$(scrollElement).stop().animate({
		'scrollTop': $target.offset().top
		}, 500, 'swing', function() {
		window.location.hash = target;
	});
});

//$('a.highlight').append('<img src="../images/nav-highlight.gif" />');
