var userClicked = false;
$(document).ready(function() {

		
	$("ul#main-menu li a:first").addClass("active");
	//Needs refinement: auto highlights button associated with element closest to header.
	//NOTE: currently assumes the name of the ideas include sequential numbers as last character (pod-1, pod-2, etc.)
	$(window).scroll(function() {
			$.each($('div.pod'), function(el) {
				if (userClicked) return;
				if ($(this).attr('id')) {
					if ($(window).scrollTop() > $('#footer').position().top-250) {
						deactivate($('a.active'));
						return;
					} else if ($(window).scrollTop() > ($(this).position().top-250) && $(window).scrollTop() < ($(this).position().top+($(this).height()/2))) {		
						var navButton = $('li#nav-'+$(this).attr('id') +' a');
						activate(navButton);
						return;
					}	

				}
			});
	});

	//--attach click event that scroll to section associated with button
	//**NOTE: assumes, href of link matches id of content element	eg. #pod-1 will scroll page to element with id="pod-1"
	$('a[href*=#]').click(function() {
  	userClicked=true;
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	    && location.hostname == this.hostname) {
				var $target = $(this.hash);
	      $target = $target.length && $target
	      || $('[id=' + this.hash.slice(1) +']');
				if ($(this).parent().attr('id').indexOf('nav')!=-1) {					
					activate($(this));
				}
	 			if ($target.length) {
	        var topOffset = $target.offset().top-$('#top-nav').height();
					scrollTo(topOffset);
	        return false;
	      }

	    }
	  });
 });

function scrollTo(n) {
	$('html,body').animate({scrollTop: n}, 1000, function(){userClicked=false;});
}

function activate(element) {
	var activeTab = $('a.active');
	deactivate(activeTab);
	if (element.parent().parent('ul').attr('id')=='main-menu') {
		element.addClass('active');
	} 
}

function deactivate(element) {
	if (element == undefined) return;
	element.removeClass('active');
}
