<?php

if ( ! class_exists( 'gp_custom_menu' ) ) {

	class gp_custom_menu extends Walker_Nav_Menu {

		// Add classes to ul sub-menus
		function start_lvl( &$output, $depth = 0, $args = array() ) {
		
			// Depth dependent classes
			$indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' ); // code indent
			$display_depth = ( $depth + 1 ); // because it counts the first submenu as 0
			$classes = array(
				'sub-menu',
				( $display_depth % 2  ? 'menu-odd' : 'menu-even' ),
				( $display_depth >=2 ? 'sub-sub-menu' : '' ),
				'menu-depth-' . $display_depth
				);
			$class_names = implode( ' ', $classes );

			// Build html
			$output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
			
		}
  
		// Add main/sub classes to li's and links
		 function start_el(  &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
			global $wp_query, $gp;
	
			$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent

			// Depth dependent classes
			$depth_classes = array(
				( $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ),
				( $depth >=2 ? 'sub-sub-menu-item' : '' ),
				( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
				'menu-item-depth-' . $depth
			);
			$depth_class_names = esc_attr( implode( ' ', $depth_classes ) );

			// Depth dependent classes
			$display_depth = ( $depth + 1); // because it counts the first submenu as 0
			$sub_menu_classes = array(
				'sub-menu',
				( $display_depth % 2  ? 'menu-odd' : 'menu-even' ),
				( $display_depth >=2 ? 'sub-sub-menu' : '' ),
				'menu-depth-' . $display_depth
				);
			$submenu_depth_class_names = implode( ' ', $sub_menu_classes );
			
			// Parsed classes
			$classes = empty( $item->classes ) ? array() : (array) $item->classes;
			$class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );

			// Build html
			
			// Fallbacks Gauge v3.0+
			if ( preg_match( '/megamenu/', $class_names ) ) {
				$menu_type = get_post_meta( $item->ID, 'menu-item-menu-type', true ) ? get_post_meta( $item->ID, 'menu-item-menu-type', true ) : 'megamenu';
			} else {
				$menu_type = get_post_meta( $item->ID, 'menu-item-menu-type', true ) ? get_post_meta( $item->ID, 'menu-item-menu-type', true ) : 'standard-menu';
			}
			
			$output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="' . $menu_type . ' ' . get_post_meta( $item->ID, 'menu-item-columns', true ) . ' ' . get_post_meta( $item->ID, 'menu-item-content', true ) . ' ' . $depth_class_names . ' ' . $class_names . '">';

			// Link attributes
			$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        ) .'"' : '';
			$attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';
			
			if ( get_post_meta( $item->ID, 'menu-item-menu-type', true ) == 'tab-content-menu' OR get_post_meta( $item->ID, 'menu-item-menu-type', true ) == 'content-menu' ) {

				// Default variables
				$GLOBALS['gp_menu'] = true;
				$GLOBALS['gp_cats'] = $item->object_id;
				$GLOBALS['gp_orderby'] = 'date';
					
				gp_query_variables();

				// Posts per page depending on menu type
				if ( get_post_meta( $item->ID, 'menu-item-menu-type', true ) == 'content-menu' ) {
					$GLOBALS['gp_per_page'] = 5;
				} else {
					$GLOBALS['gp_per_page'] = 4;
				}
								
				$query_args = array(
					'post_status' 	  => 'publish',
					'post_type'       => array( 'post', 'page' ),
					'tax_query'       => array( 'relation' => 'OR', $GLOBALS['gp_post_cats'], $GLOBALS['gp_hub_cats'], $GLOBALS['gp_video_cats'], $GLOBALS['gp_portfolio_cats'], $GLOBALS['gp_slide_cats'] ),
					'orderby'         => 'date',
					'order'           => 'desc',
					'posts_per_page'  => $GLOBALS['gp_per_page'],
					'paged'           => 1,
				);

				$gp_query = new wp_query( $query_args ); 
			
				$dropdown = '<ul class="sub-menu ' . $submenu_depth_class_names . '">
				<li id="nav-menu-item-'. $item->ID . '" class="' . $class_names . '"' . gp_data_properties( 'menu' ) . '>';
					
					if ( $gp_query->have_posts() ) :
				
						if ( get_post_meta( $item->ID, 'menu-item-menu-type', true ) == 'tab-content-menu' ) {

							$taxonomies = get_taxonomies(); // Go through all taxonomies
							foreach ( $taxonomies as $taxonomy ) {
								$term_args = array(
									'parent'  => $item->object_id, // Get child categories
								);
								$terms = get_terms( $taxonomy, $term_args );
								if ( ! empty( $terms ) ) {
									$dropdown .= '<ul class="menu-tabs">
										<li id="' . $item->object_id . '" class="selected">' . __( 'All', 'gp_lang' ) . '</li>';		
										foreach( $terms as $term ) {
											if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
												$dropdown .= '<li id="' . $term->term_id . '">' . $term->name . '</li>';
											}
										}
									$dropdown .= '</ul>';
								} 
							}
													
						}

						$dropdown .= '<div class="inner-loop ' . sanitize_html_class( $gp['ajax'] ) . '" >';
						
						while ( $gp_query->have_posts() ) : $gp_query->the_post();

							$image = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), 198, 125, true, false, true );
							if ( $gp['retina'] == 'gp-retina' ) {
								$retina = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), 198 * 2, 125 * 2, true, true, true );
							} else {
								$retina = '';
							}
							
							$dropdown .= '<article class="post-item">

								<div class="post-thumbnail featured-content image-above-content">
									<a href="' . get_permalink() . '" title="' . gp_prefix_hub_title( get_the_ID() ) . '">
										<img src="' . $image[0] . '" data-rel="' . $retina . '" width="' . $image[1] . '" height="' . $image[2] . '" alt="' . gp_prefix_hub_title( get_the_ID() ) . '" class="wp-post-image" />
									</a>
								</div>

								<div class="loop-header">
									<h2 class="entry-title loop-title"><a href="' . get_permalink() . '" title="' . gp_prefix_hub_title( get_the_ID() ) . '">' . gp_prefix_hub_title( get_the_ID() ) . '</a></h2>	
								</div>
						
							</article>';
						
						endwhile; 
					
						$dropdown .= '</div><div class="pagination standard-pagination pagination-arrows">' . gp_get_previous_posts_page_link( $gp_query->max_num_pages ) . gp_get_next_posts_page_link( $gp_query->max_num_pages ) . '</div>';
											
					endif; wp_reset_postdata();
					$GLOBALS['gp_menu'] = null;
					$GLOBALS['gp_cats'] = null;
					$GLOBALS['gp_orderby'] = null;

				$dropdown .= '</li></ul>';

				$item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s%7$s',
					$args->before,
					$attributes,
					$args->link_before,
					apply_filters( 'the_title', $item->title, $item->ID ),
					$args->link_after,
					$args->after,
					$dropdown
				);
							
			} else {

				$item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
					$args->before,
					$attributes,
					$args->link_before,
					apply_filters( 'the_title', $item->title, $item->ID ),
					$args->link_after,
					$args->after
				);

			}
			
			// build html
			$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
							
		}

	}
} 

?>