<?php
/*
===================================================================================================
WARNING! DO NOT EDIT THIS FILE OR ANY TEMPLATE FILES IN THIS THEME!

To make it easy to update your theme, you should not edit this file. Instead, you should create a
Child Theme first. This will ensure your template changes are not lost when updating the theme.

You can learn more about creating Child Themes here: http://codex.wordpress.org/Child_Themes

You have been warned! :)
===================================================================================================
*/
?>
<?php

if(is_page()):
	if(have_posts()): while(have_posts()): the_post();
		$page_options_meta = get_post_meta($post->ID,'_page_options',true);
		if(isset($page_options_meta['ct_page_layout'])):
			$page_layout = $page_options_meta['ct_page_layout'];
		else:
			$page_layout = null;
		endif;
		if(isset($page_options_meta['ct_page_sidebar'])):
			$page_sidebar = $page_options_meta['ct_page_sidebar'];
		else:
			$page_sidebar = null;
		endif;
	endwhile; endif;
endif;

$post_type = get_query_var('post_type');

$theme_options = get_option('ct_theme_options');
$search_results_layout = $theme_options['search_results_layout'];
$search_results_sidebar = $theme_options['search_results_sidebar'];

$post_settings = get_option('ct_post_settings');
$post_archive_layout = $post_settings['archive_layout'];
$post_archive_sidebar = $post_settings['archive_sidebar'];
$post_single_layout = $post_settings['single_layout'];
$post_single_sidebar = $post_settings['single_sidebar'];

$sermon_settings = get_option('ct_sermon_settings');
$sm_archive_layout = $sermon_settings['archive_layout'];
$sm_archive_sidebar = $sermon_settings['archive_sidebar'];
$sm_single_layout = $sermon_settings['single_layout'];
$sm_single_sidebar = $sermon_settings['single_sidebar'];

$location_settings = get_option('ct_location_settings');
$loc_archive_layout = $location_settings['archive_layout'];
$loc_archive_sidebar = $location_settings['archive_sidebar'];
$loc_single_layout = $location_settings['single_layout'];
$loc_single_sidebar = $location_settings['single_sidebar'];

$person_settings = get_option('ct_person_settings');
$ppl_archive_layout = $person_settings['archive_layout'];
$ppl_archive_sidebar = $person_settings['archive_sidebar'];
$ppl_single_layout = $person_settings['single_layout'];
$ppl_single_sidebar = $person_settings['single_sidebar'];

?>
<?php
	if(
		(is_page() && ($page_layout == 'right' || empty($page_layout))) || 
		((is_search() && $post_type != 'ct_sermon') && ($search_results_layout == 'right' || empty($search_results_layout))) || 
		((is_post_type_archive('post') || is_day() || is_month() || is_year() || is_tag() || is_category() || is_author()) && ($post_archive_layout == 'right' || empty($post_archive_layout))) || 
		(is_singular('post') && ($post_single_layout == 'right' || empty($post_single_layout))) || 
		((is_post_type_archive('ct_sermon') || is_tax('sermon_speaker') || is_tax('sermon_series') || is_tax('sermon_service') || is_tax('sermon_topic')) && ($sm_archive_layout == 'right' || empty($sm_archive_layout))) || 
		(is_singular('ct_sermon') && ($sm_single_layout == 'right' || empty($sm_single_layout))) || 
		((is_post_type_archive('ct_location') || is_tax('location_tag')) && ($loc_archive_layout == 'right' || empty($loc_archive_layout))) || 
		(is_singular('ct_location') && ($loc_single_layout == 'right' || empty($loc_single_layout))) || 
		((is_post_type_archive('ct_person') || is_tax('person_tag') || is_tax('person_category')) && ($ppl_archive_layout == 'right' || empty($ppl_archive_layout))) || 
		(is_singular('ct_person') && ($ppl_single_layout == 'right' || empty($ppl_single_layout)))
	):
?>
			<div id="sidebar" class="grid_4 omega">
