<?php

class cloudfwMegaMenuWalker extends Walker_Nav_Menu  {
	/**
	 * @see Walker_Nav_Menu::start_lvl()
	 * @since 3.0.0
	 *
	 * @param string $output Passed by reference.
	 * @param int $depth Depth of page.
	 */
	function start_lvl(&$output) {}

	/**
	 * @see Walker_Nav_Menu::end_lvl()
	 * @since 3.0.0
	 *
	 * @param string $output Passed by reference.
	 * @param int $depth Depth of page.
	 */
	function end_lvl(&$output) {
	}

	/**
	 * @see Walker::start_el()
	 * @since 3.0.0
	 *
	 * @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. Used for padding.
	 * @param int $current_page Menu item ID.
	 * @param object $args
	 */
	function start_el(&$output, $item, $depth, $args) {
		global $_wp_nav_menu_max_depth;
		$_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;

		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

		ob_start();
		$item_id = esc_attr( $item->ID );
		$removed_args = array(
			'action',
			'customlink-tab',
			'edit-menu-item',
			'menu-item',
			'page-tab',
			'_wpnonce',
		);

		$original_title = '';
		if ( 'taxonomy' == $item->type ) {
			$original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
		} elseif ( 'post_type' == $item->type ) {
			$original_object = get_post( $item->object_id );
			$original_title = $original_object->post_title;
		}

		$classes = array(
			'menu-item menu-item-depth-' . $depth,
			'menu-item-' . esc_attr( $item->object ),
			'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
		);

		$title = $item->title;

		if ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
			$classes[] = 'pending';
			/* translators: %s: title of menu item in draft status */
			$title = sprintf( __('%s (Pending)'), $item->title );
		}

		$title = empty( $item->label ) ? $title : $item->label;

		?>

		<li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
			<dl class="menu-item-bar">
				<dt class="menu-item-handle">
					<span class="item-title"><?php echo esc_html( $title ); ?></span>
					<span class="item-controls">
						<span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
						<span class="item-order">
							<a href="<?php
								echo wp_nonce_url(
									add_query_arg(
										array(
											'action' => 'move-up-menu-item',
											'menu-item' => $item_id,
										),
										remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
									),
									'move-menu_item'
								);
							?>" class="item-move-up"><abbr title="<?php esc_attr_e('Move up'); ?>">&#8593;</abbr></a>
							|
							<a href="<?php
								echo wp_nonce_url(
									add_query_arg(
										array(
											'action' => 'move-down-menu-item',
											'menu-item' => $item_id,
										),
										remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
									),
									'move-menu_item'
								);
							?>" class="item-move-down"><abbr title="<?php esc_attr_e('Move down'); ?>">&#8595;</abbr></a>
						</span>
						<a class="item-edit" id="edit-<?php echo $item_id; ?>" title="<?php _e('Edit Menu Item'); ?>" href="<?php
							echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? admin_url( 'nav-menus.php' ) : add_query_arg( 'edit-menu-item', $item_id, remove_query_arg( $removed_args, admin_url( 'nav-menus.php#menu-item-settings-' . $item_id ) ) );
						?>"><?php _e( 'Edit Menu Item' ); ?></a>
					</span>
				</dt>
			</dl>

