<?php
/**
 * @package WordPress
 * @subpackage Wu Wei
 */
if ( ! isset( $themecolors ) ) {
	$themecolors = array(
		'bg' => 'ffffff',
		'border' => 'd1d9dc',
		'text' => '516064',
		'link' => 'ff8a00',
		'url' => 'feb925',
	);
}

/**
 * Set the content width based on the theme's design and stylesheet.
 */
if ( ! isset( $content_width ) )
	$content_width = 460;

/**
 * Sets up theme defaults and registers support for WordPress features.
 */
function wu_wei_setup() {
	// Make theme available for translation
	load_theme_textdomain( 'wu-wei', get_template_directory() . '/languages' );

	// Add default posts and comments RSS feed links to head
	add_theme_support( 'automatic-feed-links' );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => __( 'Primary Navigation', 'wu-wei' ),
	) );

	// Add support for Post Formats
	add_theme_support( 'post-formats', array( 'aside', 'image', 'video', 'quote',  'link' ) );

	/**
	 * Setup the WordPress core custom background feature.
	 */
	add_theme_support( 'custom-background' );

	add_theme_support( 'custom-header', apply_filters( 'wu_wei_custom_header_args', array(
		'default-text-color'     => 'd1d9dc',
		'width'                  => 700,
		'height'                 => 144,
		'wp-head-callback'       => 'header_style',
		'admin-head-callback'    => 'wuwei_admin_header_style',
		'admin-preview-callback' => 'wuwei_admin_header_image',
	) ) );

	add_theme_support( 'print-style' );
}
add_action( 'after_setup_theme', 'wu_wei_setup' );

// Wu Wei has a small post title area so we need to remove the widont filter
function wuwei_wido() {
	remove_filter( 'the_title', 'widont' );
}
add_action( 'init', 'wuwei_wido' );

// Add a home link to the default menu fallback wp_page_menu and change the menu class
function wuwei_page_menu_args( $args ) {
	$args['show_home'] = true;
	$args['menu_class'] = 'menu menu-main';
	return $args;
}
add_filter( 'wp_page_menu_args', 'wuwei_page_menu_args' );

/**
 * Register widgetized area and update sidebar with default widgets.
 */
function wu_wei_widgets_init() {
	// Register sidebar 1
	register_sidebar( array (
		'name' => __( 'Footer Left', 'wu-wei' ),
		'id' => 'widget-area-1',
		'description' => __( 'Widgets in this area will be shown on the left side your blog footer.', 'wu-wei' ),
	) );

	// Register sidebar 2
	register_sidebar( array (
		'name' => __( 'Footer Middle', 'wu-wei' ),
		'id' => 'widget-area-2',
		'description' => __( 'Widgets in this area will be shown in the middle of your blog footer.', 'wu-wei' ),
	) );

	// Register sidebar 3
	register_sidebar( array (
		'name' => __( 'Footer Right', 'wu-wei' ),
		'id' => 'widget-area-3',
		'description' => __( 'Widgets in this area will be shown on the right side of your blog footer.', 'wu-wei' ),
	) );
}
add_action( 'widgets_init', 'wu_wei_widgets_init' );

/**
 * Enqueue scripts and styles
 */
function wuwei_scripts() {
	wp_enqueue_style( 'wu-wei', get_stylesheet_uri() );

	$options = get_option( 'wuwei_theme_options' );
	if ( isset( $options['colorscheme'] ) && $options['colorscheme'] == 1 ) {
		wp_enqueue_style( 'colours', get_template_directory_uri() . '/colours.css' );
	}
	if ( is_rtl() ) {
		wp_enqueue_style( 'colours-rtl', get_template_directory_uri() . '/colours-rtl.css' );
	}
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'wuwei_scripts' );

