<?php 
/** 
* @package WordPress 
* @subpackage restaurant-cafepress 
*/ 
//========= put you custom code here ============ 
add_action('wp_head', 'addCssStyle');

function addCssStyle(){
	echo "<style type='text/css'>\n";
		if ( function_exists('bm_write_current_bg')) echo bm_write_current_bg('body.custom','repeat-x');
	echo "</style> \n";
}

function hook_header(){
?>
	<div id="custom_header" class ="custom_header level2 level3 level4" >
		<div id="header_logo">
			<div class="one"><?php if ( function_exists('custom_header_logo')) custom_header_logo(); ?></div>		
		</div>
		<div id="header_contact">
			<div class="one"><?php if ( function_exists('custom_header_contact')) custom_header_contact(); ?></div>
		</div>
	</div>
	<div class="clear"></div>
<?php
} 
add_action('thesis_hook_header', 'hook_header');

function custom_menu(){
?>	
	<div id="custom_menu" class ="custom_menu level2 level3 level4">
		<?php //thesis_nav_menu(); ?>
	</div>
	<div class="clear"></div>
<?php
}
add_action('thesis_hook_before_html', 'custom_menu');

function custom_template(){	
	$pathToImageFolder = getImagePath(__FILE__);
	if (is_home() || is_front_page())  {// this is the home page template 
?>
	<div id="home_page"  class="home_page all_template level2 level3 level4" >
		<div id="left_content" class="left_content" >
			<div class="one"><?php if ( function_exists('home_page_left_content_one')) home_page_left_content_one(); ?></div>					
			<div class="two"><?php if ( function_exists('home_page_left_content_two')) home_page_left_content_two(); ?></div>
			<div class="three"><?php if ( function_exists('home_page_left_content_three')) home_page_left_content_three(); ?></div>
			<div class="four"><?php if ( function_exists('home_page_left_content_four')) home_page_left_content_four(); ?></div>
			<div class="clear"></div>	
		</div>
		<div id="right_content" class="right_content" >			
			<div class="one"><?php if ( function_exists('home_page_right_content_one')) home_page_right_content_one(); ?></div>
			<div class="two"><?php if ( function_exists('home_page_right_content_two')) home_page_right_content_two(); ?></div>
			<div class="three"><?php if ( function_exists('home_page_right_content_three')) home_page_right_content_three(); ?></div>
			<div class="four"><?php if ( function_exists('home_page_right_content_four')) home_page_right_content_four(); ?></div>
			<div class="clear"></div>
		</div>
		<div class="clear"></div>
	</div>
<?php 
	}else{
?>
	<div id="other_page"  class="other_page all_template level2 level3 level4" >
		<div id="left_content" class="left_content" >
			<div class="one"><?php if ( function_exists('other_page_left_content_one')) other_page_left_content_one(); ?></div>					
			<div class="clear"></div>	
		</div>
		<div id="right_content" class="right_content" >
			<div class="one"><?php if ( function_exists('other_page_right_content_one')) other_page_right_content_one(); ?></div>					
			<div class="clear"></div>
		</div>	
		<div class="clear"></div>
	</div>
<?php
	}
}
/* HOME PAGE IMPLEMENT */
remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
add_action('thesis_hook_custom_template', 'custom_template');


//	begining of side bars 
function do_register_sidebar()
{
	register_sidebar(array('name'=>'Homepage - Contact', 'id'=>'homepage-contact', 'before_title'=>'<h2>', 'after_title'=>'</h2>')); 
	register_sidebar(array('name'=>'Homepage - Hours', 'id'=>'homepage-hours', 'before_title'=>'<h2>', 'after_title'=>'</h2>'));
	register_sidebar(array('name'=>'Homepage - Location', 'id'=>'homepage-location', 'before_title'=>'<h2>', 'after_title'=>'</h2>'));
	register_sidebar(array('name'=>'Homepage - Menus', 'id'=>'homepage-menus', 'before_title'=>'<h2>', 'after_title'=>'</h2>'));
	register_sidebar(array('name'=>'Homepage - Footer Location 01', 'id'=>'homepage-footer-01', 'before_title'=>'<div id="homepage-footer-image-1"></div><h2>', 'after_title'=>'</h2>'));
	register_sidebar(array('name'=>'Homepage - Footer Location 02', 'id'=>'homepage-footer-02', 'before_title'=>'<div id="homepage-footer-image-2"></div><h2>', 'after_title'=>'</h2>'));
	register_sidebar(array('name'=>'Homepage - Footer Location 03', 'id'=>'homepage-footer-03', 'before_title'=>'<div id="homepage-footer-image-3"></div><h2>', 'after_title'=>'</h2>'));
}
//.............................................................................
// Homepage - Contact
function homepage_contact(){
	?>
		<div class="homepage-contact">
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Contact') ){ ?>
				<ul><li class="widget">No Widgets Added</li></ul>
			<?php } ?>
		</div>
	<?php
}

