I use ZURB Foundation sub theme and I need to theme my main menu which contains taxonomy terms. I noticed that my theme override breaks the active-trail. How can I make it work with my custom theme? Here is my theme_links__system_main_menu function:

function [mytheme]_links__system_main_menu($vars) {
  // Get all the main menu links
  $menu_links = menu_tree_output(menu_tree_all_data('main-menu'));
  // Initialize some variables to prevent errors
  $output = '';
  $sub_menu = '';
  foreach ($menu_links as $key => $link) {
    // Add special class needed for Foundation dropdown menu to work
    !empty($link['#below']) ? $link['#attributes']['class'][] = 'has-flyout' : '';
    // Render top level and make sure we have an actual link
    if (!empty($link['#href'])) {
      $output .= '<li' . drupal_attributes($link['#attributes']) . '>' . l($link['#title'], $link['#href']);
      // Get sub navigation links if they exist
      foreach ($link['#below'] as $key => $sub_link) {
        if (!empty($sub_link['#href'])) {
          $sub_menu .= '<li>' . l($sub_link['#title'], $sub_link['#href']) . '</li>';
        }
      }
      $output .=!empty($link['#below']) ? '<a href="#" class="flyout-toggle"><span> </span></a><ul class="flyout">' . 
                        $sub_menu . '</ul>' : '';
      // Reset dropdown to prevent duplicates
      unset($sub_menu);
      $sub_menu = '';
      $output .= '</li>';
    }
  }
  return '<ul class="nav hide-for-small">' . $output . '</ul>';
}