$(function() {
	var current = '';
	
	$('.sidenav-menu').each(function () {
		var $$ = $(this);
		
		if ($.cookie('sidenav-panel') == $$.attr('title')) {
			current = $$.attr('title');
		} else {
			$$.css('display', 'none');
		}
		
		$('#sidenav-'+$$.attr('title')).click(function () {
			if (!current) {
				$$.slideToggle('slow');
				$.cookie('sidenav-panel', $$.attr('title'));
				current = $$.attr('title');
			} else if (current == $$.attr('title')) {
				$$.slideToggle('slow');
				$.cookie('sidenav-panel', '');
				current = '';
			} else {
				$('#'+current+'-dropdown').slideToggle('slow', function () {
					$$.slideToggle('slow');
					$.cookie('sidenav-panel', $$.attr('title'));
					current = $$.attr('title');
				});
			}
			
			return false;
		});
	});
	
	var speed = 250;
	
	$('#top-nav li').find('a')
		.removeClass('nav-hover')
		.append('<span class="fade" />').each(function() {
				var $span = $('> span.fade', this).css('opacity', 0);
			$(this).hover(function () {
				$span.stop().fadeTo(speed, 1);
			}, function () {
				$span.stop().fadeTo(speed, 0);
			});
			});
});