<?php
/*
Plugin Name: Twenty Fifteen Scroll
*/

function tf_script() {
	?>
	<script type="text/javascript">
	jQuery( document ).ready( function ( $ ) {
		
		if( $( '#content' ).height() < $( window ).height() && $( '#sidebar' )[0].scrollHeight > $( window ).height() ) {
			$( '#content' ).height( $( '#sidebar' ).height() );
		}

		var sidebar_scrollheight = $( '#sidebar' )[0].scrollHeight;
		var content_scrollheight = $( '#content' )[0].scrollHeight;
		var ratio = sidebar_scrollheight / content_scrollheight;
		var window_height = $( window ).height();
		var offset = content_scrollheight - window_height;
		var multiplier = content_scrollheight / offset;

		if( $( window ).width() > 967 ) {
			$( '#sidebar' ).css( 'overflow', 'hidden' );
			$( window ).scroll( function() {
				var content_position = $( window ).scrollTop();
				var sidebar_position = ( content_position >= 0 ? Math.round( content_position * ratio * multiplier ) : 0 );
				$( '#sidebar' ).scrollTop( Math.abs( sidebar_position ) );
			} );
		}
	} );
	</script>
	<?php
}
add_action( 'wp_head', 'tf_script' );


function tf_add_jquery() {
	wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'tf_add_jquery' );
