<?php
/*---------------------------------------
---------Customised Menu-----------------
-----------------------------------------*/
function next_custom_text_menu($image = NULL,$onep = 0)
{
	$locations = get_nav_menu_locations();
	
    if(isset( $locations['primary'] )) { $menu = wp_get_nav_menu_object( $locations['primary']); }else { $menu = '';}
	_wp_menu_item_classes_by_context( $menu_items );

	$return = '<header id="standard-nav">
             <article class="col-md-3 col-xs-3 text-left">
              <img class="standard-logo img-responsive" alt="" title="" src="'.$image.'"/>
            </article>
            <article class="col-md-9 col-xs-9 credits">
              <ul id="mast-nav" class="text-right clearfix">
            ';
if($menu != '')
	{
	$menu_items = wp_get_nav_menu_items($menu->term_id);		

	$menunu = array();
	foreach((array)$menu_items as $key => $menu_item )
	{
		$menunu[ (int) $menu_item->db_id ] = $menu_item;
	}
	unset($menu_items);

	foreach ($menunu as $i => $men ){
		if($men->menu_item_parent == '0')
		{
			$post_finder = get_post($men->object_id);
            $page_slug = $post_finder->post_name;
            $newlink   = strtolower(preg_replace('/[^a-zA-Z]/s', '', $page_slug)); 
            //Other menu items
			$return .= '<li>
			';
			    //Specific additions add custom icons
				if( ( 'page' == $men->object ))
				{
					$href =  '#'.$newlink;				
					$incl_onepage = get_post_meta($men->object_id,'next_include_onepage',true );

                  if($incl_onepage == 1)
                    {
						$href =  '#'.$newlink;
						$identifyClass = "scroll-link  is_onepage";
				    }
				    else
				    {
                       $href = $men->url;
                       $identifyClass = "not_onepage";
				    }	
				} 
				else 
				{
					$href =  $men->url;
					$identifyClass = "not_onepage";

				}
				$return .= '<a class="'.$identifyClass.'" href="'.$href.'" id="'.$newlink.'-linker" data-soffset="60">'.$men->title.'</a>';
     if(next_detect_child($i, false) ){
                      $return .= next_detect_child($i, true);
                    }	                  
			$return .= '</li>' . "\n";

		}
	}
}
	else
	{
      $return .= '<ul id="main-nav"><li  class="links">';
 	  $return .= '<a id="defaultam-linker" title="Configure Menu" href="'.site_url().'/wp-admin/nav-menus.php">Configure Menu</a>';       
	  $return .= '</li>' . "\n";  
	}

	unset($menunu);	
	$return .= '</ul>
            </article>
</header>' . "\n";

	echo $return;
}


function next_detect_child($parent, $echo = false){
    
  $parent = ($parent != "") ? $parent : '0';
  $locations = get_nav_menu_locations();
  $menu = wp_get_nav_menu_object( $locations[ 'primary' ] );
  $menu_items = wp_get_nav_menu_items( $menu->term_id );
  
  _wp_menu_item_classes_by_context( $menu_items );
  
  $menu_next = array();
  foreach( (array) $menu_items as $key => $menu_item ){
    if($menu_item->menu_item_parent == $parent)
      $menu_next[ (int) $menu_item->db_id ] = $menu_item;
  }
  unset ($menu_items);
  
  if( !$echo ){
    if( !empty($menu_next) )
      return true;
    else
      return false;
  } else {
    $child_ul = '<ul>' . "\n";
    $ret = '';
      foreach ( $menu_next as $i => $mnn ){


          //Specific additions add custom icons
        if( ( 'page' == $mnn->object ))
        {
          $post_finder = get_post($mnn->object_id);
          $page_slug = $post_finder->post_name;                 
          $newlink   = strtolower(preg_replace('/[^a-zA-Z]/s', '', $page_slug)); 
          $href =  '#'.$newlink;        
          $incl_onepage = get_post_meta($mnn->object_id,'next_include_onepage',true );

            if($incl_onepage == 1)
            {
                $href =  '#'.$newlink;
                $identifyClass = "scroll-link is_onepage";
            }
            else
            {
                $href = $mnn->url;
                $identifyClass = "not_onepage";
            } 

        } 
        else 
        {
          $href =  $mnn->url;
          $identifyClass = "not_onepage";

        }



        $ret .= '<li><a class=" '.esc_attr($identifyClass).'" href="'.esc_url($href).'" id="'.esc_attr($newlink).'-linker" data-soffset="60">'.$mnn->title.'</a></li>' . "\n";
      }
      unset ($menu_next);
    $child_ul_close = '</ul>' . "\n";
    
    if( !empty($ret) )
      return $child_ul . $ret . $child_ul_close;
  }    

  }

