<?php
// ==========================================================================
// NAVIGATION
// ==========================================================================
//
// Implementação de toda a navegação do tema
//

// ==========================================================================
// Menus
// ==========================================================================

register_nav_menus(array(
	'top-bar-l'         => __('Left Top Bar', 'theme'),
	'top-bar-r'         => __('Right Top Bar', 'theme'),
	'mobile-off-canvas' => __('Mobile', 'theme'),
	'dashborard-menu-dealer'   => __('Dashbroard Dealers' , 'theme'),
	'dashborard-menu-general'   => __('Dashbroard General' , 'theme'),
));

// Exibindo menus
// ==========================================================================

if ( ! function_exists( 'theme_top_bar_l' ) ) {
	function theme_top_bar_l() {
    wp_nav_menu(array(
			'container'       => false,
			'container_class' => '',
			'menu'            => '',
			'menu_class'      => 'top-bar-menu left',
			'theme_location'  => 'top-bar-l',
			'before'          => '',
			'after'           => '',
			'link_before'     => '',
			'link_after'      => '',
			'depth'           => 5,
			'fallback_cb'     => false,
			'walker'          => new Theme_Top_Bar_Walker()
    ));
	}
}

if ( ! function_exists( 'theme_top_bar_r' ) ) {
	function theme_top_bar_r() {
    wp_nav_menu(array(
			'container'       => false,
			'container_class' => '',
			'menu'            => '',
			'menu_class'      => 'top-bar-menu right',
			'theme_location'  => 'top-bar-r',
			'before'          => '',
			'after'           => '',
			'link_before'     => '',
			'link_after'      => '',
			'depth'           => 5,
			'fallback_cb'     => false,
			'walker'          => new Theme_Top_Bar_Walker()
    ));
	}
}

if ( ! function_exists( 'theme_mobile_off_canvas' ) ) {
	function theme_mobile_off_canvas() {
    wp_nav_menu(array(
			'container'       => false,
			'container_class' => '',
			'menu'            => '',
			'menu_class'      => 'off-canvas-list',
			'theme_location'  => 'mobile-off-canvas',
			'before'          => '',
			'after'           => '',
			'link_before'     => '',
			'link_after'      => '',
			'depth'           => 5,
			'fallback_cb'     => false,
			'walker'          => new Theme_Offcanvas_Walker()
    ));
	}
}

if ( ! function_exists( 'theme_dashborard_menu_dealer' ) ) {
	function theme_dashborard_menu_dealer() {
    wp_nav_menu(array(
			'container'       => false,
			'container_class' => '',
			'menu'            => '',
			'menu_class'      => 'top-bar-menu left',
			'theme_location'  => 'dashborard-menu-dealer',
			'before'          => '',
			'after'           => '',
			'link_before'     => '',
			'link_after'      => '',
			'depth'           => 5,
			'fallback_cb'     => false,
			'walker'          => new Theme_Top_Bar_Walker()

    ));
	}
}

if ( ! function_exists( 'theme_dashborard_menu_general' ) ) {
	function theme_dashborard_menu_general() {
    wp_nav_menu(array(
			'container'       => false,
			'container_class' => '',
			'menu'            => '',
			'menu_class'      => 'top-bar-menu left',
			'theme_location'  => 'dashborard-menu-general',
			'before'          => '',
			'after'           => '',
			'link_before'     => '',
			'link_after'      => '',
			'depth'           => 5,
			'fallback_cb'     => false,
			'walker'          => new Theme_Top_Bar_Walker()

    ));
	}
}

// Suports
// ==========================================================================

//
// Add support for buttons in the top-bar menu:
// 1) In WordPress admin, go to Apperance -> Menus.
// 2) Click 'Screen Options' from the top panel and enable 'CSS CLasses' and 'Link Relationship (XFN)'
// 3) On your menu item, type 'has-form' in the CSS-classes field. Type 'button' in the XFN field
// 4) Save Menu. Your menu item will now appear as a button in your top-menu
//
if ( ! function_exists( 'theme_add_menuclass' ) ) {
	function theme_add_menuclass($ulclass) {
	    $find = array('/<a rel="button"/', '/<a title=".*?" rel="button"/');
	    $replace = array('<a rel="button" class="button"', '<a rel="button" class="button"');

	    return preg_replace( $find, $replace, $ulclass, 1 );
	}
	add_filter( 'wp_nav_menu','theme_add_menuclass' );
}

// Customizando menus
// ==========================================================================