<?php
	elseif(
		(is_page() && $page_layout == 'left') || 
		((is_search() && $post_type != 'ct_sermon') && $search_results_layout == 'left') || 
		((is_post_type_archive('post') || is_day() || is_month() || is_year() || is_tag() || is_category() || is_author()) && $post_archive_layout == 'left') || 
		(is_singular('post') && $post_single_layout == 'left') || 
		((is_post_type_archive('ct_sermon') || is_tax('sermon_speaker') || is_tax('sermon_series') || is_tax('sermon_service') || is_tax('sermon_topic')) && $sm_archive_layout == 'left') || 
		(is_singular('ct_sermon') && $sm_single_layout == 'left') || 
		((is_post_type_archive('ct_location') || is_tax('location_tag')) && $loc_archive_layout == 'left') || 
		(is_singular('ct_location') && $loc_single_layout == 'left') || 
		((is_post_type_archive('ct_person') || is_tax('person_tag') || is_tax('person_category')) && $ppl_archive_layout == 'left') || 
		(is_singular('ct_person') && $ppl_single_layout == 'left')
	):
?>
			<div id="sidebar" class="grid_4 alpha">
<?php endif; ?>
				<?php
					if(is_page()):
						if($page_sidebar):
							dynamic_sidebar($page_sidebar);
						else:
							dynamic_sidebar('Primary Sidebar');
						endif;
					endif;
				?>
				<?php
					if(is_search() && $post_type != 'ct_sermon'):
						if($search_results_sidebar):
							dynamic_sidebar($search_results_sidebar);
						else:
							dynamic_sidebar('Primary Sidebar');
						endif;
					endif;
				?>
				<?php
					if(is_post_type_archive('post') || is_day() || is_month() || is_year() || is_tag() || is_category() || is_author()):
						if($post_archive_sidebar):
							dynamic_sidebar($post_archive_sidebar);
						else:
							dynamic_sidebar('Primary Sidebar');
						endif;
					endif;
				?>
				<?php
					if(is_singular('post')):
						if($post_single_sidebar):
							dynamic_sidebar($post_single_sidebar);
						else:
							dynamic_sidebar('Primary Sidebar');
						endif;
					endif;
				?>
				<?php
					if(is_post_type_archive('ct_sermon') || is_tax('sermon_speaker') || is_tax('sermon_series') || is_tax('sermon_service') || is_tax('sermon_topic' || $post_type == 'ct_sermon')):
						if($sm_archive_sidebar):
							dynamic_sidebar($sm_archive_sidebar);
						else:
							dynamic_sidebar('Primary Sidebar');
						endif;
					endif;
				?>
				<?php
					if(is_singular('ct_sermon')):
						if($sm_single_sidebar):
							dynamic_sidebar($sm_single_sidebar);
						else:
							dynamic_sidebar('Primary Sidebar');
						endif;
					endif;
				?>
				<?php
					if(is_post_type_archive('ct_location') || is_tax('location_tag')):
						if($loc_archive_sidebar):
							dynamic_sidebar($loc_archive_sidebar);
						else:
							dynamic_sidebar('Primary Sidebar');
						endif;
					endif;
				?>
				<?php
					if(is_singular('ct_location')):
						if($loc_single_sidebar):
							dynamic_sidebar($loc_single_sidebar);
						else:
							dynamic_sidebar('Primary Sidebar');
						endif;
					endif;
				?>
				<?php
					if(is_post_type_archive('ct_person') || is_tax('person_tag') || is_tax('person_category')):
						if($ppl_archive_sidebar):
							dynamic_sidebar($ppl_archive_sidebar);
						else:
							dynamic_sidebar('Primary Sidebar');
						endif;
					endif;
				?>
				<?php
					if(is_singular('ct_person')):
						if($ppl_single_sidebar):
							dynamic_sidebar($ppl_single_sidebar);
						else:
							dynamic_sidebar('Primary Sidebar');
						endif;
					endif;
				?>
			</div>