<?php

get_header();

extract(etheme_get_blog_sidebar());
$postspage_id = get_option('page_for_posts');

$posts_count = 0;
$total_posts = count($posts);

$sidebarname = $position = null;

$blog_page = get_page_by_title('blog');

if ( $blog_page ) {
	$ot_page_options = get_post_meta($blog_page->ID);

	if ( isset($ot_page_options['widget_area']) && $ot_page_options['widget_area'][0] ) {
		$sidebarname = $ot_page_options['widget_area'][0];

		if ( isset($ot_page_options['sidebar_state']) ) {
			$position = is_array($ot_page_options['sidebar_state']) ? $ot_page_options['sidebar_state'][0] : $ot_page_options['sidebar_state'];
		}
	} elseif ( is_active_sidebar( 'blog-sidebar' ) ) {
		$sidebarname = 'blog-sidebar';
		$position = etheme_get_option('blog_sidebar');
	}
}
if( !$position || $position == 'no_sidebar' ) {
	$content_span = 'span12';
} else {
	$content_span = 'span9';
}


if ( $blog_layout == 'grid' && $enable_filter ) {

	$cat_args = array(
		'orderby' => 'name',
		'show_count' => '0',
		'hierarchical' => false,
		'title_li' => '',
		'order' => 'ASC',
		'style' => 'list',
		'hide_empty' => 1,
		'echo' => 0
	);

	$cats = get_categories($cat_args);

}

?>

<section id="content">
	<div class="container top">

		<div class="row">

			<?php if ( $responsive == 'top' && $position && $position != 'no_sidebar' ) { ?>
				<div class="<?php echo $sidebar_span; ?> sidebar sidebar-<?php echo $position; ?>">
					<?php
					if ( is_active_sidebar($sidebarname) ) {
						dynamic_sidebar($sidebarname);
					} else {
						echo '<p>' . __('There are no widgets added for this sidebar!', WFT_DOMAIN) . '</p>';
					}
					?>
				</div>
			<?php } //endif; ?>

			<div class="<?php echo $content_span; ?><?php if ( $position == 'left' ) { echo ' pull-right'; } ?>">

				<?php if ( $blog_layout == 'grid' && $enable_filter ) { ?>


					<section id="options" class="clearfix aligncenter">
						<ul id="filters" class="option-set" data-option-key="filter">
							<?php echo wft_get_category_filter($cats); ?>
						</ul>
					</section>



					<div class="isotope-outer">

				<?php } //endif; ?>


				<?php if(have_posts()): while(have_posts()) : the_post(); ?>

					<?php

					get_template_part('content', $blog_layout);

					?>

				<?php endwhile; ?>

				<?php if ( $blog_layout == 'grid' && $enable_filter ) { ?>
					</div>
				<?php } //endif; ?>
				<?php else: ?>

					<h1><?php _e('No posts were found!', WFT_DOMAIN) ?></h1>

				<?php endif; ?>

				<div class="post-navigation">
					<div class="pull-left"><?php next_posts_link(__('< Older Posts', WFT_DOMAIN)); ?></div>
					<div class="pull-right"><?php previous_posts_link(__('Newer Posts >', WFT_DOMAIN)); ?></div>

				</div>

			</div>

			<?php if ( $responsive == 'bottom' && $position && $position != 'no_sidebar' ): ?>
				<div class="<?php echo $sidebar_span; ?> sidebar sidebar-<?php echo $position; ?>">
					<?php
					if ( is_active_sidebar($sidebarname) ) {
						dynamic_sidebar($sidebarname);
					} else {
						echo '<p>' . __('There are no widgets added for this sidebar!', WFT_DOMAIN) . '</p>';
					}
					?>
				</div>
			<?php endif; ?>

		</div>

	</div><!-- #content .container -->
</section><!-- #content -->

<?php get_footer(); ?>

<?php if ( $blog_layout == 'grid' && $enable_filter ) { ?>
<script type="text/javascript">

	jQuery(document).ready(function($) {

		var isotopeOuter = $('.isotope-outer');

		if (isotopeOuter.length != 0) {
			var $container = isotopeOuter;

			if (isotopeOuter.find('.post-preview-small')[0]) {
				$container.isotope({
					itemSelector: '.post-preview-small'
				});
				var $optionSets = $('#options .option-set'),
					$optionLinks = $optionSets.find('a');
				$optionLinks.click(function () {

					jQuery('#ajax_loader').show();
					var cat_id = $(this).attr('data-cat-id');

					$.ajax({
						url : woocommerce_params.ajax_url,
						method : 'GET',
						data : 'cat=' + cat_id + '&action=wft_get_posts_by_category&context=frontend',
						dataType : 'json',
						success: function(response) {

							var $container = $('.isotope-outer');

							if ( isotope_animation_enabled == 1 ) {

								$container.isotope('remove', $container.data('isotope').$allAtoms );
								var newItems = $(response.posts);
								$container.isotope( 'insert', newItems );
							} else {
								$container.empty().html(response.products);
							}
							jQuery('#ajax_loader').hide();
						}
					});

					return false;
				});
			}
		}


	}(jQuery));

</script>
<?php } //endif; ?>