<?php
/**
 * The Sidebar containing the main widget areas.
 *
 * @package TOURIMO
 * @since TOURIMO 1.0
 */

if ( tourimo_sidebar_present() ) {
	
	$tourimo_sidebar_type = tourimo_get_theme_option( 'sidebar_type' );
	if ( 'custom' == $tourimo_sidebar_type && ! tourimo_is_layouts_available() ) {
		$tourimo_sidebar_type = 'default';
	}
	
	// Catch output to the buffer
	ob_start();
	if ( 'default' == $tourimo_sidebar_type ) {
		// Default sidebar with widgets
		$tourimo_sidebar_name = tourimo_get_theme_option( 'sidebar_widgets' );
		tourimo_storage_set( 'current_sidebar', 'sidebar' );
		if ( is_active_sidebar( $tourimo_sidebar_name ) ) {
			dynamic_sidebar( $tourimo_sidebar_name );
		}
	} else {
		// Custom sidebar from Layouts Builder
		$tourimo_sidebar_id = tourimo_get_custom_sidebar_id();
		do_action( 'tourimo_action_show_layout', $tourimo_sidebar_id );
	}
	$tourimo_out = trim( ob_get_contents() );
	ob_end_clean();
	
	// If any html is present - display it
	if ( ! empty( $tourimo_out ) ) {
		$tourimo_sidebar_position    = tourimo_get_theme_option( 'sidebar_position' );
		$tourimo_sidebar_position_ss = tourimo_get_theme_option( 'sidebar_position_ss' );
		?>
		<div class="sidebar widget_area
			<?php
			echo ' ' . esc_attr( $tourimo_sidebar_position );
			echo ' sidebar_' . esc_attr( $tourimo_sidebar_position_ss );
			echo ' sidebar_' . esc_attr( $tourimo_sidebar_type );

			if ( 'float' == $tourimo_sidebar_position_ss ) {
				echo ' sidebar_float';
			}
			$tourimo_sidebar_scheme = tourimo_get_theme_option( 'sidebar_scheme' );
			if ( ! empty( $tourimo_sidebar_scheme ) && ! tourimo_is_inherit( $tourimo_sidebar_scheme ) ) {
				echo ' scheme_' . esc_attr( $tourimo_sidebar_scheme );
			}
			?>
		" role="complementary">
			<?php

			// Skip link anchor to fast access to the sidebar from keyboard
			?>
			<a id="sidebar_skip_link_anchor" class="tourimo_skip_link_anchor" href="#"></a>
			<?php

			do_action( 'tourimo_action_before_sidebar_wrap', 'sidebar' );

			// Button to show/hide sidebar on mobile
			if ( in_array( $tourimo_sidebar_position_ss, array( 'above', 'float' ) ) ) {
				$tourimo_title = apply_filters( 'tourimo_filter_sidebar_control_title', 'float' == $tourimo_sidebar_position_ss ? esc_html__( 'Show Sidebar', 'tourimo' ) : '' );
				$tourimo_text  = apply_filters( 'tourimo_filter_sidebar_control_text', 'above' == $tourimo_sidebar_position_ss ? esc_html__( 'Show Sidebar', 'tourimo' ) : '' );
				?>
				<a href="#" class="sidebar_control" title="<?php echo esc_attr( $tourimo_title ); ?>"><?php echo esc_html( $tourimo_text ); ?></a>
				<?php
			}
			?>
			<div class="sidebar_inner">
				<?php
				do_action( 'tourimo_action_before_sidebar', 'sidebar' );
				tourimo_show_layout( preg_replace( "/<\/aside>[\r\n\s]*<aside/", '</aside><aside', $tourimo_out ) );
				do_action( 'tourimo_action_after_sidebar', 'sidebar' );
				?>
			</div><!-- /.sidebar_inner -->
			<?php

			do_action( 'tourimo_action_after_sidebar_wrap', 'sidebar' );

			?>
		</div><!-- /.sidebar -->
		<div class="clearfix"></div>
		<?php
	}
}
