jQuery(function($) {

// SMOOTH SCROLLING

	$('body a').smoothScroll();

// WAYPOINTS SCRIPT

	// nav menu
	$('#subnav-wrap, .scroll-nav').waypoint(function(event, direction) {
		$(this).parent().toggleClass('sticky', direction === "down");
		event.stopPropagation();
	});

	// Register each section as a waypoint.
	$('#answers > article').waypoint({ offset: '10%' });

	// The same for all waypoints
	$('body').delegate('#answers > article', 'waypoint.reached', function(event, direction) {
		var $active = $(this);

		if (direction === "up") {
			$active = $active.prev();
		}
		if (!$active.length) $active.end();

		$('.current').removeClass('current');
		$('a[href=#'+$active.attr('id')+']').addClass('current');
	});

	// goto 'Top' button
	$('.top').addClass('hidden');
	$.waypoints.settings.scrollThrottle = 25;
	$('#mainbody').waypoint(function(event, direction) {
		$('.top').toggleClass('hidden', direction === "up");
	}, {
		offset: '-100%'
	});

// AGENT LOADER SCRIPT

	// Apply "agent-current" class for nav items
	$('#agents-nav a').click(function(e){
		e.preventDefault();
		$('#agents-nav a').addClass('agent-current').not(this).removeClass('agent-current');
	});

	// Load and Unload Agent Content
	$('#agents-nav a').click(function(event) {
		event.preventDefault();
		$.smoothScroll({
			scrollElement: $('#agents-window'),
			scrollTarget: $(this).attr('href')
		});
		return false;
	});

// E-BULLETIN LOADER SCRIPT

	// Dynamically load external html file
	$('a.dynamicLoad').click(function(e) {
		e.preventDefault();   // prevent the browser from following the link
		e.stopPropagation();  // prevent the browser from following the link

		$('#articles').load($(this).attr('href'));
	});

});