			<div class="menu-item-settings" id="menu-item-settings-<?php echo $item_id; ?>">
				<?php if( 'custom' == $item->type ) : ?>
					<p class="field-url description description-wide">
						<label for="edit-menu-item-url-<?php echo $item_id; ?>">
							<?php _e( 'URL' ); ?><br />
							<input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url" name="menu-item-url[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->url ); ?>" />
						</label>
					</p>
				<?php endif; ?>
				<p class="description description-thin">
					<label for="edit-menu-item-title-<?php echo $item_id; ?>">
						<?php _e( 'Navigation Label' ); ?><br />
						<input type="text" id="edit-menu-item-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-title" name="menu-item-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->title ); ?>" />
					</label>
				</p>
				<p class="description description-thin">
					<label for="edit-menu-item-attr-title-<?php echo $item_id; ?>">
						<?php _e( 'Title Attribute' ); ?><br />
						<input type="text" id="edit-menu-item-attr-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-attr-title" name="menu-item-attr-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->post_excerpt ); ?>" />
					</label>
				</p>
				<p class="field-link-target description description-thin">
					<label for="edit-menu-item-target-<?php echo $item_id; ?>">
						<?php _e( 'Link Target' ); ?><br />
						<select id="edit-menu-item-target-<?php echo $item_id; ?>" class="widefat edit-menu-item-target" name="menu-item-target[<?php echo $item_id; ?>]">
							<option value="" <?php selected( $item->target, ''); ?>><?php _e('Same window or tab'); ?></option>
							<option value="_blank" <?php selected( $item->target, '_blank'); ?>><?php _e('New window or tab'); ?></option>
						</select>
					</label>
				</p>
				<p class="field-css-classes description description-thin">
					<label for="edit-menu-item-classes-<?php echo $item_id; ?>">
						<?php _e( 'CSS Classes (optional)' ); ?><br />
						<input type="text" id="edit-menu-item-classes-<?php echo $item_id; ?>" class="widefat code edit-menu-item-classes" name="menu-item-classes[<?php echo $item_id; ?>]" value="<?php echo esc_attr( implode(' ', $item->classes ) ); ?>" />
					</label>
				</p>
				<p class="field-xfn description description-thin">
					<label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
						<?php _e( 'Link Relationship (XFN)' ); ?><br />
						<input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn" name="menu-item-xfn[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->xfn ); ?>" />
					</label>
				</p>
				<p class="field-description description description-wide">
					<label for="edit-menu-item-description-<?php echo $item_id; ?>">
						<?php _e( 'Description' ); ?><br />
						<textarea id="edit-menu-item-description-<?php echo $item_id; ?>" class="widefat edit-menu-item-description" rows="3" cols="20" name="menu-item-description[<?php echo $item_id; ?>]"><?php echo esc_html( $item->description ); ?></textarea>
						<span class="description"><?php _e('The description will be displayed in the menu if the current theme supports it.'); ?></span>
					</label>
				</p>
				
				
				<!--  CLOUDFW HERE :) -->

				<?php if($depth == 0):?>
                
                <p class="field-megamenu description description-wide">
					<label for="edit-menu-item-megamenu-<?php echo $item_id; ?>"><br />
						<?php _e( 'Enable Mega Menu?','cloudfw' ); ?><br />
                        <?php admin_create_onoff('menu-item-megamenu['. $item_id .']',get_post_meta( $item->ID, PFIX.'_mega_menu_active', true),FALSE);?>
					</label>
				</p>
                
                <?php endif; ?>
                 <div class="menu_walker_divider"></div>
                
               
                <p class="field-megamenu description-wide">
                	<a href="#" class="show_menu_options button-primary"><?php _e('Show Mega Menu Options','cloudfw'); ?></a>
                </p>
                
                <div class="cloudfw_menu_options">
                
                <?php if($depth == 0):?>
                
                <p class="field-megamenu description description-wide">
					<label for="edit-menu-item-columns-<?php echo $item_id; ?>">
						<?php _e( 'Menu Columns','cloudfw' ); ?><br />
                        
						 <?php								
                       $column = get_post_meta( $item->ID, PFIX.'_mega_menu_columns', true);
					   if (empty($column)) $column = 5;
						
                       for ( $counter = 1; $counter <= 5; $counter++) {
                                
                                $columns[] =  array(
								"item_title"	=> $counter,
								"item_value"	=> $counter
								);
                                            
                        }
			
						admin_create_selectlist(
								'menu-item-columns['. $item_id .']', // id
								$columns, // items
								"no-repeat", //default_value
								"select",
								$column,
								"select",
								"input input_100"
						);
									
						
						?>                        
                        
					</label>
				</p>                
                
                <?php endif; ?>
                
                <?php if($depth = 1):?>
                

                               
                
                <?php endif; ?>
                
                
                <?php if($depth > 0):?>
                <div class="menu_walker_divider"></div>
                <p class="field-css-subtext description description-wide">
					<label for="edit-menu-item-subtext-<?php echo $item_id; ?>">
						<?php _e( 'Sub-Text', 'cloudfw' ); ?><br />
						<input type="text" id="edit-menu-item-subtext-<?php echo $item_id; ?>" class="widefat edit-menu-item-subtext" name="menu-item-subtext[<?php echo $item_id; ?>]" value="<?php echo esc_attr( get_post_meta( $item->ID, PFIX.'_mega_menu_text', true) ); ?>" />
					</label>
				</p>
                <?php endif; ?>
                
                <?php if($depth >= 1):?>
                <div class="menu_walker_divider"></div>
					<div class="grid oneof3" style="margin-left:0;">
                    <label for="edit-menu-item-hide-image-<?php echo $item_id; ?>">
						<span class="description"><?php _e( 'Hide The Title?','cloudfw' ); ?></span><br />
                        <?php admin_create_onoff('menu-item-hide_title['. $item_id .']',get_post_meta( $item->ID, PFIX.'_mega_menu_hide_title', true),FALSE);?>
					</label>
                    </div>
                    <div class="grid oneof3">
                    <label for="edit-menu-item-hide-image-<?php echo $item_id; ?>">
						<span class="description"><?php _e( 'Image Type','cloudfw' ); ?></span><br />
						<?php								
                                        
                        $kinds[] =  array(
                                "item_title"	=> __('Normal','cloudfw'),
                                "item_value"	=> ''
                                );
                        $kinds[] =  array(
                                "item_title"	=> __('Icon','cloudfw'),
                                "item_value"	=> 'icon'
                                );
                                            
                        admin_create_selectlist(
                                'menu-item-image-kind['. $item_id .']', // id
                                $kinds, // items
                                "", //default_value
                                "select",
                                esc_attr( get_post_meta( $item->ID, PFIX.'_mega_menu_image_kind', true) ),
                                "select",
                                "input input_75"
                        );?>
					</label>
                    </div>
                    <div class="grid oneof3 last">
                    
                    <label for="edit-menu-item-hide-image-<?php echo $item_id; ?>">
						<span class="description"><?php _e( 'Icon Width','cloudfw' ); ?></span><br />
						<input type="text" id="edit-menu-item-image-width<?php echo $item_id; ?>" class="widefat" name="menu-item-image-width[<?php echo $item_id; ?>]" value="<?php echo esc_attr( get_post_meta( $item->ID, PFIX.'_mega_menu_image_width', true) ); ?>" style="width:50px;" /> px
					</label>
                    
                    </div>
                

                
                <p class="field-css-image description description-wide">
					<label for="edit-menu-item-image-<?php echo $item_id; ?>">
						<?php _e( 'Image URL', 'cloudfw' ); ?><br />
						<?php admin_create_upload_button( 
							'menu-item-image['. $item_id .']', // id of input:text
							esc_attr( get_post_meta( $item->ID, PFIX.'_mega_menu_image', true) ), // value
							TRUE, //preview
							'', // thumbnail creator url
							__('Upload','cloudfw'), // upload button test,
							$lastID, // refference Post ID of Uploading Images
							'
								<div class="clear"></div>
								
							', // description
							NULL, // delete button text
							'widefat edit-menu-item-image' // external classes for the text input
						 );?>
					</label>
				</p>
                
                <?php endif; ?>
                </div>
				
                
				<!--  / CLOUDFW -->


				<div class="menu-item-actions description-wide submitbox">
					<?php if( 'custom' != $item->type ) : ?>
						<p class="link-to-original">
							<?php printf( __('Original: %s'), '<a href="' . esc_attr( $item->url ) . '">' . esc_html( $original_title ) . '</a>' ); ?>
						</p>
					<?php endif; ?>
					<a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
					echo wp_nonce_url(
						add_query_arg(
							array(
								'action' => 'delete-menu-item',
								'menu-item' => $item_id,
							),
							remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
						),
						'delete-menu_item_' . $item_id
					); ?>"><?php _e('Remove'); ?></a> <span class="meta-sep"> | </span> <a class="item-cancel submitcancel" id="cancel-<?php echo $item_id; ?>" href="<?php	echo add_query_arg( array('edit-menu-item' => $item_id, 'cancel' => time()), remove_query_arg( $removed_args, admin_url( 'nav-menus.php' ) ) );
						?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
				</div>

				<input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
				<input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object_id ); ?>" />
				<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
				<input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" />
				<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
				<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
			</div><!-- .menu-item-settings-->
			<ul class="menu-item-transport"></ul>
		<?php
		$output .= ob_get_clean();
	}
}





