(function($) {
	var currentScroll;
	var menuSize;
	var adminBarSize;
	var prevScroll = $(window).scrollTop();
	
	/*
	* Function: menuVisible()
	*
	* Determines whether the floating menu should be displayed or not.
	*
	* Returns TRUE if text does not wrap, meaning there is sufficient room to display the entire menu.
	* Returns FALSE if the window is too small and would cause the text to wrap. 
	*/
	var menuVisible = function() {
		if ($('#floating-menu ul').width() + $('#floating-title').width() + 80 >= $(window).width() || $('#floating-menu ul').width() + $('#floating-title').width() + 80 >= 1070) 
			return false;
		else
			return true;
	}
	
	/*
	* Function: titleVisible()
	*
	* Determines whether the site title should be displayed or not.
	*
	* Returns TRUE if text does not wrap, meaning there is sufficient room to display the entire title.
	* Returns FALSE if the window is too small and would cause the text to wrap. 
	*/
	var titleVisible = function() {
		if ($('#floating-title').width() + 80 >= $(window).width() || $('#floating-title').width() + 80 >= 1070) 
			return false;
		else
			return true;
	}
	
	/* 
	* Function: showFloatingMenu()
	*
	* Displays the floating menu, accounting for the space taken up by the admin bar if applicable.
	*/
	var showFloatingMenu = function () {
		$('#floating-header-wrapper').filter(':not(:animated)').animate({
				top: adminBarSize + 0 + "px"
		}, 300);
	}
	
	/* 
	* Function: hideFloatingMenu()
	*
	* Hides the floating menu.
	*/
	var hideFloatingMenu = function () {
		$('#floating-header-wrapper').filter(':not(:animated)').animate({
				top: (menuSize + 49) * -1 + "px"
		}, 300);
	}	
	
	$(document).ready(function() {
		adminBarSize = $('#wpadminbar').height();
		if ( $(window).width() < 782) {
			showFloatingMenu();
		}
		if (menuVisible()) {
			$('#floating-menu ul').css('visibility', 'visible');
		}
		else if (!menuVisible()) {
			$('#floating-menu ul').css('visibility', 'hidden');
		}
		
		if (titleVisible())
			$('#floating-title').css('display', 'block');
		else
			$('#floating-title').css('display', 'none');
	});
	
	$(window).scroll(function() {
		currentScroll = $(window).scrollTop();
		
		menuSize = $('#floating-header-wrapper').height();
		adminBarSize = $('#wpadminbar').height();
		if ( $(window).width() > 782) {
		
			if (currentScroll < prevScroll && currentScroll > 1000) 
				showFloatingMenu();
		
			else  
				hideFloatingMenu();
		}
		prevScroll = currentScroll;
	});
	
	$(window).resize(function() {
		adminBarSize = $('#wpadminbar').height();
		if ( $(window).width() < 782) 
			showFloatingMenu();
		
		if ( $(window).width() >= 782 && currentScroll < 1000)
			hideFloatingMenu();
		
		if (menuVisible())
			$('#floating-menu ul').css('visibility', 'visible');
		else 
			$('#floating-menu ul').css('visibility', 'hidden');
		
		if (titleVisible())
			$('#floating-title').css('display', 'block');
		else
			$('#floating-title').css('display', 'none');
	});
	
}) ( jQuery );