function header_style() {
// This function defines the style for the theme
// You can change these selectors to match your theme
?>
<style type="text/css">
#header img {
	margin: 2em 0 0 0;
}
<?php
// Has the text been hidden?
if ( 'blank' == get_header_textcolor() && get_header_image() != '' ) { ?>
	#header img {
		margin: -<?php echo get_custom_header()->height; ?>px 0 0 0;
	}
	.blog-name a {
		display: block;
		text-indent: -9000px;
		width: <?php echo get_custom_header()->width; ?>px;
		height: <?php echo get_custom_header()->height; ?>px;
	}
	.description {
		position: absolute;
		left: -9999px;
	}
<?php } elseif ( 'blank' == get_header_textcolor() && get_header_image() == '' ) {
// No text, no image
?>
.blog-name a, .description {
	position: absolute;
	left: -9999px;
}
<?php } elseif ( get_header_textcolor() != HEADER_TEXTCOLOR ) {
// If the user has set a custom color for the text use that
?>
.blog-name a:link, .blog-name a:visited, .description {
	color: #<?php echo header_textcolor(); ?>
}
<?php } ?>
</style>
<?php
}

function wuwei_admin_header_style() {
	$header_text_color = get_header_textcolor();
?>
<style type="text/css">
#headimg {
	font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;
	<?php if ( 'blank' != $header_text_color ) : ?>
	<?php endif; ?>
	border: none;
}
.appearance_page_custom-header #headimg {
	border: none;
	min-height: 1px;
}
#headimg h1 {
	font-size: 60px;
	line-height: 1em;
	margin: 0;
	padding-bottom:0.25em;
}
#headimg, #headimg h1 a {
	text-decoration: none;
}
#headimg #desc {
	color: #6A797D;
	font-size: 18px;
	padding-bottom: 30px;
}
<?php if ( 'blank' == $header_text_color ) { ?>
#header h1 a {
	display: none;
}
#header, #header h1 a {
	color: <?php echo $header_text_color ?>;
}
<?php } ?>
</style>
<?php
}

function wuwei_admin_header_image() {
	$style        = sprintf( ' style="color:#%s;"', get_header_textcolor() );
	$header_image = get_header_image();
?>
	<div id="headimg" style="max-width:<?php echo get_custom_header()->width; ?>px;">
		<h1 class="displaying-header-text"><a id="name"<?php echo $style; ?> onclick="return false;" href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
		<div class="displaying-header-text" id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
		<?php if ( ! empty( $header_image ) ) : ?>
			<img src="<?php echo esc_url( $header_image ); ?>" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />
		<?php endif; ?>
	</div>
<?php
}

/**
 * Return an additional class to body when the dark color scheme is being used.
 */
function wu_wei_body_class( $classes ) {
	$options = get_option( 'wuwei_theme_options' );

	if ( isset( $options[ 'colorscheme' ] ) && 1 == $options[ 'colorscheme' ] )
		$classes[] = 'wu-wei-dark';

	return $classes;
}
add_filter( 'body_class', 'wu_wei_body_class' );

/**
 * Filters wp_title to print a neat <title> tag based on what is being viewed.
 *
 * @since Wu Wei 1.2.4
 */
function wuwei_wp_title( $title, $sep ) {
	global $page, $paged;

	if ( is_feed() )
		return $title;

	// Add the blog name
	$title .= get_bloginfo( 'name' );

	// Add the blog description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title .= " $sep $site_description";

	// Add a page number if necessary:
	if ( $paged >= 2 || $page >= 2 )
		$title .= " $sep " . sprintf( __( 'Page %s', 'wu-wei' ), max( $paged, $page ) );

	return $title;
}
add_filter( 'wp_title', 'wuwei_wp_title', 10, 2 );

// Create Theme Options Page
require get_template_directory() . '/theme-options.php';

/**
 * Jetpack support for Wu Wei
 */
function wu_wei_jetpack_setup() {
	/**
	 * Add theme support for Infinite Scroll.
	 * See: http://jetpack.me/support/infinite-scroll/
	 */
	add_theme_support( 'infinite-scroll', array(
		'footer'         => 'content',
		'footer_widgets' => array( 'widget-area-1', 'widget-area-2', 'widget-area-3' )
	) );
}
add_action( 'after_setup_theme', 'wu_wei_jetpack_setup' );