<?php
/**
 * The Sidebar containing the main widget area.
 *
 * @package Catch Themes
 * @subpackage Catch Flames Pro
 * @since Catch Flames Pro 1.0
 */
?>

<?php
/**
 * catchflames_before_secondary hook
 */
do_action( 'catchflames_before_secondary' );

	//Getting Ready to load data from Theme Options Panel
	global $post, $wp_query, $catchflames_options_settings;
   	$options = $catchflames_options_settings;
	$themeoption_layout = $options['sidebar_layout'];

	// Front page displays in Reading Settings
	$page_on_front = get_option('page_on_front') ;
	$page_for_posts = get_option('page_for_posts'); 

	// Get Page ID outside Loop
	$page_id = $wp_query->get_queried_object_id();
	
	// Blog Page setting in Reading Settings
	if ( $page_id == $page_for_posts ) {
		$layout = get_post_meta( $page_for_posts,'catchflames-sidebarlayout', true );
		$sidebaroptions = get_post_meta( $page_for_posts, 'catchflames-sidebar-options', true );
	}	
	// Front Page setting in Reading Settings
	elseif ( $page_id == $page_on_front ) {
		$layout = get_post_meta( $page_on_front,'catchflames-sidebarlayout', true );
		$sidebaroptions = get_post_meta( $page_on_front, 'catchflames-sidebar-options', true );
	}	
	// Settings for page/post/attachment
	elseif ( is_singular() ) {
		if ( is_attachment() ) { 
			$parent = $post->post_parent;
			$layout = get_post_meta( $parent, 'catchflames-sidebarlayout', true );
			$sidebaroptions = get_post_meta( $parent, 'catchflames-sidebar-options', true );
		} else {
			$layout = get_post_meta( $post->ID, 'catchflames-sidebarlayout', true ); 
			$sidebaroptions = get_post_meta( $post->ID, 'catchflames-sidebar-options', true );  
		}
	}
	else {
		$layout = 'default';	
		$sidebaroptions = '';
	}

	//check empty and load default
	if ( empty( $layout ) ) {
		$layout = 'default';	
	}	
	
	// WooCommerce Settings
	if ( !is_active_sidebar( 'catchflames_woocommerce_sidebar' ) && ( class_exists( 'Woocommerce' ) && is_woocommerce() ) ) {
		$layout = 'no-sidebar';
	}

	if ( $layout == 'left-sidebar' || $layout == 'right-sidebar' || $layout == 'three-columns' || $layout == 'three-columns-sidebar' || $layout == 'three-columns-equal' || ( $layout=='default' && $themeoption_layout == 'left-sidebar' ) || ( $layout=='default' && $themeoption_layout == 'right-sidebar' ) || ( $layout=='default' && $themeoption_layout == 'three-columns' ) || ( $layout=='default' && $themeoption_layout == 'three-columns-sidebar' ) || ( $layout=='default' && $themeoption_layout == 'three-columns-equal' ) ) :
	?>
        <div id="secondary" class="widget-area" role="complementary">
			<?php
			/**
			 * catchflames_before_widget hook
			 */
			do_action( 'catchflames_before_widget' );

			if ( is_active_sidebar( 'catchflames_woocommerce_sidebar' ) && ( class_exists( 'Woocommerce' ) && is_woocommerce() ) ) {
				dynamic_sidebar( 'catchflames_woocommerce_sidebar' );
			}
			elseif ( is_active_sidebar( 'catchflames_optional_sidebar_one' ) && $sidebaroptions == 'optional-sidebar-one' ) {
				dynamic_sidebar( 'catchflames_optional_sidebar_one' );
			}
			elseif ( is_active_sidebar( 'catchflames_optional_sidebar_two' ) && $sidebaroptions == 'optional-sidebar-two' ) {
				dynamic_sidebar( 'catchflames_optional_sidebar_two' );
			}
			elseif ( is_active_sidebar( 'catchflames_optional_sidebar_three' ) && $sidebaroptions == 'optional-sidebar-three' ) {
				dynamic_sidebar( 'catchflames_optional_sidebar_three' );
			}
			elseif ( is_active_sidebar( 'catchflames_home_sidebar' ) && ( is_front_page() || ( is_home() && $page_id != $page_for_posts ) ) ) {
				dynamic_sidebar( 'catchflames_home_sidebar' );
			}
			elseif ( is_active_sidebar( 'catchflames_archive_sidebar' ) && ( $page_id == $page_for_posts || is_archive() || is_page_template( 'template-blog.php' ) ) ) {
				dynamic_sidebar( 'catchflames_archive_sidebar' );
			}
            elseif ( is_page() && is_active_sidebar( 'catchflames_page_sidebar' ) ) {
                dynamic_sidebar( 'catchflames_page_sidebar' );
            }
            elseif ( is_single() && is_active_sidebar( 'catchflames_post_sidebar' ) ) {
                dynamic_sidebar( 'catchflames_post_sidebar' );
            }
			elseif ( is_active_sidebar( 'sidebar-1' ) ) {
				dynamic_sidebar( 'sidebar-1' );
			}
            else { ?>
    			<aside class="widget widget_text">
                	<h3 class="widget-title"><?php _e( 'Primary Sidebar Widget Area', 'catch-flames' ); ?></h3>
                    <div class="textwidget">
                    	<p><?php _e( 'This is the Primary Sidebar Widget Area if you are using a two or three column site layout option.', 'catch-flames' ); ?></p>
                    	<p><?php printf( __( 'You can add content to this area by visiting your <a href="%s">Widgets Panel</a> and adding new widgets to this area.', 'catch-flames' ), admin_url( 'widgets.php' ) ); ?></p>
                  	</div>
				</aside>
            <?php
            }

			/**
			 * catchflames_after_widget hook
			 */
			do_action( 'catchflames_after_widget' ); ?>

        </div><!-- #secondary .widget-area -->

    <?php endif;

/**
 * catchflames_after_secondary hook
 */
do_action( 'catchflames_after_secondary' ); ?>