//
// Personalisar saida de menus para Foundation Off-canvas menu com suport multi-leveis
//
if ( ! class_exists( 'Theme_Offcanvas_Walker' ) ) :
class Theme_Offcanvas_Walker extends Walker_Nav_Menu {

	function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {
		$element->has_children = ! empty( $children_elements[ $element->ID ] );
		$element->classes[]    = ( $element->current || $element->current_item_ancestor ) ? 'active' : '';
		$element->classes[]    = ( $element->has_children && 1 !== $max_depth ) ? 'has-submenu' : '';

		parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
	}

	function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {
		$item_html = '';
		parent::start_el( $item_html, $object, $depth, $args );

		$classes = empty( $object->classes ) ? array() : (array) $object->classes;

		if ( in_array( 'label-title', $classes ) ) {
			$item_html = preg_replace( '/<a[^>]*>(.*)<\/a>/iU', '<label>$1</label>', $item_html );
		}

		$output .= $item_html;
	}

	function start_lvl( &$output, $depth = 0, $args = array() ) {
		$output .= "\n<ul class=\"left-submenu\">\n<li class=\"back\"><a href=\"#\">". __( 'Back', 'theme' ) ."</a></li>\n";
	}

}
endif;

//
// Personalizar a saída de menus para a Fundação top-bar
//
if ( ! class_exists( 'Theme_Top_Bar_Walker' ) ) :
class Theme_Top_Bar_Walker extends Walker_Nav_Menu {

	function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {
		$element->has_children = ! empty( $children_elements[ $element->ID ] );
		$element->classes[]    = ( $element->current || $element->current_item_ancestor ) ? 'active' : '';
		$element->classes[]    = ( $element->has_children && 1 !== $max_depth ) ? 'has-dropdown' : '';

		parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
	}

	function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {
		$item_html = '';
		parent::start_el( $item_html, $object, $depth, $args );

		$output .= ( 0 == $depth ) ? '<li class="divider"></li>' : '';

		$classes = empty( $object->classes ) ? array() : (array) $object->classes;

		if ( in_array( 'label', $classes ) ) {
			$output .= '<li class="divider"></li>';
			$item_html = preg_replace( '/<a[^>]*>(.*)<\/a>/iU', '<label>$1</label>', $item_html );
		}

	if ( in_array( 'divider', $classes ) ) {
		$item_html = preg_replace( '/<a[^>]*>( .* )<\/a>/iU', '', $item_html );
	}

		$output .= $item_html;
	}

	function start_lvl( &$output, $depth = 0, $args = array() ) {
		$output .= "\n<ul class=\"sub-menu dropdown\">\n";
	}

}
endif;

// ==========================================================================
// Paginação | Page Links
// ==========================================================================

//
// Links das paginas
//
if ( ! function_exists( 'theme_link_pages' ) ) {
	function theme_link_pages() {
		wp_link_pages( array('before' => '<nav id="page-nav"><p>' . __( 'Pages:', 'theme' ), 'after' => '</p></nav>' ) );
	}
}

//
// Paginação
//
if ( ! function_exists( 'theme_pagination' ) ) :
function theme_pagination() {
	global $wp_query;

	$big = 999999999; // This needs to be an unlikely integer
	$paginate_links = paginate_links( array(
		'base'      => str_replace( $big, '%#%', html_entity_decode( get_pagenum_link( $big ) ) ),
		'current'   => max( 1, get_query_var( 'paged' ) ),
		'total'     => $wp_query->max_num_pages,
		'mid_size'  => 5,
		'prev_next' => true,
		'prev_text' => __( '&laquo;', 'theme' ),
		'next_text' => __( '&raquo;', 'theme' ),
		'type'      => 'list',
	) );

	$paginate_links = str_replace( "<ul class='page-numbers'>", "<ul class='pagination'>", $paginate_links );
	$paginate_links = str_replace( '<li><span class="page-numbers dots">', "<li><a href='#'>", $paginate_links );
	$paginate_links = str_replace( "<li><span class='page-numbers current'>", "<li class='current'><a href='#'>", $paginate_links );
	$paginate_links = str_replace( '</span>', '</a>', $paginate_links );
	$paginate_links = str_replace( "<li><a href='#'>&hellip;</a></li>", "<li><span class='dots'>&hellip;</span></li>", $paginate_links );
	$paginate_links = preg_replace( '/\s*page-numbers/', '', $paginate_links );

	// Display the pagination if more than one page is found
	if ( $paginate_links ) {
		echo '<div class="pagination-centered">';
		echo $paginate_links;
		echo '</div><!--// end .pagination -->';
	}
}
endif;