<?php 
/**
 * Trending news super menu
**/ 
?>
<?php
class tn_super_menu extends Walker_Nav_Menu {
	
	function start_lvl(&$output, $depth = 0, $args = array()) {
		$output .= "\n<ul id=\"menu-links\">\n";
	}
	
	function end_lvl(&$output, $depth = 0, $args = array()) {
		$output .= "</ul>\n";
	}
	
	function start_el(&$output, $item, $depth = 0, $args = array(), $current_object_id = 0) {
		global $wp_query;
		$cat = $item->object_id;
		$indent = ($depth) ? str_repeat ( "\t", $depth ) : '';
		$class_names = $value = '';
		$classes = empty ( $item->classes ) ? array () : ( array ) $item->classes;
		$class_names = join ( ' ', apply_filters ( 'nav_menu_css_class', array_filter ( $classes ), $item ) );
		$class_names = ' class="' . esc_attr ( $class_names ) .'"';
		
		$output .= $indent . '<li id="menu-item-' . $item->ID . '"' . $value . $class_names . '>';
		$attributes = ! empty ( $item->attr_title ) ? ' title="' . esc_attr ( $item->attr_title ) . '"' : '';
		$attributes .= ! empty ( $item->target ) ? ' target="' . esc_attr ( $item->target ) . '"' : '';
		$attributes .= ! empty ( $item->xfn ) ? ' rel="' . esc_attr ( $item->xfn ) . '"' : '';
		$attributes .= ! empty ( $item->url ) ? ' href="' . esc_attr ( $item->url ) . '"' : '';
		$item_output = $args->before;
		$item_output .= '<a' . $attributes . ' title="'.$item->title.'" class="menu-link">';
		$item_output .= $args->link_before . apply_filters ( 'the_title', $item->title, $item->ID ) . $args->link_after;
		$item_output .= '</a>';	
		$item_output .= $args->after;
		$parent_tax = get_post_meta( $item->menu_item_parent, '_menu_item_object', true );
		$children = get_posts ( array (
				'post_type' => 'nav_menu_item',
				'nopaging' => true,
				'numberposts' => 1,
				'meta_key' => '_menu_item_menu_item_parent',
				'meta_value' => $item->ID ,
		) );		
				
		if ($depth == 0 && $item->object == 'category' && ! empty ( $children )) {
			$item_output .= '<div class="sub-menu-wrapper">';
		} elseif (! empty ( $children )) {
			$item_output .= '<div class="sub-meni">';
		} elseif ($depth == 0 && $item->object == 'category' && empty ( $children )) {
		} elseif ($depth == 1 && $item->object == 'category' && ! empty ( $children ) && $parent_tax == 'category') {
		}
						
		if ($depth == 0 && empty ( $children ) && $item->object == 'category' ) {
		} 
	
		elseif ($depth < 2 && $item->object == 'category' )  {
				  if ($parent_tax == 'category' || $parent_tax == ''){
						$cat = $item->object_id;		
						$sign_before_link = get_option('tn_sign_before_link'); 
						$item_output .= '<div class="sub-menu"><ul class="menu-thumbs big-post">';		
						global $post;
						$menuposts = get_posts ( array('numberposts' => 1, 'offset' => 0, 'cat' => $cat ));			
						foreach ( $menuposts as $post ) :
							setup_postdata ( $post );			
							$post_title = get_the_title ();
							$post_link = get_permalink ();
							$post_image = wp_get_attachment_image_src ( get_post_thumbnail_id ( $post->ID ), "feat-cat-thumb" );				
							$menu_post_image = '<img src="' . $post_image [0] . '" alt="' . $post_title . '" />';			
							$item_output .= '
							<li>
								<div class="big-featured-posts-image">
									<a href="' . $post_link . '" title="' . $post_title . '">' . $menu_post_image . '</a>
									<div class="big-featured-posts-title-box">
										<div class="big-featured-posts-title">
											<h2><a href="' . $post_link . '" class="big-featured-posts-title">' . $post_title . '</a></h2>
										</div><!--big-featured-posts-title-->
									</div><!--big-featured-posts-title-box-->
								</div><!--big-featured-posts-image-->		
							</li>';
						endforeach;
						wp_reset_query ();			
						$item_output .= '</ul>';
			
			
			
						$item_output .= '<ul class="menu-thumbs featured-posts-category-small">';		
						global $post;
						$menuposts = get_posts ( array('numberposts' => 1, 'offset' => 1, 'cat' => $cat ));			
						foreach ( $menuposts as $post ) :
							setup_postdata ( $post );			
							$post_title = get_the_title ();
							$category = get_the_category( $post );
							$post_link = get_permalink ();
							$post_time = human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago';
							$post_image = wp_get_attachment_image_src ( get_post_thumbnail_id ( $post->ID ), "small-thumb" );				
							$menu_post_image = '<img src="' . $post_image [0] . '" alt="' . $post_title . '" />';			
							$item_output .= '<li>
								<div class="small-featured-posts-image">				
								<a href="' . $post_link . '" title="' . $post_title . '">' . $menu_post_image . '</a>
								</div><!--small-featured-posts-image-->
									<div class="small-featured-posts-text">
										<span class="category-sidebar-news"><a href="'.$category[0]->cat_name.'">'.$category[0]->cat_name.'</a></span>
										<div class="small-featured-posts-title"><h2><a href="' . $post_link . '" class="small-featured-posts-title">' . $post_title . '</a></h2>
										</div><!--small-featured-posts-title-->
										<span class="tiny-date">Posted '. $post_time  .'</span>
									</div><!--small-featured-posts-text-->
							</li>';
						endforeach;
						wp_reset_query ();			
			
			
			
						
						$item_output .= '<ul class="featured-posts-category-tiny">';
						global $post;
						$menuposts = get_posts ( array('numberposts' => 3, 'offset' => 2, 'cat' => $cat ));			
						foreach ( $menuposts as $post ) :
							setup_postdata ( $post );			
							$post_title = get_the_title ();
							$post_time = human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago';
							$post_link = get_permalink ();
							$post_image = wp_get_attachment_image_src ( get_post_thumbnail_id ( $post->ID ), "tiny-thumb" );				
							$menu_post_image = '<img src="' . $post_image [0] . '" alt="' . $post_title . '" />';			
							$item_output .= '
							<li>
								<div class="tiny-featured-posts-image">
								<a href="' . $post_link . '" title="' . $post_title . '">' . $menu_post_image . '</a>
								</div><!--tiny-featured-posts-image-->
								<div class="tiny-featured-posts-text">
									<span class="post-arrow">'.$sign_before_link.'</span>
									<h2><a href="' . $post_link . '" class="tiny-featured-posts-title">' . $post_title . '</a>
									 <span class="tiny-date">Posted '. $post_time  .'</span>
									 </h2>
								</div><!--tiny-featured-posts-text-->
							</li>';
						endforeach;
						wp_reset_query ();			
						$item_output .= '</ul></ul>';
			
			
			
			
						$item_output .= '<ul class="menu-thumbs featured-posts-category-tiny">';		
						global $post;
						$menuposts = get_posts ( array('numberposts' => 5, 'offset' => 5, 'cat' => $cat ));			
						foreach ( $menuposts as $post ) :
							setup_postdata ( $post );			
							$post_title = get_the_title ();
							$post_time = human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago';
							$post_link = get_permalink ();
							$post_image = wp_get_attachment_image_src ( get_post_thumbnail_id ( $post->ID ), "tiny-thumb" );				
							$menu_post_image = '<img src="' . $post_image [0] . '" alt="' . $post_title . '" />';			
							$item_output .= '
							<li>
								<div class="tiny-featured-posts-image">
								<a href="' . $post_link . '" title="' . $post_title . '">' . $menu_post_image . '</a>
								</div><!--tiny-featured-posts-image-->
								<div class="tiny-featured-posts-text">
									<span class="post-arrow">'.$sign_before_link.'</span>
									<h2><a href="' . $post_link . '" class="tiny-featured-posts-title">' . $post_title . '</a>
									 <span class="tiny-date">Posted '. $post_time  .'</span>
									 </h2>
								</div><!--tiny-featured-posts-text-->
							</li>';
						endforeach;
						wp_reset_query ();			
						$item_output .= '</ul></div>';
						
				  
				  }
			  } 
		elseif ($depth == 0 && $item->object != 'category') {
		}		
		$output .= apply_filters ( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}
	function end_el(&$output, $item, $depth = 0, $args = array(), $current_object_id = 0) {
		$output .= "</li>\n";
	}
}