function wpmega_add_menu_item_callback(){
	
	if ( ! current_user_can( 'edit_theme_options' ) )
		die('-1');

	check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );

	require_once ABSPATH . 'wp-admin/includes/nav-menu.php';

	$item_ids = wp_save_nav_menu_items( 0, $_POST['menu-item'] );
	if ( is_wp_error( $item_ids ) )
		die('-1');

	foreach ( (array) $item_ids as $menu_item_id ) {
		$menu_obj = get_post( $menu_item_id );
		if ( ! empty( $menu_obj->ID ) ) {
			$menu_obj = wp_setup_nav_menu_item( $menu_obj );
			$menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items
			$menu_items[] = $menu_obj;
		}
	}

	if ( ! empty( $menu_items ) ) {
		$args = array(
			'after' => '',
			'before' => '',
			'link_after' => '',
			'link_before' => '',
			'walker' =>	new cloudfwMegaMenuWalker,
		);
		echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
	}
}
add_action('wp_ajax_cloudfw-add-menu', 'wpmega_add_menu_item_callback');


function _walker_edit(){
	return 'cloudfwMegaMenuWalker';
}


function _update_menu_item($menu_id, $menu_item_db_id, $args ){
	
	$active_megamenu = $_POST['menu-item-megamenu'][$menu_item_db_id];
	
	if (empty($active_megamenu)) $active_megamenu = "FALSE";
	update_post_meta( $menu_item_db_id, PFIX.'_mega_menu_active', $active_megamenu );
	
	$hide_title = $_POST['menu-item-hide_title'][$menu_item_db_id];
	if (empty($hide_title)) $hide_title = "FALSE";
	update_post_meta( $menu_item_db_id, PFIX.'_mega_menu_hide_title', $hide_title );
	
	update_post_meta( $menu_item_db_id, PFIX.'_mega_menu_text', $_POST['menu-item-subtext'][$menu_item_db_id] );
	update_post_meta( $menu_item_db_id, PFIX.'_mega_menu_columns', $_POST['menu-item-columns'][$menu_item_db_id] );
	update_post_meta( $menu_item_db_id, PFIX.'_mega_menu_image', $_POST['menu-item-image'][$menu_item_db_id] );
	update_post_meta( $menu_item_db_id, PFIX.'_mega_menu_image_kind', $_POST['menu-item-image-kind'][$menu_item_db_id] );
	update_post_meta( $menu_item_db_id, PFIX.'_mega_menu_image_width', $_POST['menu-item-image-width'][$menu_item_db_id] );

}
		
function cloudfw_megamenu_admin_init(){
	add_filter( 'wp_edit_nav_menu_walker', '_walker_edit' , 500);
	add_action( 'wp_update_nav_menu_item', '_update_menu_item', 10, 3); //, $menu_id, $menu_item_db_id, $args;
}

?>