$(function() {
	
	/**
	 * Make sidebar follow viewport.
	 * 
	 * @source http://css-tricks.com/4855-scrollfollow-sidebar/
	 *
	 * Adapted to make sidebar stay within parent container.
	 */
	var $sidebar   = $("#sidebar"),
		$parent    = $sidebar.parent(),
		$window    = $(window),
		offset     = $sidebar.offset(),
		topPadding = 0,
		bottomPadding = 32;
	
	if ( !$parent.hasClass('nofollow') ) {
		$window.scroll(function() {
			if ($window.scrollTop() > offset.top) {
				newMarginTop = $window.scrollTop() - offset.top + topPadding;
				
				if (($sidebar.outerHeight() + newMarginTop + bottomPadding) < $parent.height()) {
					$sidebar.css({
						marginTop: newMarginTop
					});
				}
			} else {
				$sidebar.css({
					marginTop: 0
				});
			}
			
			/* [Experimental] Update current sub-navigation based on most visible h2
			if( $('section.page h2[id]').mostVisible().attr('id') != location.hash.replace(/#\//,'') ) {
				setSubMenuItem( $('section.page h2[id]').mostVisible().attr('id') );
			}
			*/
		});
	}
	
	function setSubMenuItem(tgt) {
		$tgt = $('#menu-navigation .current_page_item .sub-menu a[href*='+tgt+']');
		if ($tgt) {
			$tgt.parent().siblings('.current_page_item').removeClass('current_page_item');
			$tgt.parent().addClass('current_page_item');
		}
	}
	
	$('a[href*=#comments], a[href*=#respond], a.footnote-link').smoothScroll();
	
	$(window).bind('hashchange', function(event) {
		if (location.hash) {
			var tgt = location.hash.replace(/#\//,'');
		} else {
			var tgt = '#top';
		}
		
		$.smoothScroll({
			scrollTarget: '#' + tgt,
			offset: -48
		});
		
		setSubMenuItem(tgt);
	});
		
	$(window).trigger('hashchange');
	
	$('#menu-navigation > li:not(.current_page_item):not(.current_page_parent)').hoverIntent({
		interval: 300,
		over: function() {
			$(this).children('.sub-menu').slideDown('fast');
		},
		out: function() {
			$(this).children('.sub-menu').slideUp('fast');
		}
	});
	
	// Indicate menu items that have a sub-menu
	$('#menu-navigation > li:has(.sub-menu)').addClass('has-sub-menu');
	
	// Transition testimonial
	function activateTestimonial() {
		$section = $('#client-testimonials');
		$clients = $('.clients', $section).first();
		$client = $(this).parent();
		$testimonials = $('.testimonials', $section).first();
		testimonial_anchor = $(this).data('href');
		$testimonial = $( testimonial_anchor );
		
		// reset current client style
		$client.siblings('.current').each(function() {
			$(this).removeClass('current');
			$(this).children('a').first().css('background-color', 'transparent');
		});
		
		// activate new client
		$client.addClass('current');
		
		// transition client color
		$(this).css({
			backgroundColor: $client.data('color')
		});
		
		// transition testimonial color
		$testimonials.css({
			backgroundColor: $client.data('color')
		});
		
		// fade out current quote, fade in new one
		$testimonial.siblings('.current').fadeOut(500, function() {
			$testimonial.fadeIn(500);
			$testimonial.siblings('.current').removeClass('current');
			$testimonial.addClass('current');
		});
		
		// if color is dark, use white text color
		if ( $.Color($client.data('color')).lightness() < 0.5 )
			$testimonials.addClass('alternate');
		else
			$testimonials.removeClass('alternate');
		
		/* Height animation is a little sluggish
		$testimonials.animate({
			height: $testimonial.height()
		}, 1000);
		*/
		
		return false;
	}
	
	$('#client-testimonials .client a')
		.hover(activateTestimonial)
		.click(activateTestimonial);
	
	// Remove anchor link to prevent jumping
	$('#client-testimonials .client a').each(function() {
		$(this).data('href', $(this).attr('href'));
		$(this).attr('href', '');
	});
	
	// Activate first testimonial
	$('#client-testimonials .client a:first').click();
	
	// Clients & Class Histories page
	$('.clients.table').hover(
		function() {
			$(this).find('.client a').each(function() {
				$client = $(this).parent();
				
				// transition client color
				$(this).css({
					backgroundColor: $client.data('color')
				});
			});
		},
		function() {
			$(this).find('.client a').each(function() {
				$(this).css({
					backgroundColor: 'transparent'
				});
			});
		}
	);
});
