<?php
$excerpt_symbols_count = ThemeMakersThemeModel::get_option('excerpt_symbols_count');
if (!$excerpt_symbols_count) {
	$excerpt_symbols_count = 140;
}
$unique_id = uniqid();
$consoles = get_terms(array("cons"));
?>
<div class="topgames widget">

	<?php if ($instance['title'] != '') { ?>
		<div class="title-caption">
			<h3><?php echo $instance['title']; ?></h3>
		</div><!--/ .title-captin -->
	<?php } ?>

    <div class="entry-holder">

        <div class="tabs-2">

			<?php if ($instance['show_console_filter']): ?>
				<ul class="tabs-nav tabs-2 clearfix">
					<li class="active"><a href="#tab_all_<?php echo $unique_id ?>"><?php _e('See all', THEMEMAKERS_THEME_FOLDER_NAME); ?></a></li>
					<?php foreach ($consoles as $key => $console) : ?>
						<?php if (isset($instance['consoles_' . $console->term_id])): ?>
							<?php if ($instance['consoles_' . $console->term_id]): ?>
								<li><a href="#tab_<?php echo $key ?>_<?php echo $unique_id ?>"><?php echo $console->name ?></a></li>
							<?php endif; ?>
						<?php endif; ?>
					<?php endforeach; ?>
				</ul><!--/ .tabs-nav -->
			<?php endif; ?>

            <div class="tabs-container">

				<?php
				$orderby = 'rand';
				$order = '';
			//	if ($instance['show_console_filter']) {
					$orderby = $instance['orderby'];
					$order = $instance['sort_order'];
			//	}

				if($orderby=="rating"){
						$args = array(
								'post_type' => $instance['post_type'],
								'showposts' => $instance['post_number'],
								'meta_key' => 'post_rating', 
								'meta_compare' => '>=', 
								'meta_value' => 0, 
								'orderby' => 'meta_value_num', 
								'order' => $order
								);				
				}
				else{
						$args = array(
								'post_type' => $instance['post_type'],
								'showposts' => $instance['post_number'],
								'orderby' => $orderby,
								'order' => $order
								);
				}		
								
				$posts = get_posts($args);
				?>

                <div id="tab_all_<?php echo $unique_id ?>" class="tab-content" style="display: none;">

                    <ul class="rate">
						
						<?php foreach ($posts as $key => $post_item) : ?>

							<li>
								
															
								<?php if (has_post_thumbnail($post_item->ID) AND $instance['show_thumbnail']) { ?>
									<a href="<?php echo get_post_permalink($post_item->ID); ?>"><img class="alignleft" alt="" src="<?php echo ThemeMakersHelper::get_post_featured_image($post_item->ID, 'single_thumb3'); ?>"  alt="<?php echo $post_item->post_title ?>"></a>                                        
								<?php } ?>

								<div class="teaser-content">

									<h6><a href="<?php echo get_post_permalink($post_item->ID); ?>" class="title"><?php echo $post_item->post_title ?></a></h6>
																									
									<?php if ($instance['show_rating']): ?>
										<?php echo Thememakers_Entity_Rating::draw_rating($post_item->ID) ?>                                        
									<?php endif; ?>
									
									<?php if ($instance['show_exerpt']) : ?>

										<p>
											<?php
											if ($excerpt_symbols_count) {
												echo substr($post_item->post_excerpt, 0, $excerpt_symbols_count) . " ...";
											} else {
												echo $post_item->post_excerpt;
											}
											?>
										</p>

									<?php endif; ?>

									<div class="clear"></div>

								</div><!--/ .teaser-conent-->

								<div class="clear"></div>

							</li>

						<?php endforeach; ?>

                    </ul><!--/ .rate-->

                </div>

				<?php if ($instance['show_console_filter']): ?>
					<?php foreach ($consoles as $key => $console) : ?>

						<?php
						$args = array(
							'post_type' => $instance['post_type'],
							'showposts' => $instance['post_number'],
							'orderby' => $instance['orderby'],
							'order' => $instance['sort_order'],
							'taxonomy' => 'cons',
							'term' => $console->slug,
						);

						$posts = get_posts($args);
						?>

						<div id="tab_<?php echo $key ?>_<?php echo $unique_id ?>" class="tab-content" style="display: none;">

							<ul class="rate">
								
								<?php foreach ($posts as $key => $post_item) : ?>

									<li>

										<?php if (has_post_thumbnail($post_item->ID) AND $instance['show_thumbnail']) { ?>
											<a href="<?php echo get_post_permalink($post_item->ID); ?>"><img class="alignleft" alt="" src="<?php echo ThemeMakersHelper::get_post_featured_image($post_item->ID, 'single_thumb3'); ?>"  alt="<?php echo $post_item->post_title ?>"></a>                                        
										<?php } ?>

										<div class="teaser-content">

											<h6><a href="<?php echo get_post_permalink($post_item->ID); ?>" class="title"><?php echo $post_item->post_title ?></a></h6>

											<div class="teaser-date"><?php echo get_the_time('M d, Y', $post_item->ID); ?></div>

											<?php if ($instance['show_exerpt']) : ?>

												<p>
													<?php
													if ($excerpt_symbols_count) {
														echo substr($post_item->post_excerpt, 0, $excerpt_symbols_count) . " ...";
													} else {
														echo $post_item->post_excerpt;
													}
													?>
												</p>

											<?php endif; ?>

											<div class="clear"></div>

											<?php if ($instance['show_rating']): ?>
												<?php echo Thememakers_Entity_Rating::draw_rating($post_item->ID) ?>
											<?php endif; ?>

										</div><!--/ .teaser-conent-->

										<div class="clear"></div>

									</li>

								<?php endforeach; ?>

							</ul><!--/ .rate-->

						</div>

					<?php endforeach; ?>
				<?php endif; ?>


                

			</div><!--/ .tabs container-->		

        </div><!--/ .tabs-2-->

    </div><!--/ .entry-holder-->

</div>

