$jq( document ).ready( function() {
	$jq( "#topPanel" ).each( function( index ) {
		var panel = $jq( this );

		var contentWrap = $jq( document.createElement( "div" ) );
		var content = panel.children( ".topPanelContent" );
		var menuWrap = $jq( document.createElement( "div" ) );
		var menu = panel.children( ".topPanelMenu" );
		var openButtons = menu.find( ".open" );
		var closeButtons = menu.find( ".close" );
		var buttons = menu.find( ".open, .close" );
		
		contentWrap.attr( "class", "contentWrap" );
		menuWrap.attr( "class", "menuWrap" );

		content.wrap( contentWrap );		
		menu.wrap( menuWrap );
	
		var boxes = menu.children( ".box" );
		var boxCount = boxes.size();
		boxes.each( function( index ) {
			var box = $jq( this );
		
			if ( index == boxCount - 1 )
				return;
		
			var separator = $jq( document.createElement( "div" ) );
			separator.attr( "class", "separator" );
			box.after( separator );
		} );
	
		openButtons.show();
	
		openButtons.click( function() {
			content.slideDown();			
			buttons.toggle();
		} );

		closeButtons.click( function() {
			content.slideUp();			
			buttons.toggle();
		} );
	} );
	
	// design mode
	var designMode = $jq( ".PagePlaceholder #topPanel" );
	if ( designMode.size() > 0 ) {
		designMode.find( ".open" ).first().click();
		designMode.css( "position", "static" );
	} else if ( $jq( "#topPanel .loginForm .loginError" ).text().length > 0 ) {
		// open on login error
		$jq( "#topPanel .open" ).first().click();
	}
} );