//.............................................................................
// Homepage - Hours

function homepage_hours(){
	?>
		<div class="homepage-hours">
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Hours') ){ ?>
				<ul><li class="widget">No Widgets Added</li></ul>
			<?php } ?>
		</div>
	<?php
}

//.............................................................................
// Homepage - Location

function homepage_location(){
	?>
		<div class="homepage-location">
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Location') ){ ?>
				<ul><li class="widget">No Widgets Added</li></ul>
			<?php } ?>
		</div>
	<?php
}

//.............................................................................
// Homepage - Menus

function homepage_menus(){
	?>
		<ul class="homepage-menus">
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Menus') ){ ?>
				<ul><li class="widget">No Widgets Added</li></ul>
			<?php } ?>
		</ul>
	<?php
}

//.............................................................................
// Homepage - Footer Location 01

function homepage_footer_01(){
	?>
		<div class="homepage-footer-01">
			<ul><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Footer Location 01') ){ ?>
				<li class="widget">No Widgets Added</li>
			<?php } ?>
                </ul></div>
	<?php
}

//.............................................................................
// Homepage - Footer Location 02

function homepage_footer_02(){
	?>
		<div class="homepage-footer-02">
			<ul><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Footer Location 02') ){ ?>
				<li class="widget">No Widgets Added</li>
			<?php } ?>
		</ul></div>
	<?php
}

//.............................................................................
// Homepage - Footer Location 03

function homepage_footer_03(){
	?>
		<div class="homepage-footer-03">
			<ul><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Footer Location 03') ){ ?>
				<li class="widget">No Widgets Added</li>
			<?php } ?>
		</ul></div>
	<?php
}
//	end of side bars 
//.............................................................................





/* CUSTOM Dennison+Wolfe FOOTER HOOK */

function add_custom_footer () { 
$pathToImageFolder = getImagePath(__FILE__);
?>
	<div class="footer level2 level3 level4">
		<div id="footer-custom">
			<?php if ( function_exists('custom_footer')) custom_footer(); ?>
		</div>		
	</div>
<?php }

add_action('thesis_hook_footer', 'add_custom_footer');




/* Custom 404 Hooks */
function custom_thesis_404_title() {	
	
?>
You have reached a page that does not exist.
<?php
}
add_action('thesis_hook_404_title', 'custom_thesis_404_title');

function custom_thesis_404_content() {
global $site_url;
?>
<p>Please use the navigation to find what you are looking for.
<?php 
	//echo '<script type="text/javascript" >window.location.href = "'.$site_url.'";</script>';
?>
</p>

<?php
}
add_action('thesis_hook_404_content', 'custom_thesis_404_content');


//=============================================================   BEGIN common to all Post   =========================================
add_action("admin_menu", "menu_admin_init");
function menu_admin_init(){
	add_meta_box("Default_Value-meta", "Default Value", "mb_default_value", "general_attributes", "normal", "low");
	add_meta_box("General_Options-meta", "Options", "mb_options", "general_attributes", "normal", "low");
	add_meta_box("Default_Value-meta", "Default Value", "mb_default_svalue", "specific_attributes", "normal", "low");
	add_meta_box("Specific_Options-meta", "Options", "mb_options", "specific_attributes", "normal", "low");
	add_meta_box("Menu_Category-meta", "Menu Category", "mb_menu_category", "menu_type", "normal", "low");
	add_meta_box("Menu_Section-meta", "Menu Section", "mb_menu_section", "menu_type", "normal", "low");
	add_meta_box("Menu_Section-meta-details", "Menu Section details", "mb_menu_details", "menu_type", "normal", "low");
	add_meta_box("Restaurant_Menu-meta", "Restaurant Menu", "mb_restaurant_menu", "restaurant_menu", "normal", "low");
}

add_action("manage_posts_custom_column",  "posts_custom_columns");

function posts_custom_columns($column){
	global $post;
	$custom = get_post_custom($post->ID);

	switch ($column) {
	case "category":
		the_category(', ');
		break;
	case "description":
		the_excerpt();
		break;
	case "menu_category":
		echo $custom["menu_category"][0];
		break;
	case "default":
		echo $custom["default_value"][0];
		break;
	case "menu_sections":
		echo implode(',',unserialize($custom["menu_sections"][0]));
		break;
	}
}

add_action('save_post', 'save_custom_post_type_posts',10,2);

function save_custom_post_type_posts($post_id,$post=false) {
    do_action("save_{$post->post_type}_post",$post_id);
} 

add_theme_support('post-thumbnails');

//=============================================================   END common to all Post  ======================================================================

//=============================================================   BEGIN Custom Post Type 'General Attributes'  ===================================================
add_action('init', 'create_general_attributes');
 
