<?php

/*-----------------------------------------------------------------------------------*/
/* Register Custom Post Types - Portfolios */
/*-----------------------------------------------------------------------------------*/
function register_portfolio_post_type(){
	$permalink_slug = trim(theme_get_option('portfolio','permalink_slug'));
	if ( empty($permalink_slug) ) {
		$permalink_slug = 'portfolio';
	}
	register_post_type('portfolio', array(
		'labels' => array(
			'name' => _x('Portfolios', 'post type general name', THEME_ADMIN_DOMAIN ),
			'singular_name' => _x('Portfolio', 'post type singular name', THEME_ADMIN_DOMAIN ),
			'add_new' => _x('Add New', 'portfolio', THEME_ADMIN_DOMAIN ),
			'add_new_item' => __('Add New Portfolio', THEME_ADMIN_DOMAIN ),
			'edit_item' => __('Edit Portfolio', THEME_ADMIN_DOMAIN ),
			'new_item' => __('New Portfolio', THEME_ADMIN_DOMAIN ),
			'view_item' => __('View Portfolio', THEME_ADMIN_DOMAIN ),
			'search_items' => __('Search Portfolios', THEME_ADMIN_DOMAIN ),
			'not_found' =>  __('No portfolios found', THEME_ADMIN_DOMAIN ),
			'not_found_in_trash' => __('No portfolios found in Trash', THEME_ADMIN_DOMAIN ), 
			'parent_item_colon' => '',
			'menu_name' => __('Portfolio items', THEME_ADMIN_DOMAIN ),
		),
		'singular_label' => __('portfolio', THEME_ADMIN_DOMAIN ),
		'public' => true,
		'publicly_queryable' => true,
		'exclude_from_search' => false,
		'show_ui' => true,
		'show_in_menu' => true,
		//'menu_position' => 20,
		'capability_type' => 'post',
		'hierarchical' => false,
		'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments', 'page-attributes'),
		'has_archive' => false,
		'rewrite' => array( 'slug' => $permalink_slug, 'with_front' => true, 'pages' => true, 'feeds'=>false ),
		'query_var' => false,
		'can_export' => true,
		'show_in_nav_menus' => true,
	));

	//register taxonomy for portfolio
	register_taxonomy('portfolio_category','portfolio',array(
		'hierarchical' => true,
		'labels' => array(
			'name' => _x( 'Portfolio Categories', 'taxonomy general name', THEME_ADMIN_DOMAIN ),
			'singular_name' => _x( 'Portfolio Category', 'taxonomy singular name', THEME_ADMIN_DOMAIN ),
			'search_items' =>  __( 'Search Categories', THEME_ADMIN_DOMAIN ),
			'popular_items' => __( 'Popular Categories', THEME_ADMIN_DOMAIN ),
			'all_items' => __( 'All Categories', THEME_ADMIN_DOMAIN ),
			'parent_item' => null,
			'parent_item_colon' => null,
			'edit_item' => __( 'Edit Portfolio Category', THEME_ADMIN_DOMAIN ), 
			'update_item' => __( 'Update Portfolio Category', THEME_ADMIN_DOMAIN ),
			'add_new_item' => __( 'Add New Portfolio Category', THEME_ADMIN_DOMAIN ),
			'new_item_name' => __( 'New Portfolio Category Name', THEME_ADMIN_DOMAIN ),
			'separate_items_with_commas' => __( 'Separate Portfolio category with commas', THEME_ADMIN_DOMAIN ),
			'add_or_remove_items' => __( 'Add or remove portfolio category', THEME_ADMIN_DOMAIN ),
			'choose_from_most_used' => __( 'Choose from the most used portfolio category', THEME_ADMIN_DOMAIN ),
			'menu_name' => __( 'Categories', THEME_ADMIN_DOMAIN ),
		),
		'public' => true,
		'show_in_nav_menus' => false,
		'show_ui' => true,
		'show_tagcloud' => false,
		'query_var' => false,
		'rewrite' => false,
	));
}
add_action('init','register_portfolio_post_type',0);

function portfolio_context_fixer() {
	if ( get_query_var( 'post_type' ) == 'portfolio' ) {
		global $wp_query;
		$wp_query->is_home = false;
	}
	if ( get_query_var( 'taxonomy' ) == 'portfolio_category' ) {
		global $wp_query;
		$wp_query->is_404 = true;
		$wp_query->is_tax = false;
		$wp_query->is_archive = false;
	}
}
add_action( 'template_redirect', 'portfolio_context_fixer' );
