$(function(){
	// Set up expand/collapse of toc and sidebar items
	$('#toc h3, #sidetoc h3, #p-tb h3').click(function(){
		if ( $(this).is('.collapsed') ) {
			$(this).removeClass('collapsed').next('ul').slideDown('fast');
		} else {
			$(this).addClass('collapsed').next('ul').slideUp('fast');
		}
	});

	//initially collapse all items, except the container parent (wikiSidebarBox) of any items with class 'expanded'
	$('#sidetoc h3, #p-tb h3').each(function(){
		if ( !$(this).next('ul').find('.expanded').length ) {
			$(this).addClass('collapsed').next('ul').hide();
		}
	});

	// Sidebar resizing code
	$('.wikiSidebar').resizable({
		autoHide: true,
		minWidth: '175',
		stop: function(e, ui){
			//reset the sidebar height
			$('.wikiSidebar').css('height','100%');
			$.cookie('sidebarWidth', $('.wikiSidebar').css('width') )
		}
	});

	// Remember sidebar state/size across docs site
	var sidebarWidthCookieValue = $.cookie('sidebarWidth');

	if ( sidebarWidthCookieValue ) {
		$('.docsPage .wikiSidebar').css('width',sidebarWidthCookieValue);
	}

	var sidebarStateValue = $.cookie('sidebarState');

	if ( sidebarStateValue == 'collapsed') {
		$('.docsPage .wikiSidebar').hide();
		$('.docsPage .sidebarOpener').show();
	} 		

	// Sidebar open/close code

	// sidebar closer click event
	$('.sidebarCloser').click(function(){
		$('.wikiSidebar').hide();
		$('.sidebarOpener').show();
		$.cookie('sidebarState', 'collapsed');
	});

	// sidebar opener click event
	$('.sidebarOpener').click(function(){
		$('.wikiSidebar').show();
		$(this).hide();
		$.cookie('sidebarState', 'expanded');
	});

});
