<?php
/**
 * @version    $Id$
 * @package    IT_Theme_Framework
 * @author     InnoThemes Team <support@innothemes.com>
 * @copyright  Copyright (C) 2012 InnoThemes.com. All Rights Reserved.
 * @license    GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Websites: http://www.innothemes.com
 */

// Get theme options
global $it_options;

//Menu as Select Dropdown
class Walker_Nav_Menu_Dropdown extends Walker_Nav_Menu {
	var $to_depth = -1;

	function start_lvl( &$output, $depth ) {
		$output .= '</option>';  //don't output children opening tag (<ul>)
	}

	function end_lvl( &$output, $depth ) {
		$indent = str_repeat( "\t", $depth ); // don't output children closing tag
	}

	/**
	 * Start the element output.
	 *
	 * @param   string  $output  Passed by reference. Used to append additional content.
	 * @param   object  $item    Menu item data object.
	 * @param   int     $depth   Depth of menu item. May be used for padding.
	 * @param   array   $args    Additional strings.
	 *
	 * @return  void
	*/
	function start_el( &$output, $item, $depth, $args ) {
		$indent = '';

		if ( $depth ) {
			$indent = str_repeat( '&nbsp;', $depth * 4 );
		}

		$class_names = $value = '';
		$classes     = array();

		if ( isset( $item->classes ) ) {
			$classes = (array) $item->classes;
		}

		$classes[]   = 'menu-item-' . $item->ID;
		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
		$class_names = ' class="' . esc_attr( $class_names ) . '"';

		$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );

		if ( strlen( $id ) ) {
			$id = ' id="' . esc_attr( $id ) . '"';
		}

		$value       = ' value="'. $item->url .'"';
		$item_output = $args->before . $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;

		$output .= '<option' . $id . $value . $class_names . '>' . $indent . $item_output;
	}

	function end_el( &$output, $item, $depth ) {
		if ( '</option>' != substr( $output, -9 ) ) {
			$output .= '</option>'; // replace closing </li> with the option tag
		}
	}
}
?>
<nav id="it-mainmenu">
	<div id="it-responsive-menu" class="<?php esc_attr_e( $it_options['responsive_menu_type'] ); ?>">
		<div id="it-menu-toggle">
			<h3><?php _e( 'Menu', IT_THEME_TEXTDOMAIN ); ?></h3>
		</div><!--End div#it-menu-toggle-->

		<div id="it-menu-dropdown">
<?php
if ( 'on' == @$_POST['wp_customize'] && '' != @$_POST['customized'] && $it_options['main_menu'] ) {
	wp_nav_menu(
		array(
			'container'  => false,
			'menu_id'    => 'nav',
			'menu'       => $it_options['main_menu'],
			'walker'     => new Walker_Nav_Menu_Dropdown(),
			'items_wrap' => '<select id="menu-main-menu" onchange="if (this.value) window.location.href=this.value">%3$s</select>',
		)
	);
} else {
	wp_nav_menu(
		array(
			'container'      => false,
			'menu_id'        => 'nav',
			'theme_location' => 'main_menu',
			'walker'         => new Walker_Nav_Menu_Dropdown(),
			'items_wrap'     => '<select id="menu-main-menu" onchange="if (this.value) window.location.href=this.value">%3$s</select>',
		)
	);
}
?>
		</div><!--End div#it-menu-dropdown-->
	</div><!--End div#it-responsive-menu-->
<?php
if ( 'on' == @$_POST['wp_customize'] && '' != @$_POST['customized'] && $it_options['main_menu'] ) {
	wp_nav_menu(
		array(
			'menu'        => $it_options['main_menu'],
			'items_wrap'  => '<ul id="it-main-menu" class="nav-menu">%3$s</ul>',
			'fallback_cb' => array( 'IT_Theme_Render', 'menu_placeholder' ),
		)
	);
} else {
	wp_nav_menu(
		array(
			'theme_location' => 'main_menu',
			'items_wrap'     => '<ul id="it-main-menu" class="nav-menu">%3$s</ul>',
			'fallback_cb'    => array( 'IT_Theme_Render', 'menu_placeholder' ),
		)
	);
}
?>
</nav><!--End nv#it-mainmenu-->
