$(function () {
	// This one is important, many browsers don't reset scroll on refreshes
	// Reset all scrollable panes to (0,0)
	$('div.pane').scrollTo( 0 );
	// Reset the screen to (0,0)
	$.scrollTo( 0 ); 
	// **********
	// make content header sticky when reaches top
	// **********
	var placeholder = $("#navHolder");
	var sticky = $("#titleBar");
	var logo = $(".logo");
	var view = $(window);
	// Bind to the window scroll and resize events.
	view.bind(
		"scroll resize",
		function(){
			// Get the current offset of the placeholder.
			var placeholderTop = placeholder.offset().top;
			// Get the current scroll of the window.
			var viewTop = view.scrollTop();
			// Check to see if the view had scroll down
			// past the top of the placeholder AND that
			// the sticky is not yet fixed.
			if (
				(viewTop > placeholderTop) &&
				!sticky.is( ".fixed" )
				){
				// The sticky needs to be fixed. Before
				// we change its positon, we need to re-
				// adjust the placeholder height to keep
				// the same space as the sticky.
				placeholder.height(
					placeholder.height()
				);
				// Make the sticky fixed.
				sticky.addClass( "fixed" );
				logo.addClass( "fixed" );
			// Check to see if the view has scroll back up
			// above the sticky AND that the sticky is
			// currently fixed.
			} else if (
				(viewTop <= placeholderTop +116) &&
				sticky.is( ".fixed" )
				){
				// Make the placeholder height auto again.
				placeholder.css( "height", "auto" );
				// Remove the fixed position class on the
				// sticky. This will pop it back into its
				// static position.
				sticky.removeClass( "fixed" );
				logo.removeClass( "fixed" );

			}
		}
	);
	// smooth scroll when sub-nav links clicked

   	$('.nav').localScroll({ // this is for main nav links on the home page
   		target: $(window), 
		stop: true,
		lock: true,
  		axis: 'y',
  		offset: {left: 0, top: -66},
   		queue: false,
   		duration: 800
	});

	
	// detect scroll, snap to top of element, change current sub-nav based on position;
	$(window).scroll(function(){
        var waitToScroll = 20000;
//		if(oldViewTop==viewTop){
//		    alert(viewTop);
//		}
		// if currently scrolling, stop
		$.doTimeout( 'scroll' );
		$.doTimeout( 'wait' );
	
		// wait a fraction of a second before starting to correct scroll
		$.doTimeout( 'wait', 400, function(){
		
			var viewTop = $(window).scrollTop();
			var offsetTop = placeholder.height()+1;
			var top0 = $("#header").offset().top;
			var top1 = $("#about").offset().top;
			var top2 = $("#work").offset().top;
			var top3 = $("#blog").offset().top;
	
			// compare window scroll to section
			if ( viewTop < (top1 - offsetTop) ) {
				navCurrent("#header");
			}
			if ( viewTop >= (top1 - offsetTop) ) {
				navCurrent("#about");
			}
			if ( viewTop >= (top2 - offsetTop) ) {
				navCurrent("#work");
			}
			if ( viewTop >= (top3 - offsetTop) ) {
				navCurrent("#blog");
			}
		
			// compare window scroll to section when using scroll bar or mouse wheel

			if ( viewTop > (top3 - 450) ) {
				$.doTimeout( 'scroll', waitToScroll, function(){
					$(window).scrollTo( $('#blog'), 500, {offset: {top: -66} } );	
				});		
			}
			if ( viewTop > (top2 - 450) && viewTop < (top3 - 450) ) {
				$.doTimeout( 'scroll', waitToScroll, function(){
					$(window).scrollTo( $('#work'), 500, {offset: {top: -66} } );
				});		
			}
			if ( viewTop > (top1 - 450) && viewTop < (top2 - 450) ) {
				$.doTimeout( 'scroll', waitToScroll , function(){
					$(window).scrollTo( $('#about'), 500, {offset: {top: -66} } );	
				});		
			}
			if ( viewTop < (top1 - 450) ) {
				$.doTimeout( 'scroll', waitToScroll , function(){
					$(window).scrollTo( $('#home'), 500, {offset: {top: -66} } );	
				});		
			}
		oldViewTop=viewTop;
		});

	});
	// current sub-nav function
	function navCurrent(targetID) {
		var targetNav = $("#nav li a[href="+targetID+"]");
		$("#nav li a").removeClass("current");
		targetNav.addClass("current");
	};
});

