$(function(){
    // 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;
        }    
    });
    
    // 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;
        });
    });
	
	/*$('.anchor').waypoint(function() {
	   alert('You have scrolled to an entry.');
	});*/
	
	$('div.anchor').waypoint(function(){offset:'50%'});
	
	$('body').delegate('div.anchor', 'waypoint.reached', function(event, direction) {
        var $active = $(this);
		if (direction === "up") {
			//alert( $active.prev().find('h3:first').html())
			$active = $active.prev()
		}
		if (!$active.length) $active.end(); 
		//alert($active.attr('name'));
		$('#act').removeAttr('id');
		$('a[href=#'+$active.attr('id')+']').attr('id','act');
    });
	
	
});
