<?php
/*
Template Name: Homepage
*/	
	$homepage_slider_cat = get_option(THEME_NAME."_homepage_slider_cat");
	$homepage_slider = get_option(THEME_NAME."_homepage_slider");
	$homepage_image=get_option(THEME_NAME."_homepage_image");
	$homepage_textblocks_enabled = get_option(THEME_NAME."_homepage_text_blocks_enabled");
	
	$homepage_news_big = get_option(THEME_NAME."_homepage_news_big");
	$homepage_news_5 = get_option(THEME_NAME."_homepage_news_5");
	$homepage_news_9 = get_option(THEME_NAME."_homepage_news_9");
	$homepage_news_3_blocks = get_option(THEME_NAME."_homepage_news_3_blocks");
	$homepage_triple_box = get_option(THEME_NAME."_homepage_triple_box");
	
	if ( get_option(THEME_NAME."-slide-order-set") != "1" ) {
		$order = "";
	} else {
		$order = "&orderby=menu_order&order=ASC";
	}
?>
<?php get_header(); ?>
<?php get_template_part(THEME_INCLUDES.'top'); ?>

		<?php
			if ( $homepage_slider!= "off" && $homepage_slider ) { ?>
			<!-- BEGIN .homepage-slider-wrapper -->
			<div class="homepage-slider-wrapper">

				<!-- BEGIN .homepage-slider -->
				<div class="homepage-slider">
					<?php if ( $homepage_slider == "on" ) { ?>
					<?php $my_query = new WP_Query('cat='.$homepage_slider_cat.'&showposts=7'.$order); ?>
					<div id="homepage-slider">
							<?php if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
							<?php $image = get_post_thumb($post->ID,871,350,THEME_NAME.'_slider_image'); ?>
								<img src="<?php echo $image['src']; ?>" alt="&lt;a href=&quot;<?php the_permalink();?>&quot;&gt;&lt;b&gt;<?php the_title(); ?>&lt;/b&gt;&lt;/a&gt;&lt;p&gt;<?php the_excerpt(); ?>&lt;/p&gt;" width="871" height="350" />

							<?php endwhile; else: ?>
							<?php endif; ?>					
					</div>
					<div id="navigation"></div>
					<div id="control"></div>
					<div class="shadow">&nbsp;</div>
					<?php } else if ( $homepage_slider == "single" ) { ?>
						<div id="homepage-slider">
							<img src="<?php echo $homepage_image; ?>" width="871" height="350" />
						</div>
					<?php } ?>
				
				</div>
			</div>
		<?php } ?>
		
		<?php 
			if ( $homepage_textblocks_enabled=="on" ) {
				$homepage_text_bock_txt = get_option(THEME_NAME."_homepage_text_bock_txt");
		?>
			<!-- BEGIN .homepage-title -->
			<div class="homepage-title">
				<?php echo $homepage_text_bock_txt; ?>
			<!-- END .homepage-title -->
			</div>
		<?php } ?>
		
		<?php 
			if ( $homepage_news_big == "on" ) { 
			
				$homepage_news_big_cat = get_option( THEME_NAME."_homepage_news_big_cat" );
				$category_link_big = get_category_link( $homepage_news_big_cat );
				$homepage_news_big_cat_name = get_cat_name( $homepage_news_big_cat );	
				
				$counter = 0; 
				$i = 1;
				
				$query='cat='.$homepage_news_big_cat.'&showposts=7';
				
				//get selected post count
				$myposts = get_posts( $query );	
				$count_total = count( $myposts );
				
				$my_query = new WP_Query($query);
				
		?>
			<!-- BEGIN .homepage-block-1 -->
			<div class="homepage-block-1">
				<div class="main-title"><span><?php printf ( __( 'Latest in' , THEME_NAME ));?> <?php echo $homepage_news_big_cat_name;?></span><a href="<?php echo $category_link_big;?>"><?php printf ( __( 'read more about' , THEME_NAME ));?> <?php echo $homepage_news_big_cat_name;?></a></div>

				<!-- BEGIN .layout-1 -->
				<div class="layout-1">
					<?php if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
					<?php if ( $counter == 0 ) { ?>
					<?php $image = get_post_thumb( $post->ID, 315, 177 ); ?>
						<div class="featured">
							<a href="<?php the_permalink();?>"><img src="<?php echo $image['src'];?>" alt="" width="315" height="177" class="image" /></a>
							<div>
								<a href="<?php the_permalink();?>" class="title">
									<?php the_title(); ?>
								</a>
								<?php if ( comments_open() ) { ?>
									<a href="<?php the_permalink(); ?>#comments" class="comments"><?php comments_number('0', '1', '%'); ?></a>
								<?php } ?>
							</div>
						</div>
					<?php } ?>
					<?php if ( $counter == 1 ) { ?>	
						<div class="list">
					<?php } ?>
					<?php if ( $counter >= 1 && $counter <= 4) { ?>
					<?php $image = get_post_thumb( $post->ID, 50, 50 ); ?>
						<div class="item">
							<div class="image"><a href="<?php the_permalink();?>"><img src="<?php echo $image['src'];?>" alt="" width="50" height="50" /></a></div>
							<div class="title"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></div>
							<div class="info">
								<span class="date">
									<?php echo the_time("d / m / Y"); ?>
								</span>
								<?php if ( comments_open() ) { ?>
									<a href="<?php the_permalink(); ?>#comments" class="comments"><?php comments_number('0', '1', '%'); ?></a>
								<?php } ?>
							</div>
						</div>
					<?php } ?>
					<?php 
						if ( $counter == 3 || $count_total == $i ) {
							$counter=0;
					?>	
						</div>
					<?php } ?>
					
					<?php 
						$counter++; 
						$i++;
					?>
					<?php endwhile; else: ?>
					<?php endif; ?>	

	
				<!-- END .layout-1 -->
				</div>

			<!-- END .homepage-block-1 -->
			</div>
		<?php } ?>
		
		<?php 
			if ( $homepage_news_5 == "on" ) {
			
				$homepage_news_5_cat = get_option(THEME_NAME."_homepage_news_5_cat");
				$category_link_5 = get_category_link( $homepage_news_5_cat );
				$homepage_news_5_cat_name = get_cat_name($homepage_news_5_cat);
				
				$counter = 0; 
				
				$query='cat='.$homepage_news_5_cat.'&showposts=5';
				$my_query = new WP_Query($query);
		?>
			<!-- BEGIN .homepage-block-1 -->
			<div class="homepage-block-1">
				<div class="main-title"><span><?php printf ( __( 'Latest in' , THEME_NAME ));?> <?php echo $homepage_news_5_cat_name;?></span><a href="<?php echo $category_link_5;?>"><?php printf ( __( 'read more about' , THEME_NAME ));?> <?php echo $homepage_news_5_cat_name;?></a></div>

				<!-- BEGIN .layout-2 -->
				<div class="layout-2">
				<?php if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
				<?php $image = get_post_thumb( $post->ID, 151, 100 ); ?>
					<div class="item">
						<a href="<?php the_permalink();?>"><img src="<?php echo $image['src'];?>" alt="" width="151" height="100" class="image" /></a>
						<div class="title">
							<a href="<?php the_permalink();?>" class="text"><?php the_title(); ?></a>
							<?php if ( comments_open() ) { ?>
								<a href="<?php the_permalink();?>#comments" class="comments"><?php comments_number('0', '1', '%'); ?></a>
							<?php } ?>
						</div>
					</div>

				<?php $counter++; ?>
				<?php endwhile; else: ?>
				<?php endif; ?>	
				<!-- BEGIN .layout-2 -->
				</div>

			<!-- END .homepage-block-1 -->
			</div>
		<?php } ?>

		<?php 
			if ( $homepage_news_9 == "on" ) {
			
				$homepage_news_9_cat = get_option(THEME_NAME."_homepage_news_9_cat");
				$category_link_9 = get_category_link( $homepage_news_9_cat );
				$homepage_news_9_cat_name = get_cat_name($homepage_news_9_cat);
				
				$counter = 0; 

				$query='cat='.$homepage_news_9_cat.'&showposts=9';
				
				//get selected post count
				$myposts = get_posts( $query );	
				$count_total = count( $myposts );
				
				$my_query = new WP_Query($query);
		?>
			<!-- BEGIN .homepage-block-1 -->
			<div class="homepage-block-1">
				<div class="main-title"><span><?php printf ( __( 'Latest in' , THEME_NAME ));?> <?php echo $homepage_news_9_cat_name; ?></span><a href="<?php echo $category_link_9; ?>"><?php printf ( __( 'read more about' , THEME_NAME ));?> <?php echo $homepage_news_9_cat_name; ?></a></div>

				<!-- BEGIN .layout-3 -->
				<div class="layout-3">
				<?php if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
				<?php $image = get_post_thumb( $post->ID, 50, 50 ); ?>
					<?php if ( $counter == 0 ) { ?>
						<div class="list">
					<?php } ?>					
					<?php if ( $counter % 3 == 0 && $counter != 0 ) { ?>
						</div>
						<div class="list">
					<?php } ?>
							<div class="item">
								<div class="image"><a href="<?php the_permalink();?>"><img src="<?php echo $image['src'];?>" alt="" width="50" height="50" /></a></div>
								<div class="title"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></div>
								<div class="info">
									<span class="date">
										<?php echo the_time("d / m / Y"); ?>
									</span>
									<?php if ( comments_open() ) { ?>
										<a href="<?php the_permalink();?>#comments" class="comments"><?php comments_number('0', '1', '%'); ?></a>
									<?php } ?>
								</div>
							</div>

					<?php if ( $counter == $count_total-1 ) { ?>
						</div>
					<?php } ?>
					
				<?php $counter++; ?>
				<?php endwhile; else: ?>
				<?php endif; ?>	
				
				<!-- BEGIN .layout-3 -->
				</div>

			<!-- END .homepage-block-1 -->
			</div>
		<?php } ?>
		<?php 
			if ( $homepage_triple_box == "on" ) {
		?>
			<!-- BEGIN .homepage-block-1 -->
			<div class="homepage-block-1">

				<!-- BEGIN .tabs-1 -->
				<div class="tabs-1">
					<table>
						<tr>
							<td><a href="#" class="tab-1 kernel_triple_btn active" id="kernel_triple_popular_btn_kernel_triple_box-3"><span><?php printf ( __( 'Most popular posts' , THEME_NAME ));?></span></a></td>
							<td><a href="#" class="tab-1 tab-1-disabled kernel_triple_btn" id="kernel_triple_recent_btn_kernel_triple_box-3"><span><?php printf ( __( 'Recent posts' , THEME_NAME ));?></span></a></td>
							<td><a href="#" class="tab-1 tab-1-disabled kernel_triple_btn" id="kernel_triple_comments_btn_kernel_triple_box-3"><span><?php printf ( __( 'Comments' , THEME_NAME ));?></span></a></td>
						</tr>
					</table>
				<!-- END .tabs-1 -->
				</div>

				<!-- BEGIN .layout-5 -->
				<div class="layout-5" id="kernel_triple_popular_kernel_triple_box-3">
					<?php
						add_filter( 'posts_where', 'filter_where' );
						$args=array(
							'posts_per_page' => 5,
							'orderby' => 'comment_count',
							'post_type'=> 'any'
						);
						$the_query = new WP_Query($args);
						$myposts = get_posts( $args );	
						$count_total = count($myposts);
						remove_filter( 'posts_where', 'filter_where' );
					?>
					<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
					<?php $image = get_post_thumb($the_query->post->ID,151,100); ?>
					<div class="item">
						<a href="<?php the_permalink();?>"><img src="<?php if($image['src']) { echo $image['src']; } ?>" alt="" width="151" height="100" class="image" /></a>
						<div class="title"><a href="<?php the_permalink();?>" class="text"><?php the_title();?></a></div>
						<div class="info">
							<?php echo the_time("F d, Y"); ?>
							<?php if ( comments_open() ) { ?>
								<a href="<?php the_permalink();?>#comments" class="comments">
									<?php comments_number('0', '1', '%'); ?>
								</a>
							<?php } ?>
						</div>
					</div>
					<?php endwhile; else: ?>
					<p><?php printf ( __( 'No posts where found' , THEME_NAME ));?></p>
					<?php endif; ?>
				<!-- BEGIN .layout-5 -->
				</div>
				
				<!-- BEGIN .layout-5 -->
				<div class="layout-5" id="kernel_triple_recent_kernel_triple_box-3" style="display: none;">
					<?php
						$args=array(
							'posts_per_page'=> 5,
							'post_type'=> 'any'
						);
						$the_query = new WP_Query($args);
						$myposts = get_posts( $args );	
						$count_total = count($myposts);
					?>
					<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
					<?php $image = get_post_thumb($the_query->post->ID,151,100); ?>
					<div class="item">
						<a href="<?php the_permalink();?>"><img src="<?php if($image['src']) { echo $image['src']; } ?>" alt="" width="151" height="100" class="image" /></a>
						<div class="title"><a href="<?php the_permalink();?>" class="text"><?php the_title();?></a></div>
						<div class="info">
							<?php echo the_time("F d, Y"); ?>
							<?php if ( comments_open() ) { ?>
								<a href="<?php the_permalink();?>#comments" class="comments">
									<?php comments_number('0', '1', '%'); ?>
								</a>
							<?php } ?>
						</div>
					</div>
					<?php endwhile; else: ?>
					<p><?php printf ( __( 'No posts where found' , THEME_NAME )); ?></p>
					<?php endif; ?>
				<!-- BEGIN .layout-5 -->
				</div>
				
				<!-- BEGIN .layout-5 -->
				<div class="layout-5" id="kernel_triple_comments_kernel_triple_box-3" style="display: none;">
					<?php
						$comments = get_comments( array('status' => 'approve', 'order' => 'DESC' ) );
						if ( $comments ) {
							foreach($comments as $c) {
								$ids[] = $c->comment_post_ID ;
							}
													
							$ids=array_unique($ids);
							$ids=array_slice($ids, 0, 5);
						} else {
							$ids=0;
						}
						
						$args=array(
							'post__in' => $ids,
							'posts_per_page'=> 5,
							'post_type'=> 'any',
							'order'=> 'ASC'
						);
												
						$myposts = get_posts( $args );	
						$count_total = count($myposts);
												
						$the_query = new WP_Query($args);
												
					?>
					<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
					<?php $image = get_post_thumb($the_query->post->ID,151,100); ?>
					<div class="item">
						<a href="<?php the_permalink();?>"><img src="<?php if($image['src']) { echo $image['src']; } ?>" alt="" width="151" height="100" class="image" /></a>
						<div class="title"><a href="<?php the_permalink();?>" class="text"><?php the_title();?></a></div>
						<div class="info">
							<?php echo the_time("F d, Y"); ?>
							<?php if ( comments_open() ) { ?>
								<a href="<?php the_permalink();?>#comments" class="comments">
									<?php comments_number('0', '1', '%'); ?>
								</a>
							<?php } ?>
						</div>
					</div>
					<?php endwhile; else: ?>
					<p><?php printf ( __( 'No posts where found' , THEME_NAME )); ?></p>
					<?php endif; ?>
				<!-- BEGIN .layout-5 -->
				</div>

			<!-- END .homepage-block-1 -->
			</div>
		<?php } ?>
		<?php 
			if ( $homepage_news_3_blocks == "on" ) {
		?>
			<!-- BEGIN .homepage-block-1 -->
			<div class="homepage-block-1">

				<!-- BEGIN .layout-4 -->
				<div class="layout-4">

				<?php
					 for ($i=1; $i<=3; $i++) {

						$homepage_news_3_blocks_cat = get_option(THEME_NAME."_homepage_news_3_blocks_cat_".$i);
						$category_link_3_blocks = get_category_link( $homepage_news_3_blocks_cat );
						$homepage_news_3_blocks_cat_name = get_cat_name($homepage_news_3_blocks_cat);	
						
						$counter = 0; 

						$query='cat='.$homepage_news_3_blocks_cat.'&showposts=3';
						
						//get selected post count
						$myposts = get_posts( $query );	
						$count_total = count( $myposts );
						
						$my_query = new WP_Query($query);
				?>
				<!-- BEGIN .item-wrapper -->
					<div class="item-wrapper<?php if ( $i == 3 ) : echo " last"; endif;?>">
						<div class="layout-4-title"><b><?php printf ( __( 'Latest in' , THEME_NAME ));?> <?php echo $homepage_news_3_blocks_cat_name;?></b></div>
						<?php if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
						<?php if ( $counter == 0 ) { ?>
						<?php $image = get_post_thumb( $post->ID, 254, 141 ); ?>
							<div class="featured-item">
								<a href="<?php the_permalink();?>" class="featured-image"><img src="<?php echo $image['src'];?>" alt="" width="254" height="141" /></a>
								<h5><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
								<p class="info"><?php echo the_time("F d, Y"); ?>&nbsp;&nbsp;&bull;&nbsp;&nbsp;
									<?php if ( comments_open() ) { ?>
										<a href="<?php the_permalink(); ?>#comments"><?php comments_number(__('No comments', THEME_NAME), __('1 comment', THEME_NAME), __('% comments', THEME_NAME)); ?></a>
									<?php } ?>
								</p>
								<p class="intro"><?php echo word_trim(get_the_excerpt(),18);?></p>
							</div>
						<?php } else { ?>
						<?php $image = get_post_thumb( $post->ID, 50, 50 ); ?>
						<div class="item">
							<div class="image">
								<a href="<?php the_permalink();?>"><img src="<?php echo $image['src'];?>" alt="" width="50" height="50" /></a>
							</div>
							<div class="text">
								<h5><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
								<p class="info"><?php echo the_time("F d, Y"); ?></p>
							</div>
						</div>
						<?php } ?>
						<?php if ($counter == $count_total-1) : ?>
							<p class="show-all"><a href="<?php echo $category_link_3_blocks;?>" class="btn-1 btn-1-color-default"><span><?php printf ( __( 'Show all %1$s articles', THEME_NAME ), $homepage_news_3_blocks_cat_name);?></span></a></p>
						<?php endif;?>
	
				<?php 
					$counter++;
					endwhile; else:
					endif;
				?>
					<!-- END .item-wrapper -->
					</div>
				<?php
					} 
				?>
				<!-- END .layout-4 -->
				</div>

			<!-- END .homepage-block-1 -->
			</div>
			<?php } ?>
			<?php get_footer(); ?>