function isScrolledIntoView(elem, theoffset) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top - theoffset;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

// Animate the scrolling around divs
$(document).ready(function($) {
	$(".topbar a.scroll").click(function(event){		
		event.preventDefault();
		$('html,body').animate({scrollTop:$(this.hash).offset().top - 33}, 500);
	});
	
	// Actions for when you hit a link with a .participate class
	$(".participate").click(function(event){		
		event.preventDefault();
		$('html,body').animate({scrollTop:$(this.hash).offset().top - 40}, 500);
	});
	
	// Detect if the participate button should get in your face
	$(window).scroll (function () {
		
		if (isScrolledIntoView($('#page-main'), 41)) {
			$('#participate_button').slideUp(100);
		} else {
			$('#participate_button').slideDown(100);
		}
		
		if (isScrolledIntoView($('#top_strip'), 0)) {
			$('#topbar').removeClass('fixed');
		} else {
			$('#topbar').addClass('fixed');
		}
		
	});
	
	// Scrolling action for the quotes
	$(".quotes").scrollable({disabledClass: 'inactive'});
	 
	
	// FAQ Action
	$('#faq_button').bind('click', function(e) {
		$('#faq_button').slideUp('fast', function() {
			$('#faq_content').slideDown('slow', function() {
				$('html,body').animate({scrollTop:$('#faq_content').offset().top - 30}, 500);
			});
		});
		e.preventDefault();
	});
	
});
