<?php
/*
 * The sidebar containing the main widget area.
 * Author & Copyright: themexshaper
 * URL: http://themeforest.net/user/themexshaper
 */
$confin_blog_widget = cs_get_option( 'blog_widget' );
$confin_single_blog_widget = cs_get_option( 'single_blog_widget' );
$confin_service_widget = cs_get_option( 'single_service_widget' );
$confin_service_sidebar_position = cs_get_option( 'service_sidebar_position' );
$confin_project_sidebar_position = cs_get_option( 'project_sidebar_position' );
$confin_project_widget = cs_get_option( 'single_project_widget' );
$confin_blog_sidebar_position = cs_get_option( 'blog_sidebar_position' );
$confin_blog_sidebar_position = isset($_GET['sidebar'])? $_GET['sidebar'] : $confin_blog_sidebar_position;
$confin_sidebar_position = cs_get_option( 'single_sidebar_position' );
$confin_sidebar_position = isset($_GET['sidebar'])? $_GET['sidebar'] : $confin_sidebar_position;
if ( is_home() || is_archive() || is_search() ) {
	$confin_blog_sidebar_position = $confin_blog_sidebar_position;
} else {
	$confin_blog_sidebar_position = '';
}
if ( is_single() ) {
	$confin_sidebar_position = $confin_sidebar_position;
} else {
	$confin_sidebar_position = '';
}

if ( is_singular( 'service' ) ) {
	$confin_service_sidebar_position = $confin_service_sidebar_position;
} else {
	$confin_service_sidebar_position = '';
}
if ( is_singular( 'project' ) ) {
	$confin_project_sidebar_position = $confin_project_sidebar_position;
} else {
	$confin_project_sidebar_position = '';
}
if ( is_page() ) {
	// Page Layout Options
	$confin_page_layout = get_post_meta( get_the_ID(), 'page_layout_options', true );
	if ( $confin_page_layout ) {
		$confin_page_sidebar_pos = $confin_page_layout['page_layout'];
	} else {
		$confin_page_sidebar_pos = '';
	}
} else {
	$confin_page_sidebar_pos = '';
}
if (isset($_GET['sidebar'])) {
  $confin_blog_sidebar_position = $_GET['sidebar'];
}
// sidebar class
if ( $confin_sidebar_position === 'sidebar-left' || $confin_page_sidebar_pos == 'left-sidebar' || $confin_blog_sidebar_position === 'sidebar-left' || $confin_project_sidebar_position === 'sidebar-left' ||  $confin_service_sidebar_position === 'sidebar-left' ) {
	$col_class = 'col-md-pull-8';
} else {
	$col_class = '';
}

if ( $confin_service_sidebar_position === 'sidebar-left' ) {
	$sv_push_class = ' col-lg-pull-9';
} else {
	$sv_push_class = '';
}

if ( is_singular( 'service' ) ) {
	$service_col = ' service-sidebar col-lg-3';
} else {
	$service_col = '';
}

?>
<div class="blog-sidebar col-md-4 <?php echo esc_attr( $col_class.$service_col.$sv_push_class ); ?>">
	<?php
	if (is_page() && isset( $confin_page_layout['page_sidebar_widget'] ) && !empty( $confin_page_layout['page_sidebar_widget'] ) ) {
		if ( is_active_sidebar( $confin_page_layout['page_sidebar_widget'] ) ) {
			dynamic_sidebar( $confin_page_layout['page_sidebar_widget'] );
		}
	} elseif (!is_page() && $confin_blog_widget && !$confin_single_blog_widget) {
		if ( is_active_sidebar( $confin_blog_widget ) ) {
			dynamic_sidebar( $confin_blog_widget );
		}
	}  elseif ( $confin_service_widget && is_singular( 'service' ) ) {
		if ( is_active_sidebar( $confin_service_widget ) ) {
			dynamic_sidebar( $confin_service_widget );
		}
	} elseif ( $confin_project_widget && is_singular( 'project' ) ) {
		if ( is_active_sidebar( $confin_project_widget ) ) {
			dynamic_sidebar( $confin_project_widget );
		}
	} elseif ( is_single() && $confin_single_blog_widget ) {
		if ( is_active_sidebar( $confin_single_blog_widget ) ) {
			dynamic_sidebar( $confin_single_blog_widget );
		}
	} else {
		if ( is_active_sidebar( 'sidebar-1' ) ) {
			dynamic_sidebar( 'sidebar-1' );
		}
	} ?>
</div><!-- #secondary -->