function create_general_attributes() {
 
	$labels = array(
		'name' => _x('General Attributes', 'post type general name'),
		'singular_name' => _x('General Attributes', 'post type singular name'),
		'add_new' => _x('Add New', 'General Attributes'),
		'add_new_item' => __('Add New General Attributes'),
		'edit_item' => __('Edit General Attributes'),
		'new_item' => __('New General Attributes'),
		'view_item' => __('View General Attributes'),
		'search_items' => __('Search General Attributes'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
	$args = array(
		'labels' => $labels,
		'description' => "this post type is used in capturing a General Attributes",
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
        'taxonomies' => array('post_tag', 'category'),
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'rewrite' => array('slug' => 'general_attributes'),
		'supports' => array('title','editor','thumbnail','custom-fields','revisions','excerpt')
	  ); 
 
	register_post_type( 'general_attributes' , $args );
}

add_filter("manage_edit-general_attributes_columns", "general_attributes_edit_columns");

function general_attributes_edit_columns($columns){
	$columns = array(
		"cb" => "<input type=\"checkbox\" />",
		"title" => "profile Title",
		"description" => "Description",
		"default" => "Default Value",
		"category" => "Category"
	);

	return $columns;
}

//=============================================== default value html =================================

function mb_default_value(){
  global $post;
  echo '<input type="hidden" name="menu_category_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  $custom = get_post_custom($post->ID);
  $default_value = $custom["default_value"][0];
  ?>
	<input type="text" name="default_value"  value="<?php echo $default_value; ?>" size="30" style="width:97%" />
 <?php
}
//================================================ end of default value html =======================

//================================================ options html ===================================

function mb_options(){
  global $post;
  $custom = get_post_custom($post->ID);
  $my_options = $custom["my_options"][0];
  ?>
	<input type="text" name="my_options_name"  id="my_options_name" value="Type option name here" size="30" style="width:40%"  
			onfocus="if (this.value == 'Type option name here') {this.value = '';}" 
			onblur="if (this.value == '') {this.value = 'Type option name here';}"/>
			
	<input type="text" name="my_options_value"  id="my_options_value"  value="Type option value here" size="30" style="width:40%"   
			onfocus="if (this.value == 'Type option value here') {this.value = '';}" 
			onblur="if (this.value == '') {this.value = 'Type option value here';}"/>
			
    <div onclick = "writeOptions();" class="button"style="width:10%; margin-top:10px; margin-bottom:20px;">Add Option</div>
	
	<input type="text" name="my_options"  id="my_options"  value="<?php echo $my_options; ?>" size="30" style="width:97%" />
	
	<script type="text/javascript">
		function writeOptions(){
			var myName = document.getElementById('my_options_name');
			var myValue = document.getElementById('my_options_value');
			var myOptions = document.getElementById('my_options');
			if (myName.value !== 'Type option name here' && myValue.value !== 'Type option value here'){
				myOptions.value += myName.value +','+ myValue.value + ';';
				myName.value = 'Type option name here';
				myValue.value = 'Type option value here';
			}else{
				alert('missing input');
			}
		}
	</script>
	
 <?php
}
//================================================ end of options html ================================

add_action('save_general_attributes_post','save_general_attributes',10,1);
function save_general_attributes($post_id){

	// verify if this is an auto save routine. 
	// If our form has not been submitted,  we dont want to do anything
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return;
	  
	// verify this came from the our screen and with proper authorization,
	// because save_post can be triggered at other times\
	if ( !wp_verify_nonce($_POST['menu_category_meta_box_nonce'], basename(__FILE__))) {
		return;
	}
	
	// check permissions
	if ( !current_user_can('edit_page', $post_id) ) 
		return;
	
	update_post_meta($post_id, "default_value", $_POST["default_value"]);
	update_post_meta($post_id, "my_options", $_POST["my_options"]);
}

//   =============================================================   END Custom Post Type 'General Attributes'  ====================================================

//	=============================================================   BEGIN Custom Post Type 'Specific Attributes'  ================================================
add_action('init', 'create_specific_attributes');
 
function create_specific_attributes() {
 
	$labels = array(
		'name' => _x('Specific  Attributes', 'post type general name'),
		'singular_name' => _x('Specific  Attributes', 'post type singular name'),
		'add_new' => _x('Add New', 'Specific  Attributes'),
		'add_new_item' => __('Add New Specific  Attributes'),
		'edit_item' => __('Edit Specific  Attributes'),
		'new_item' => __('New Specific  Attributes'),
		'view_item' => __('View Specific  Attributes'),
		'search_items' => __('Search Specific  Attributes'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
	$args = array(
		'labels' => $labels,
		'description' => "this post type is used in capturing a Specific  Attributes",
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
        'taxonomies' => array('post_tag', 'category'),
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'rewrite' => array('slug' => 'specific_attributes'),
		'supports' => array('title','editor','thumbnail','custom-fields','revisions','excerpt')
	  ); 
 
	register_post_type( 'specific_attributes' , $args );
}

add_filter("manage_edit-specific_attributes_columns", "specific_attributes_edit_columns");

function specific_attributes_edit_columns($columns){
	$columns = array(
		"cb" => "<input type=\"checkbox\" />",
		"title" => "profile Title",
		"description" => "Description",
		"default" => "Default Value",
		"category" => "Category"
	);

	return $columns;
}
//================================================ begining default value html =======================
function mb_default_svalue(){
  global $post;
  echo '<input type="hidden" name="menu_category_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  $custom = get_post_custom($post->ID);
  $default_value = $custom["default_value"][0];
  ?>
	<input type="text" name="default_value"  value="<?php echo $default_value; ?>" size="30" style="width:97%" />
 <?php
}
//================================================ end of default value html =======================


add_action('save_specific_attributes_post','save_specific_attributes',10,1);
function save_specific_attributes($post_id){

	// verify if this is an auto save routine. 
	// If our form has not been submitted,  we dont want to do anything
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return;
	  
	// verify this came from the our screen and with proper authorization,
	// because save_post can be triggered at other times\
	if ( !wp_verify_nonce($_POST['menu_category_meta_box_nonce'], basename(__FILE__))) {
		return;
	}
	
	// check permissions
	if ( !current_user_can('edit_page', $post_id) ) 
		return;
	
	update_post_meta($post_id, "default_value", $_POST["default_value"]);
	update_post_meta($post_id, "my_options", $_POST["my_options"]);
}


//   =============================================================   END Custom Post Type 'Specific  Attributes'  ====================================================

//	=============================================================   BEGIN Custom Post Type 'Menu Section'  ====================================================
add_action('init', 'create_menu_section');
 
function create_menu_section() {
 
	$labels = array(
		'name' => _x('Menu Section', 'post type general name'),
		'singular_name' => _x('Menu Section', 'post type singular name'),
		'add_new' => _x('Add New', 'Menu Section'),
		'add_new_item' => __('Add New Menu Section'),
		'edit_item' => __('Edit Menu Section'),
		'new_item' => __('New Menu Section'),
		'view_item' => __('View Menu Section'),
		'search_items' => __('Search Menu Section'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
	$args = array(
		'labels' => $labels,
		'description' => "this post type is used in capturing a menu section",
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
        'taxonomies' => array('post_tag', 'category'),
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'rewrite' => array('slug' => 'menu_sections'),
		'supports' => array('title','editor','thumbnail','custom-fields','revisions','excerpt')
	  ); 
 
	register_post_type( 'menu_sections' , $args );
}

add_filter("manage_edit-menu_sections_columns", "menu_section_edit_columns");

function menu_section_edit_columns($columns){
	$columns = array(
		"cb" => "<input type=\"checkbox\" />",
		"title" => "profile Title",
		"description" => "Description",
		"category" => "Category"
	);

	return $columns;
}


//   =============================================================   END Custom Post Type 'Menu Section'  ====================================================

//   =============================================================   BEGIN Custom Post Type 'Menu Type'  =====================================================

add_action('init', 'create_menu_type');
 
function create_menu_type() {
	$labels = array(
		'name' => _x('Menu Type', 'post type general name'),
		'singular_name' => _x('Menu Type', 'post type singular name'),
		'add_new' => _x('Add New', 'Menu Type'),
		'add_new_item' => __('Add New Menu Type'),
		'edit_item' => __('Edit Menu Type'),
		'new_item' => __('New Menu Type'),
		'view_item' => __('View Menu Type'),
		'search_items' => __('Search Menu Type'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
	$args = array(
		'labels' => $labels,	
		'description' => "this post type is used in capturing a Menu Type",
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
        'taxonomies' => array('post_tag', 'category'),
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'rewrite' => array('slug' => 'menu_type'),
		'supports' => array('title','editor','thumbnail','custom-fields','revisions','excerpt')
	  ); 
 
	register_post_type( 'menu_type' , $args );
}

 
function mb_menu_category(){
	global $post;
	echo '<input type="hidden" name="menu_category_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
	$custom = get_post_custom($post->ID);
	$menu_category = $custom["menu_category"][0];
	?>
		<input type="radio" name="menu_category"value="breakfast" <?php echo $menu_category == 'breakfast' ? 'checked="checked"' : '' ; ?> /> Breakfast </br>
		<input type="radio" name="menu_category"value="lunch" <?php echo $menu_category == 'lunch' ? 'checked="checked"' : '' ; ?> /> Lunch </br>
		<input type="radio" name="menu_category"value="dinner" <?php echo $menu_category == 'dinner' ? 'checked="checked"' : '' ; ?> /> Dinner </br>
	<?php
}

function mb_menu_section(){
	global $post;
	$custom = get_post_custom($post->ID);
	$menu_sections = $custom["menu_sections"][0];// ---------- get checked values
	$menu_sections = unserialize($menu_sections);
	
	$mainQ = new WP_Query(array('post_type' => 'menu_sections', 'orderby' => 'title', 'order' => 'asc'));// ---------- get db values
	$catposts = $mainQ->posts;
	foreach($catposts as $menu_section){
		$val = array_search($menu_section->post_title,$menu_sections);
?>
		<input type="checkbox" name="menu_sections[]" id="menu_sect" value="<?php echo $menu_section->post_title; ?>" <?php echo $val === false ? '' : 'checked="checked"' ; ?> />&nbsp;<?php echo $menu_section->post_title; ?></br>
<?php
	}
?>
		<div style="margin-top: 15px; width: 25%; text-align: center;" id="update_ms" class="button">Menu Section Details</div>
		<div  id="info_message"> </div>
		<div  id="hpostId"> <input type="hidden" name="my_postId" id="my_postId" value="<?php echo $post->ID;?>"/> </div>
<?php
}

function mb_menu_details(){
?>
		<div  id="menu_sd" > Select section or sections above then click on <strong> Menu Section Details</strong> button</div>
<?php
}

add_action('wp_ajax_menu_section_details', 'add_sd_meta_box', 10);

function add_sd_meta_box(){
	//----- get post id value ---------
	$post_id = $_POST["thePostId"];
	//----- get checked values ---------
	$menu_sections = $_POST["menu_sections"];
	$arrMenu_sections = split(",", $menu_sections);
	//----- create meta boxes ---------
	$arrNum=0;
	foreach($arrMenu_sections as $section){
		if ($section == '') {
			update_post_meta($post_id, "srtArr", $metaArr2 );
			die;
		}
		$sectionID = str_replace(' ', '_', $section);
		$uniqueid = 'ms_'.$sectionID;
		$filterArr = array('post_type'=>'menu_item');
		$meta_box = create_metabox($uniqueid,$section,$filterArr,$post_id);   // id , title , field filter post id
?>
		<div id="<?php echo $meta_box['id'].'-meta';?>" class="postbox" >
			<div class="handlediv" title="Click to toggle">
				<br/>
			</div>
			<h3 class="hndle">
				<span><?php echo $meta_box['title'];?></span>
			</h3>
			<div class="inside">
				<?php 
					$dummyPr = array('args' => $meta_box);
					mytheme_show_box($post_id, $dummyPr);
				?>
			</div>
		</div>
<?php
		$metaArr2[$arrNum] = $meta_box;
		$arrNum += 1;
	}
	update_post_meta($post_id, "srtArr", $metaArr2 );
	die;
}

function dummy_form(){
?>
	<div id="dummy_form" >
		<form method="post" accept-charset="utf-8" action="">
			<input type="hidden" name="action" value="menu_section_details"/>
		</form>
	</div>
<?php
}

add_action('admin_footer', 'dummy_form');

add_filter("manage_edit-menu_type_columns", "menu_type_edit_columns");
function menu_type_edit_columns($columns){
	$columns = array(
		"cb" => "<input type=\"checkbox\" />",
		"title" => "Menu Type",
		"description" => "Description",
		"menu_category" => "Menu Category",
		"menu_sections" => "Menu Section",
		"category" => "Category"
	);

	return $columns;
}

add_action('save_menu_type_post','save_menu_type',10,1);
function save_menu_type($post_id){
	$metaArr2 = get_post_meta($post_id, 'srtArr', true);
	// verify if this is an auto save routine. 
	// If our form has not been submitted,  we dont want to do anything
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return $postID;
	}
	// verify this came from the our screen and with proper authorization,
	// because save_post can be triggered at other times\
	if ( !wp_verify_nonce($_POST['menu_category_meta_box_nonce'], basename(__FILE__))) {
		return;
	}
	
	// check permissions
	if ( !current_user_can('edit_page', $post_id) ) 
		return;
	
	update_post_meta($post_id, "menu_category", $_POST["menu_category"]);
	update_post_meta($post_id, "menu_sections", $_POST["menu_sections"] );
	foreach ($metaArr2 as $meta_box) {
		foreach ($meta_box['fields'] as $field) {
			$old = get_post_meta($post_id, $field['id'], true);
			$new = fillValue($_POST[$field['id']],$field['type']);
			if ($new && $new != $old) {
				update_post_meta($post_id, $field['id'], $new);
			} elseif ($new == '' && $old) {
				delete_post_meta($post_id, $field['id'], $old);
			}
		}
	}
}

//   =============================================================   END Custom Post Type 'Menu Type'  =======================================================

//   =============================================================   BEGIN Custom Post Type 'Menu Item'  =====================================================

add_action('init', 'create_menu_item');
 
function create_menu_item() {
 
	$labels = array(
		'name' => _x('Menu Item', 'post type general name'),
		'singular_name' => _x('Menu Item', 'post type singular name'),
		'add_new' => _x('Add New', 'Menu Item'),
		'add_new_item' => __('Add New Menu Item'),
		'edit_item' => __('Edit Menu Item'),
		'new_item' => __('New Menu Item'),
		'view_item' => __('View Menu Item'),
		'search_items' => __('Search Menu Item'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
	$args = array(
		'labels' => $labels,
		'description' => "this post type is used in capturing a Menu Item",
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
        'taxonomies' => array('post_tag', 'category'),
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'rewrite' => array('slug' => 'menu_item'),
		'supports' => array('title','editor','thumbnail','custom-fields','revisions','excerpt')
	  ); 
 
	register_post_type( 'menu_item' , $args );
}

// END Custom Post Type 'Menu Item'
//.............................................................................
//.............................................................................
//.............................................................................
// BEGIN Adding Thesis Meta Boxes to 'Menu Items' */

add_action('admin_menu','my_admin_menu');
function my_admin_menu() {
	$post_options = new thesis_post_options;
	$post_options->meta_boxes();
		foreach ($post_options->meta_boxes as $meta_name => $meta_box)
		{
			add_meta_box($meta_box['id'], $meta_box['title'], array('thesis_post_options', 'output_' . $meta_name . '_box'), 'menu_item', 'normal', 'high'); #wp
		}
	add_action('save_post', array('thesis_post_options', 'save_meta')); #wp
}

// END Adding Thesis Meta Boxes to 'Menu Items'
//.............................................................................
//.............................................................................
//.............................................................................
// BEGIN Custom Columns for 'Menu Items'

add_filter('manage_edit-menu_item_columns', 'menu_item_edit_columns');

function menu_item_edit_columns($menu_item_columns){
	$menu_item_columns = array(
		"cb" => "<input type=\"checkbox\" />",
		"title" => "Menu Item Title",
		"description" => "Description",
		"category" => "Category"
	);
	return $menu_item_columns;
}

// END Custom Columns for 'Menu Items'
//.............................................................................
//.............................................................................
//.............................................................................
//	BEGIN Custom Fields Meta Box

$prefix = 'dwd_';
$metaArr = array();
//.............................................................................
//.............................................................................
//.............................................................................
// Add meta box

add_action('admin_menu', 'gen_attr_add_box');

function gen_attr_add_box() {
	global $metaArr;
	$filterArr = array('post_type'=>'general_attributes');
	$meta_box = create_metabox('gen_metabox','Menu General Details',$filterArr);   // id , title , field filter
	add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority'],$meta_box);
	$metaArr[0] = $meta_box;
}

add_action('admin_menu', 'spe_attr_add_box');

function spe_attr_add_box() {
	global $metaArr;
	$filterArr = array('post_type'=>'specific_attributes');
	$meta_box = create_metabox('spe_metabox','Menu Specific Details',$filterArr);   // id , title , field filter
	add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority'],$meta_box);
	$metaArr[1] = $meta_box;
}
//.............................................................................
//.............................................................................
//.............................................................................

// Save data from meta box
add_action('save_menu_item_post','save_menu_item',10,1);
function save_menu_item($post_id) {
	global $metaArr;
	
	// verify nonce
	if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
		return ;
	}

	// check autosave
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return ;
	}

	// check permissions
	if ( !current_user_can('edit_page', $post_id) ) 
		return;
	
	foreach ($metaArr as $meta_box) {
		foreach ($meta_box['fields'] as $field) {
			$old = get_post_meta($post_id, $field['id'], true);
			$new = fillValue($_POST[$field['id']],$field['type']);
			if ($new && $new != $old) {
				update_post_meta($post_id, $field['id'], $new);
			} elseif ($new == '' && $old) {
				delete_post_meta($post_id, $field['id'], $old);
			}
		}
	}
}
//=============================================================   END Custom Post Type 'Menu Item'  =========================================================

//=============================================================   BEGIN Custom Post Type 'Restaurant Menu'  ===================================================

add_action('init', 'create_restaurant_menu');
 
function create_restaurant_menu() {
 
	$labels = array(
		'name' => _x('Restaurant Menu', 'post type general name'),
		'singular_name' => _x('Restaurant Menu', 'post type singular name'),
		'add_new' => _x('Add New', 'Restaurant Menu'),
		'add_new_item' => __('Add New Restaurant Menu'),
		'edit_item' => __('Edit Restaurant Menu'),
		'new_item' => __('New Restaurant Menu'),
		'view_item' => __('View Restaurant Menu'),
		'search_items' => __('Search Restaurant Menu'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
	$args = array(
		'labels' => $labels,
		'description' => "this post type is used in capturing a Restaurant Menu",
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
        'taxonomies' => array('post_tag', 'category'),
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'rewrite' => array('slug' => 'menu_type'),
		'supports' => array('title','editor','thumbnail','custom-fields','revisions','excerpt')
	  ); 

	register_post_type( 'restaurant_menu' , $args );
}

function mb_restaurant_menu(){
global $post;
?>
	<div id="breakfast" class="postbox" >
		<div class="handlediv" title="Click to toggle">
			<br/>
		</div>
		<h3 class="hndle">
			<span>Breakfast</span>
		</h3>
		<div class="inside">
			<?php 
				$filterArr = array('post_type'=>'menu_type', 'meta_key'=>'menu_category', 'meta_value'=>'breakfast');
				$meta_box = create_metabox('singelmi','Singel Menu Item',$filterArr);
				$dummyPr = array('args' => $meta_box);
				mytheme_show_box($post->ID, $dummyPr);
				$metaArr[0] = $meta_box;
			?>
		</div>
	</div>
	<div id="lunch" class="postbox" >
		<div class="handlediv" title="Click to toggle">
			<br/>
		</div>
		<h3 class="hndle">
			<span>Lunch</span>
		</h3>
		<div class="inside">
			<?php 
				$filterArr = array('post_type'=>'menu_type', 'meta_key'=>'menu_category', 'meta_value'=>'lunch');
				$meta_box = create_metabox('singelmi','Singel Menu Item',$filterArr);
				$dummyPr = array('args' => $meta_box);
				mytheme_show_box($post->ID, $dummyPr);
				$metaArr[1] = $meta_box;
			?>
		</div>
	</div>
	<div id="dinner" class="postbox" >
		<div class="handlediv" title="Click to toggle">
			<br/>
		</div>
		<h3 class="hndle">
			<span> Dinner</span>
		</h3>
		<div class="inside">
			<?php 
				$filterArr = array('post_type'=>'menu_type', 'meta_key'=>'menu_category', 'meta_value'=>'dinner');
				$meta_box = create_metabox('singelmi','Singel Menu Item',$filterArr);
				$dummyPr = array('args' => $meta_box);
				mytheme_show_box($post->ID, $dummyPr);
				$metaArr[2] = $meta_box; 
			?>
		</div>
	</div>
	<div id="singelmi" class="postbox" >
		<div class="handlediv" title="Click to toggle">
			<br/>
		</div>
		<h3 class="hndle">
			<span> Singel Menu Item</span>
		</h3>
		<div class="inside">
			<?php 
				$filterArr = array('post_type'=>'menu_item');
				$meta_box = create_metabox('singelmi','Singel Menu Item',$filterArr);
				$dummyPr = array('args' => $meta_box);
				mytheme_show_box($post->ID, $dummyPr);
				$metaArr[3] = $meta_box;
			?>
		</div>
	</div>
<?php
	update_post_meta($post->ID, "srtArr2", $metaArr );
}

add_action('save_restaurant_menu_post','save_restaurant_menu',10,1);
function save_restaurant_menu($post_id){
	$metaArr= get_post_meta($post_id, 'srtArr2', true);
	foreach ($metaArr as $meta_box) {
		foreach ($meta_box['fields'] as $field) {
			$old = get_post_meta($post_id, $field['id'], true);
			$new = fillValue($_POST[$field['id']],$field['type']);
			if ($new && $new != $old) {
				update_post_meta($post_id, $field['id'], $new);
			} elseif ($new == '' && $old) {
				delete_post_meta($post_id, $field['id'], $old);
			}
		}
	}
}
//	add_action("admin_init", "menu_type_admin_init");
//	=============================================================   END Custom Post Type 'Restaurant Menu'  =====================================================

//	=============================================================  common functions  ===========================================================================
function fillValue($value, $type){
	switch ($type) {
		case 'manyoptions': //if you have many options save them commer as string
			return $value;
			break;
		default:
			return $value;
			break;
	}
}
function create_metabox($id,$title,$field_filter,$special_fill=""){
	$fields = $special_fill=="" ? get_attributes($field_filter) : get_attributes($field_filter, $id);
	$meta_box = array(
		'id' => $id,
		'title' => $title,
		'page' => 'menu_item',
		'context' => 'normal',
		'special_fill' => $special_fill,
		'priority' => 'high',
		'fields' => $fields 
	);
	return $meta_box;
}
function filter_where($where = '') {
  //published posts only
  global $blog_id;
  $where = str_replace("(wp_".$blog_id."_posts.post_status = 'publish' OR wp_".$blog_id."_posts.post_status = 'future' OR wp_".$blog_id."_posts.post_status = 'draft' OR wp_".$blog_id."_posts.post_status = 'pending' OR wp_".$blog_id."_posts.post_status = 'private')", "(wp_".$blog_id."_posts.post_status = 'publish')", $where);
  return $where;
}

function get_attributes($attributes_type, $special_id = ''){
	$fieldsArr = array();
	$arrCounter = 0;

	//========================= get elements from database ========================================
	// Register the filtering function
	add_filter('posts_where', 'filter_where');
	$fieldQ = new WP_Query(array(
		'post_type' => $attributes_type['post_type'],   //'general_attributes'  , specific_attributes ETC
		'posts_per_page' => 50,
		'orderby' => 'name',
		'order' => 'asc',
		'meta_query' => array(
							array(
								'key'=> $attributes_type['meta_key'], 
								'value'=> $attributes_type['meta_value'],
								'compare' => 'like',
								'type' => 'text'
							)
						)
	));
	remove_filter('posts_where', 'filter_where');
	$fieldposts = $fieldQ->posts;
	
	foreach($fieldposts as $field):
		$fieldsArr[$arrCounter]['name'] = $field->post_title;
		$fieldsArr[$arrCounter]['desc'] = $field->post_content;
		$fieldsArr[$arrCounter]['id'] = $special_id.'-'.$field->ID;//----- html field id ---------
			$postCat = get_the_category($field->ID);
			$custom = get_post_custom($field->ID);
		$fieldsArr[$arrCounter]['type'] = $postCat[0]->cat_name;        			//----- html field type ---------
		$fieldsArr[$arrCounter]['std'] = $custom['default_value'][0];   			//----- default field value ---------
		$fieldsArr[$arrCounter]['options'] = get_option_arry($custom['my_options'][0]);          //----- html control options ---------
		$arrCounter += 1;
	endforeach;
	//========================= end of get elements from database ==================================
	return $fieldsArr;

}
function get_option_arry($strOpt){
	$arrOpt = array();
	if($strOpt){
		$arrCounter = 0;
		$arrPairValues = split(";", $strOpt);
		foreach($arrPairValues as $pairValues):
			$pairs = split(",", $pairValues);
			if($pairs[0] && $pairs[1]){
				$arrOpt[$arrCounter][name] = $pairs[0];
				$arrOpt[$arrCounter][value] = $pairs[1];
			}
			$arrCounter += 1;
		endforeach;
	}else{
		return '';
	}
	return $arrOpt;
}

// Callback function to show fields in meta box
function mytheme_show_box($post, $meta_box_arr) {
	$colunmToggle = 0;
	$meta_box = $meta_box_arr['args'];
	$htmlStr = "";
	if(is_string($post)){
		$the_post_id = $post;
	}else{
		$the_post_id = $post->ID;
	}
	// Use nonce for verification
	$htmlStr .= "<input type='hidden' name='mytheme_meta_box_nonce' value='".wp_create_nonce(basename(__FILE__))."' />";
	
	$htmlStr .= "<table class='form-table'>";
	foreach ($meta_box["fields"] as $field) {
		// get current post meta data
		$meta = get_post_meta($the_post_id, $field["id"], true);
		if($colunmToggle == 1 && $field["type"] == "checkbox" ){
			
		}else{
			$htmlStr .= "<tr>";
		}
		$htmlStr .= "<th style='width:20%'><label for='".$field["id"]."'>".$field["name"]."</label></th><td>";
		switch ($field['type']) {
			case 'text':
				// if ($meta_box["special_fill"]=='special_fill'{
					
				// $tempv = $meta ? $meta : $field['std'];
				// }else{
					$tempv = $meta ? $meta : $field['std'];
				// }
				$htmlStr .= "<small style='color: #AAAAAA;'> ".$field["desc"]." </small> <br /> "." <input type='text' name='".$field['id']."' id='".$field['id']."' value='$tempv' size='30' style='width:97%' /> ";
				break;
			case "textarea":
				$tempv = $meta ? $meta : $field['std'];
				$htmlStr .= "<small style='color: #AAAAAA;'>".$field["desc"]."</small><br />"."<textarea name='".$field["id"]."' id='".$field["id"]."' cols='60' rows='4' style='width:97%'>$tempv</textarea>";
				break;
			case "dropdown":
				$htmlStr .= "<select style='width:40%;' name='".$field["id"]."' id='".$field["id"]."'>";
				foreach ($field["options"] as $option) {
					$tempv = $meta == $option["value"] ? " selected='selected'" : '';
					$htmlStr .= "<option   value ='".$option["value"]."' $tempv>".$option["name"]."</option> ";
				}
				$htmlStr .= "</select>";
				break;
			case "radiobutton":
				foreach ($field["options"] as $option) {
					$tempv = $meta == $option["value"] ? " checked='checked'" : '';
					$htmlStr .= "<input type='radio' name='".$field["id"]."' value='".$option["value"]."' $tempv />".$option["name"]."&nbsp;&nbsp;&nbsp;";
				}
				break;
			case "checkbox":
				$tempv = $meta ? " checked='checked'" : '';
				$htmlStr .= "<input type='checkbox' name='".$field["id"]."' value='".$field["name"]."' id='".$field["id"]."' $tempv />";
				break;
		}
		// $htmlStr .= ($colunmToggle == 0 && $field["type"] == "checkbox" );
		if($colunmToggle == 0 && $field["type"] == "checkbox" ){
			$htmlStr .= 	"</td>";
			$colunmToggle = 1;
		}else{
			$htmlStr .= 	"</td>";
			$htmlStr .= "</tr>";
			$colunmToggle = 0;
		}
	}
	$htmlStr .= "</table>";
	echo $htmlStr;
}
		
//.............................................................................
//.............................................................................
//.............................................................................

function mboxVoimitJavaScripts(){
	wp_enqueue_script( 'mboxscripts', '/wp-content/themes/skins/scripts/mata_box.js', null, '1.0', false );
}
add_action('init', 'mboxVoimitJavaScripts');
?>