<?php get_header(); ?>

</div> <!-- // top_wrap -->

<?php
global $wp_query, $post;

$term = $wp_query->queried_object;
$term_url = get_term_link( $term );
$layout = 	get_field( 'c_layout', $term );
$sb = 		get_field( 'c_sidebar', $term );
?>

<div class="grid"><!--

	<?php
	$frag = new IMI_Fragment_Cache( 'crb',  $term_url );

	if ( !$frag->output() ) {
	?>
		<?php echo '-->'; the_breadcrumbs(); echo '<!--'; ?>

		<?php

		$frag->store();
	}
	?>

	<?php
	$frag = new IMI_Fragment_Cache( 'tax',  $term_url, (get_query_var('paged')) ? get_query_var('paged') : false );

	if ( !$frag->output() ) {
	?>

		<?php
		$descendants = get_categories(array('child_of' => $term->term_id, 'parent' => $term->term_id, 'hide_empty' => 0));
		?>

		--><main id="main_internal" class="category ib<?php if ( $layout == 'x4' || !$sb ) { ?> full_width<?php } ?>">
			<div class="divin">

			<article>

				<h1 class="c1"><?php echo $term->name; ?></h1>

				<?php if (!is_paged() && $term_desc = get_field( 'c_desc', $term ) ) { ?>

				<div class="description runner"><?php echo $term_desc; ?></div>

				<?php } ?>

				<?php if ($layout == 'x4') { ?>

					<div class="gal_boxes4"><div>

						<?php if ($descendants && !is_paged()) { ?>

							<?php foreach ($descendants as $des) { ?>

							<section class="box ib">

								<div class="bg"><div class="box_style"></div></div>

								<div class="divin">

									<a href="<?php echo get_category_link( $des->term_id ); ?>" class="boxa">

										<?php if ( $c_image = get_field( 'c_image', $des ) ) { ?>

										<figure><img src="<?php echo $c_image['sizes']['x-size']; ?>" alt="<?php echo $c_image['alt']; ?>" /></figure>

										<?php } ?>

										<div class="gf_des">

											<h3 class="gf_title"><?php echo $des->name; ?></h3>

											<?php if ($desc = get_field( 'c_desc', $des ) ) { ?>

												<?php echo $desc; ?>

												<span class="gf_more"></span>

											<?php } ?>

										</div>

									</a>

								</div>

							</section>

							<?php } ?>

						<?php } ?>

						<?php get_template_part('loop'); ?>

					</div></div>

					<?php get_template_part('pagination'); ?>

				<?php } elseif ($layout == 'list') { ?>

					<section class="content_part">

						<?php if ($descendants && !is_paged()) { ?>

							<?php foreach ($descendants as $des) { ?>

								<?php $term_link = get_category_link( $des->term_id ); ?>

								<section class="cat_item"><div class="mock_table">

									<?php if ( $c_image = get_field( 'c_image', $des ) ) { ?>

									<figure class="td">

										<a title="<?php echo __('Continue Reading:','profile') . $des->name; ?>" href="<?php echo $term_link; ?>"><img src="<?php echo $c_image['sizes']['cat-size']; ?>" alt="<?php echo $c_image['alt']; ?>" /></a>

									</figure>

									<?php } ?>

									<div class="text runner td">

										<h3><a title="<?php echo __('Continue Reading:','profile') . $des->name; ?>" href="<?php echo $term_link; ?>"><?php echo $des->name; ?></a></h3>

										<?php if ($desc = get_field( 'c_desc', $des ) ) { ?>

											<?php echo $desc; ?>

										<?php } ?>

									</div>

								</div></section>

							<?php } ?>

						<?php } ?>

						<?php get_template_part('loop'); ?>

					</section>

					<?php get_template_part('pagination'); ?>

				<?php } ?>

			</article>

		</div></main><!--

		<?php

		$frag->store();
	}
	?>

	<?php if ($layout == 'list' && $sb) { ?>

	--><aside id="main_sidebar" class="ib"><div class="divin"><div>

		<?php if (function_exists('dynamic_sidebar')) { dynamic_sidebar('widget-category'); } ?>

	</div></div></aside><!--

	<?php } ?>

--></div>

<?php
$frag = new IMI_Fragment_Cache( 'frm',  $term_url, (get_query_var('paged')) ? get_query_var('paged') : false );

if ( !$frag->output() ) {
?>

	<?php if ( get_field( 'c_form', $term ) ) { ?>

		<?php get_template_part('form'); ?>

	<?php } ?>

	<?php

	$frag->store();
}
?>

<?php get_footer(); ?>
